我试图让当前用户 session 保持事件状态,直到用户决定注销。我应该实现什么代码?究竟应该在哪里实现? Root View 的 viewDidLoad 或 viewWillAppear 函数?我尝试在我的 Root View 中使用此代码:
override func viewDidAppear(animated: Bool) {
let vc = ViewController()
var loggedIn = false
if PFUser.currentUser() != nil {
loggedIn = true
} else {
presentViewController(vc, animated: true, completion: nil)
}
}
但是每当我停止模拟器并再次运行它时,我都必须重新登录。什么是最好的解决方案?保留在我的“ViewController”中是我的主视图,其中包含我的登录/注册字段。所以我基本上想要一种方法来说明当前用户 session 是否存在,继续,否则,显示初始 View 。
最佳答案
在您的应用委托(delegate)中试试这个...
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
// there is a user logged in go to the main screen
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
MainViewController *mainViewController = (MainViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Main"];
//set the root controller to it
self.window.rootViewController = mainViewController;
} else {
// Log in!
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Login" bundle: nil];
LoginViewController *loginViewController = (LoginViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Login"];
//set the root controller to it
self.window.rootViewController = loginViewController;
}
...
关于ios - 如何使用解析保持当前用户 session 处于事件状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31260045/