我想知道如果用户已经登录,绕过登录 View Controller 的最佳方法是什么。我已将 Facebook 集成到我的应用程序中。我尝试检查 FBSDKAccessToken.currentAccessToken
如果它为零或不在appdelegate类中,那么应用程序可以从loginviewcontroller启动,也可以不从loginviewcontroller启动,但它不起作用。这是我到目前为止所尝试过的。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if ((FBSDKAccessToken.currentAccessToken()) != nil) {
print("it is logged in")
}else{
print("it is not ")
}
// Override point for customization after application launch.
FBSDKLoginButton.classForCoder()
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
无论用户是否登录,我都会收到相同的消息。 我究竟做错了什么?谢谢。
最佳答案
注意: 我不知道如何解决您的代码中的问题,但还有另一种方法可以做到这一点,我认为更好!
登录:
SWIFT 2:
您可以将此代码添加到在用户登录尝试成功时执行操作的函数中:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue("loggedin", forKey: "yourKey")
然后,在 AppDelegate.swift 中的 ApplicationDidFinishLaunchingWithOptions
内添加以下代码:
let defaults = NSUserDefaults.standardUserDefaults()
if defaults.valueForKey("yourKey") != nil{
let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
self.window.rootViewController = viewController!
}
注销:
在注销函数中,添加以下内容:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue("loggedout", forKey: "yourKey")
并将 didFinishLaunching...
中的 if-statement
更改为:
if defaults.valueForKey("yourKey") != nil{
let vc = UIViewController()
let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
let value = defaults.valueForKey("yourKey") as! String!
if value == "loggedin"{
vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController!
}
else if value == "loggedout"{
vc = storyboard.instantiateViewControllerWithIdentifier("loginViewController") as! UIViewController!
}
self.window.rootViewController = viewController!
}
希望这有帮助!
关于ios - 用户登录 IOS 后绕过 facebook 登录 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39210262/