Facebook 权限请求崩溃 iOS 应用

Facebook permission request crashes iOS app

我们需要这样的用户,我将他们添加到 FacebookAuthManager(我的自定义 class),所有新用户都会看到此权限的请求。但我们也有当前用户,所以我写了一个 class 来验证用户是否已在 Facebook 注册并请求许可。我面临着应用程序崩溃并收到此错误 "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Read permissions are not permitted to be requested with publish or manage permissions." 的事实。我在评论中指出了调用函数的顺序。我该如何解决这个问题?

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)
    }

}

我修复了 it.I 将 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)
    }

}