让用户登录ios app swift

时间:2016-06-12 00:50:40

标签: ios swift xcode7

我制作了包含登录和注册功能的应用。当我运行我的应用程序时,登录和其他功能一切正常,但当我停止我的应用程序并再次运行它登录页面再次显示我可以让用户登录时再次启动应用程序。以下是我在我的应用中使用的代码。

<ion-navbar secondary *navbar>
  <ion-title>
    <ion-searchbar primary placeholder="enter subreddit name..."></ion-searchbar>
  </ion-title>

  <ion-buttons end>
    <button (click)="openSettings()"><ion-icon name="settings"></ion-icon></button>
  </ion-buttons>
</ion-navbar>

2 个答案:

答案 0 :(得分:4)

在AppDelegate中: -

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    let userLoginStatus = NSUserDefaults.standardUserDefaults().boolForKey("isUserLoggedIn")

if(userLoginStatus)
{

    let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    .
    .

}

在LoginViewController中(您已经为登录验证放置了业务逻辑): -

NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isUserLoggedIn")
NSUserDefaults.standardUserDefaults().synchronize()

在OtherViewController中(保留注销的IBAction): -

   NSUserDefaults.standardUserDefaults().setBool(false, forKey: "isUserLoggedIn")
   NSUserDefaults.standardUserDefaults().synchronize()

答案 1 :(得分:2)

在“didFinishLaunchingWithOptions”方法中检查用户是否已经登录,如果是,则将ProfailViewController设置为窗口rootViewController

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let isUserLoggedIn:Bool = NSUserDefaults.standardUserDefaults().boolForKey("isUserLoggedIn")
    if(isUserLoggedIn) {
        let mainStoryboard = UIStoryboard(name: "Main" , bundle: nil)
        let protectedPage = mainStoryboard.instantiateViewControllerWithIdentifier("profail") as! ProfailViewController
        window!.rootViewController = protectedPage
        window!.makeKeyAndVisible()
    }
    return true
}