Facebook 权限请求使 iOS 应用程序崩溃

Posted

技术标签:

【中文标题】Facebook 权限请求使 iOS 应用程序崩溃【英文标题】:Facebook permission request crashes iOS app 【发布时间】:2018-01-14 23:02:11 【问题描述】:

我们需要一个喜欢的用户,我将他们添加到 FacebookAuthManager(我的自定义类)中,所有新用户都会看到对此权限的请求。但是我们也有当前用户,所以我编写了一个类来验证用户是否已在 Facebook 注册并请求许可。我面临这样一个事实,即应用程序崩溃并出现此错误“由于未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:'不允许使用发布或管理权限请求读取权限。强>”。我在 cmets 中指出了我调用函数的顺序。我该如何解决这个问题?

class FacebookPermissionManager 

    /// Request new permission if user's provider is Facebook
    // first
    open func requestNewPersmission(_ permission: FacebookInfo.Permession, completion: ((_ isGranted: Bool, _ error: Error?) -> Void)?) 
        DispatchQueue.global(qos: .background).async 
            guard let currentUser = Auth.auth().currentUser else  return 

            let facebookProvider = "facebook.com"

            var isFacebookProvider = false

            for providerInfo in currentUser.providerData 
                if facebookProvider == providerInfo.providerID 
                    isFacebookProvider = true
                    break
                
            
            guard isFacebookProvider else 
                completion?(false, nil)
                return
            
            guard !self.checkGrant(permission) else 
                debugPrint("checkGrant == true")
                completion?(true, nil)
                return
            
            debugPrint("isFacebookProvider", isFacebookProvider)
            self.requestPermission(permission, completion: completion)
        
    

    // the third
    open func requestPermission(_ permission: FacebookInfo.Permession, completion: ((_ isGranted: Bool, _ error: Error?) -> Void)?) 
        DispatchQueue.main.async 
            guard let topVC = UIApplication.topViewController() else  return 
            let loginManager = FBSDKLoginManager()
            loginManager.logIn(withPublishPermissions: [permission.rawValue], from: topVC, handler:  (result, error) in
                if let _error = error 
                    let file = #file
                    let funcName = #function
                    BugfenderManager.reportError(funcName, fileName: file, error: _error)
                    completion?(false, _error)
                    return
                
                guard let _result = result else 
                    completion?(false, nil)
                    return
                
                if _result.grantedPermissions.contains(permission.rawValue)  
                    completion?(true, nil)
                 else if _result.declinedPermissions.contains(permission.rawValue) 
                    // TODO: do not request permissions again immediately. Consider providing a NUX
                    // describing  why the app want this permission.
                    completion?(false, nil)
                
            )
        
    

    // the second
    private func checkGrant(_ permission: FacebookInfo.Permession) -> Bool 
        guard let accessToken = FBSDKAccessToken.current() else  return false 
        return accessToken.hasGranted(permission.rawValue)
    


【问题讨论】:

【参考方案1】:

我修好了。我把loginManager.logIn(withPublishPermissions:)改成了loginManager.logIn(withPublishPermissions:)

class FacebookPermissionManager 

    /// Request new permission if user's provider is Facebook
    // first
    open func requestNewPersmission(_ permission: FacebookInfo.Permession, completion: ((_ isGranted: Bool, _ error: Error?) -> Void)?) 
        DispatchQueue.global(qos: .background).async 
            guard let currentUser = Auth.auth().currentUser else  return 

            let facebookProvider = "facebook.com"

            var isFacebookProvider = false

            for providerInfo in currentUser.providerData 
                if facebookProvider == providerInfo.providerID 
                    isFacebookProvider = true
                    break
                
            
            guard isFacebookProvider else 
                completion?(false, nil)
                return
            
            guard !self.checkGrant(permission) else 
                debugPrint("checkGrant == true")
                completion?(true, nil)
                return
            
            debugPrint("isFacebookProvider", isFacebookProvider)
            self.requestReadPermission(permission, completion: completion)
        
    

    // the third
    open func requestReadPermission(_ permission: FacebookInfo.Permession, completion: ((_ isGranted: Bool, _ error: Error?) -> Void)?) 
        DispatchQueue.main.async 
            guard let topVC = UIApplication.topViewController() else  return 
            let loginManager = FBSDKLoginManager()
            loginManager.logIn(withReadPermissions: [permission.rawValue], from: topVC, handler:  (result, error) in
                if let _error = error 
                    let file = #file
                    let funcName = #function
                    BugfenderManager.reportError(funcName, fileName: file, error: _error)
                    completion?(false, _error)
                    return
                
                guard let _result = result else 
                    completion?(false, nil)
                    return
                
                if _result.grantedPermissions.contains(permission.rawValue)  
                    completion?(true, nil)
                 else if _result.declinedPermissions.contains(permission.rawValue) 
                    // TODO: do not request permissions again immediately. Consider providing a NUX
                    // describing  why the app want this permission.
                    completion?(false, nil)
                
            )
        
    

    // the second
    private func checkGrant(_ permission: FacebookInfo.Permession) -> Bool 
        guard let accessToken = FBSDKAccessToken.current() else  return false 
        return accessToken.hasGranted(permission.rawValue)
    


【讨论】:

以上是关于Facebook 权限请求使 iOS 应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

iOS上的Facebook登录使我的应用程序崩溃

如果用户取消登录,Swift ios facebook登录会使应用程序崩溃

React Native Facebook 登录按钮在 iOS 模拟器中使应用程序崩溃

Facebook iOS SDK 3.5.1 好友请求对话框在关闭时崩溃

如何在 iOS 中请求 Facebook XMPP 聊天权限?

读取权限后直接请求发布权限 - 适用于 iOS 6 的 Facebook SDK