尝试使用FBSDK快速注销Facebook时iOS应用程序崩溃

时间:2019-02-11 15:42:30

标签: ios swift facebook swift4 fbsdk

我无法在iOS应用程序中使用FBSDK。登录似乎可以正常工作,但注销立即使应用程序崩溃。

我正在使用Swift 4和FBSDK 4.4版。我怀疑我在网上找到的文档以及我在Google上搜索的其他解决方案没有使用完全相同的版本,因为没有发现对我有用。这是我的应用程序委托代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    }

func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        let handled: Bool = FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: options[.sourceApplication] as? String, annotation: options[.annotation])
        return handled
    }

这是我的注销代码:

@IBAction func btnFbLogoutClicked(_ sender: UIButton) {
        let loginManager : FBSDKLoginManager = FBSDKLoginManager()
        loginManager.logOut()
        self.performSegue(withIdentifier: "UnwindToLogin", sender: self)
    }

它崩溃并关闭logOut()上的应用程序,并且不执行segue。如果有人使用相同版本的FBSDK和Swift,我将感谢一些提示。

1 个答案:

答案 0 :(得分:-1)

您应该在Appdelegate中添加缺少的方法,并按照我的按钮操作代码进行操作。它为我工作。希望它将为您服务。

  import UIKit
  import FBSDKCoreKit

  @UIApplicationMain
  class AppDelegate: UIResponder, UIApplicationDelegate {
     var window: UIWindow?
     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    func applicationWillResignActive(application: UIApplication) {    
        FBSDKAppEvents.activateApp()
    }
    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
     }
  }

 //MARK: FacebookLogin Button
 @IBAction func btnFacebookLoginClicked(sender: AnyObject) {
    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.logOut()
    fbLoginManager.loginBehavior = FBSDKLoginBehavior.Web //Without safari Browser
    fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self, handler: { (result, error) -> Void in
        if (error != nil){
            let fbloginresult : FBSDKLoginManagerLoginResult = result
            if(fbloginresult.grantedPermissions.contains("email")){
                self.getFBUserData()
                fbLoginManager.logOut()
            }
        }else if result.isCancelled {
        print("Cancelled")
    }
    })
}