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

时间:2018-01-14 23:02:12

标签: ios swift facebook facebook-ios-sdk facebook-permissions

我们需要像这样的用户,我将它们添加到FacebookAuthManager(我的自定义类),所有新用户都将看到此权限的请求。但我们也有当前的用户,因此我写了一个类,用于验证用户是否已在Facebook注册并请求了权限。我遇到了这样一个事实:我收到应用程序崩溃并收到此错误“由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:'不允许通过发布或管理权限请求读取权限。”。我在评论中指出了我称之为函数的顺序。我该如何解决这个问题?

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 个答案:

答案 0 :(得分:0)

我修好了。我将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)
    }

}