gpt4 book ai didi

ios - 检查用户是否登录 ios swift

转载 作者:搜寻专家 更新时间:2023-10-31 08:08:09 25 4
gpt4 key购买 nike

如果用户登录时我想检查 appdelegate.swift 文件,显示像 Instagram 这样的 HomeviewController,如果没有显示 LoginViewController,我会使用 mysql 来保存用户数据和 php bridge lang

appdalegate.swift 中的代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

var rootViewController = self.window!.rootViewController
let isUserLoggedIn:Bool = NSUserDefaults.standardUserDefaults().boolForKey("isUserLoggedIn")
if(isUserLoggedIn) {
let mainStoryboard = UIStoryboard(name: "Main" , bundle: nil)
let protectedPage = mainStoryboard.instantiateViewControllerWithIdentifier("goToHome") as! HomeViewController
window!.rootViewController = protectedPage
window!.makeKeyAndVisible()
}
else{
let mainStoryboard = UIStoryboard(name: "Main" , bundle: nil)
let loginViewController = mainStoryboard.instantiateViewControllerWithIdentifier("loginview") as! LoginViewController
window!.rootViewController = loginViewController
window!.makeKeyAndVisible()


}


return true
}

登录 View Controller .swift

let success:NSInteger = jsonData.valueForKey("success") as! NSInteger

//[jsonData[@"success"] integerValue];

NSLog("Success: %ld", success);

if(success == 1)
{
NSLog("Login SUCCESS");

let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
prefs.setObject(username, forKey: "USERNAME")
prefs.setInteger(1, forKey: "ISLOGGEDIN")
prefs.synchronize()


self.performSegueWithIdentifier("goToHome", sender: self)

ProfileViewController.swift

 override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)


let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
let isLoggedIn:Int = prefs.integerForKey("ISLOGGEDIN") as Int


if (isLoggedIn != 1) {

self.performSegueWithIdentifier("goto_login", sender: self)

}

else
{

self.usernameLabel.text = prefs.valueForKey("USERNAME") as? String

}

}

对不起,问题很长

最佳答案

不要在这种情况下使用 NSUserDefaults

https://github.com/matthewpalmer/Locksmith

这是使用安全登录/注销的好例子所以,在实现之后

if let dictionary = Locksmith.loadDataForUserAccount("accaunt")
{
//go to profile
}
else
{
//go to login
}

关于ios - 检查用户是否登录 ios swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37920021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com