我刚刚按照教程使用 swift 和 parse 制作了一个 Facebook 登录按钮。我按下登录按钮,第一次成功登录。之后,当我再次运行该应用程序并再次按下登录按钮时,我会看到下图所示的屏幕: login page
当我按下“确定”时,一切正常,然后转到下一个 viewContoller,问题是在我按下“取消”按钮后,应用程序崩溃了。您能否给我一些提示,说明为什么会发生这种情况?
import UIKit
import Parse
import ParseFacebookUtilsV4
import ParseTwitterUtils
var userName: String = ""
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@available(iOS 8.0, *)
@IBAction func signInButtonTapped(sender: AnyObject) {
PFFacebookUtils.logInInBackgroundWithReadPermissions([], block: { (user: PFUser?, error: NSError?) -> Void in
if (error != nil)
{
//Display an alert message
var myAlert = UIAlertController(title: "Alert", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction);
self.presentViewController(myAlert, animated: true, completion: nil)
return
}
if(FBSDKAccessToken.currentAccessToken() != nil) {
userName = (PFUser.currentUser()?.objectId)!
let protectedPage = self.storyboard?.instantiateViewControllerWithIdentifier("ProtectedPageViewController") as! ProtectedPageViewController
let protectedPageNav = UINavigationController(rootViewController: protectedPage)
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var window = UIApplication.sharedApplication().keyWindow
window?.rootViewController = protectedPageNav
}
})
}
}
最佳答案
在 PFFFacebookUtils.logInInBackgroundWithReadPermissions block 中仍然存在的确定或取消期间,当您点击它时没有问题,因为它调用
if (error != nil)
{ }
当您点击“取消”时,下面的方法可能会被调用,
if(FBSDKAccessToken.currentAccessToken() != nil) { }
因为token已经有效,请检查一下!
关于ios - 使用 Swift 崩溃的 Facebook 登录中的取消按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34409300/