简体   繁体   中英

SWIFT - FBSDKLoginManager error when using FB App

I'm using the FBSDKLoginManager to receive a token for later usage (FB posting) and end up in an endless loop after confirming the access.

When calling the FBSDKLoginManager() , the following popup appears (Sorry, it's in German language... This is the official Facebook popup, where the user can select whether he wants to logon manually or using the FB App).

Facebook弹出屏幕

Now the following error occurs:

  • If I'm using the second button (logon via phone number or E-Mail-Address), everything works fine. The function returns with a token and I can go on in my App.

  • The ERROR: If I'm using the first button (logon with Facebook-App), the Facebook App opens, I can set all the privacy settings in FB, and confirm. After confirming the access, the FB-App closes automatically and returns to the same popup-screen without any change. No action occurs after coming back to this screen...

I don't have any foggy idea where the problem is... There is no error message. Due to the fact that everything works fine when using the E-Mail Login, the problem must be in the return of the FB-App. The E-Mail Login works via Safari, in the error case there is the break to the FB-App.

let login: FBSDKLoginManager = FBSDKLoginManager()
login.logIn(withPublishPermissions: ["publish_actions"], from: self) { (result, error) in
if (error != nil) {
    print("publish_actions: \(error!)")
} else if (result?.isCancelled)! {
    print("publish_actions: Canceled")
} else if (result?.grantedPermissions.contains("publish_actions"))! {
    //print("publish_actions: permissions granted: \(String(describing: result?.token.tokenString))")

    UserDefaults.standard.set(result?.token.tokenString, forKey: "facebook_token")
}

Added Frameworks: Bolts / CoreKit / LoginKit

Development Environment: Xcode 9.2 / iPhone 6s with iOS 11.2 / latest Facebook App installed.

I solved by adding the follow missing call to FB SDK:

[[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation];

inside

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation method of the AppDelegate.

Check for following in your info plist file

<key>FacebookAppID</key>
<string>fb_id_here</string> // in format 234234234
<key>FacebookDisplayName</key>
<string>display_name_here</string>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>

</array>
<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fbfb_id_here </string>. // in format fb234234234
        </array>
    </dict>
</array>

now inyour appdelegate

in

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

func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {

    if let scheme = url.scheme {

        if scheme == "fb 234234234" { //the one you kept in your info plist file
            //TODO: replace with real value
            return SDKApplicationDelegate.shared.application(application, open: url, options: options)
        }

    }

    return true

}

Do configure these and then dub your app, if the control folw comes in appdelegate function

func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool

if it doesnt you have not configured facebook schemas correctly in info plist file

also

let fbManager = FBSDKLoginManager()
fbManager.logOut()

    let readPermissions: [ReadPermission] = [.publicProfile, .email]
    let sdkPermissions = readPermissions.map({ $0.permissionValue.name })

    fbManager.logIn(withReadPermissions: sdkPermissions, from: nil) { (result, error) in
        if let token = result?.token {
            // your token here
        }
        else {
        }
    }

Thank you very much. This solved the problem.

1.) The plist file was already set up. I think otherwise the login with Safari wouldn't have worked.

2.) The App Delegate was the reason!!! Small adjustment:

SDKApplicationDelegate.shared didn't work. I should be as followed:

FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

(As well in the added "application-open URL"-function.)

But now it works. Thanks a lot.

我想也许我在xcode中发现了一个问题,AppDelegate的func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool ,xcode将此功能修复为private ,我将此功能固定为internal ,它将起作用〜

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM