ios - 使用 Swift 崩溃的 Facebook 登录中的取消按钮?

标签 ios facebook swift parse-platform

我刚刚按照教程使用 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/

相关文章:

ios - 成功构建应用程序后出现 Xcode 存档错误

facebook - FQL 可以查询多早的时间?

ios - 为什么我们在swift中给可选值赋值时忽略感叹号?

ios - 准备Segue Swift

ios - SwiftUI View 内容布局意外弹出/跳转出现?

ios - 如何将 SFSafariViewController 与 SwiftUI 一起使用?

ios - Xcode 9.2 存档成功但无法导出 IPA

ios - 无法更新单例类中的属性

Android - Facebook SDK 3 - 仅限墙贴

javascript - 打开 'feed' 对话框时如何在没有访问 token 的情况下获取用户的名字?