【发布时间】:2015-05-20 10:22:28
【问题描述】:
我在使用适用于 iOS 的新 FacebookSDK 时遇到问题。
我可以成功登录并取回必要的信息,但是当我再次打开我的应用并打算跳过登录过程时,我正在验证的会话为空。
这是我的 AppDelegate:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.rootViewController = LogInViewController()
self.window!.makeKeyAndVisible()
FBSDKProfile.enableUpdatesOnAccessTokenChange(true)
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
func applicationDidBecomeActive(application: UIApplication) {
FBSDKAppEvents.activateApp()
}
}
这是我的 Facebook 登录视图控制器:
class LogInViewController: UIViewController, FBSDKLoginButtonDelegate {
// MARK: Properties
@IBOutlet weak var logInView: FBSDKLoginButton!
// MARK: Init
init() { super.init(nibName: "LogInViewController", bundle: nil) }
required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
// MARK: View Delegates
override func viewDidLoad() {
super.viewDidLoad()
if (FBSDKAccessToken.currentAccessToken() != nil) {
// User is already logged in, do work such as go to next view controller.
println(FBSDKAccessToken.currentAccessToken().tokenString)
self.goToNavigation()
} else {
logInView.readPermissions = ["public_profile", "email", "user_friends"]
logInView.delegate = self
}
}
// MARK: Facebook Delegate Methods
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
println("User Logged In")
if ((error) != nil) {
// Process error
} else if result.isCancelled {
// Handle cancellations
} else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if result.grantedPermissions.contains("email") {
// Do work
returnUserData()
}
}
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
println("User Logged Out")
}
func returnUserData() {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil) {
// Process error
println("Error: \(error)")
} else {
println("fetched user: \(result)")
let userName : NSString = result.valueForKey("name") as! NSString
println("User Name is: \(userName)")
let userEmail : NSString = result.valueForKey("email") as! NSString
println("User Email is: \(userEmail)")
}
self.goToNavigation()
})
}
func goToNavigation() {
// Creating a navigation controller with MainMenuViewController at the root of the navigation stack.
let navController = UINavigationController(rootViewController: MainMenuViewController())
self.presentViewController(navController, animated:true, completion: nil)
}
}
当我检查if (FBSDKAccessToken.currentAccessToken() != nil) 时,会话总是为空,尽管按钮标题是“注销”。
这可能是什么问题? 欢迎提出任何建议!
【问题讨论】:
-
用问题和答案检查这篇文章 - stackoverflow.com/questions/30205396/…
-
你也可以检查一下 - stackoverflow.com/questions/30042905/…
标签: ios facebook swift session facebook-ios-sdk