Ad

Facebook Permission Request Crashes IOS App

We need a user like it, I added them to FacebookAuthManager (my custom class), and all new users will see the request for this permission. But we also have current users, so I wrote a class that verifies that the user was registered with Facebook and requested permission. I'm faced with the fact that I get the app crash and get this error "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Read permissions are not permitted to be requested with publish or manage permissions.". I indicated in the comments in which sequence I call the functions. How can I fix this?

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

}
Ad

Answer

I fixed it.I changed loginManager.logIn(withPublishPermissions:) to 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)
    }

}
Ad
source: stackoverflow.com
Ad