1

我试图保持当前用户会话处于活动状态,直到用户决定注销。我应该实现什么代码?究竟应该在哪里实施?根视图的 viewDidLoad 或 viewWillAppear 函数?我曾尝试在我的根视图中使用此代码:

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”中是我的主视图,其中包含我的登录/注册字段。所以我基本上想要一种方式来说明当前用户会话是否存在,继续,否则,显示初始视图。

4

3 回答 3

2

在您的应用委托中尝试此操作...

-(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;
}

...
于 2015-07-07T05:27:32.963 回答
0

signUpInBackgroundWithBlock当您使用(objective -c)正确注册用户时,Parse 会为您处理此问题。

PFUser.currentUser()应该一直可用,直到您主动注销。

也许您尝试注册->失败->无论如何都转到下一个VC->在没有实际注册的情况下再次启动应用程序?

于 2015-07-07T05:13:06.747 回答
0

Parse 为开发人员提供了 Sessions(用于会话管理)。如需进一步帮助,请参阅以下链接中的会话部分... https://parse.com/docs/ios/guide

于 2015-11-11T05:41:19.730 回答