我正在使用PFAnonymousUtils和PFUser.enableAutomaticUser。我想检查用户是否存在是数据库,如果是,我想pivot到DiscoverViewController。
问题是,即使没有用户,应用程序也会继续转发到DiscoverVc…
let currentUser = PFUser.currentUser()
if currentUser != nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// instantiate your desired ViewController
let rootController = storyboard.instantiateViewControllerWithIdentifier("DiscoverVc")
// Because self.window is an optional you should check it's value first and assign your rootViewController
if self.window != nil {
self.window!.rootViewController = rootController
}
} else {
let installation = (PFInstallation.currentInstallation())
installation["User"] = PFUser.currentUser()
installation.saveInBackground()
PFAnonymousUtils.logInWithBlock {
(user: PFUser?, error: NSError?) -> Void in
if error != nil || user == nil {
print("Anonymous user failed")
} else {
print("Anonymous user logged in.")
}
}
}
来自解析文档:
PFUser.enableAutomaticUser
调用此方法后,currentUser将始终有一个值。的用户只有在保存后才会在服务器上创建,或者是与该用户或引用的ACL有关系的对象到用户已保存
检查用户是否为匿名用户,使用:
PFAnonymousUtils.isLinkedWithUser(PFUser.currentUser())
如果用户是匿名用户,返回值YES。如果用户不是当前用户或非匿名。