简体   繁体   中英

Keep same session FBSDKLoginKit, FBSDKShareKit 4.36

i'm using FBSDKLoginKit and FBSDKShareKit for login and after share an FBSDKShareOpenGraphContent.

I've a similar behaviour in android and it works fine.

In swift i'm able to log-in success in Facebook but when i call FBSDKShareDialog.show(from: self, with: content, delegate: nil) to share my FBSDKShareOpenGraphContent from a detail viewcontroller, app SHOWS LOGIN FACEBOOK PAGE AGAIN in browser! I expected that FBSDKShareOpenGraphContent was shared directly without need to login again!

Why this behaviour if FBSDKAccessToken.currentAccessTokenIsActive() return true? How to keep same session between different viewcontroller? I read a billion of posts about it bot those solutions doesn't solve my issue

Be carefull that if I type user and password and re-login for second time, the FBSDKShareOpenGraphContent is shared correctly! But is a bad user experience and i want user can login just 1 time!

Please HELP Here is my code:

//APPDELEGATE

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
                // Override point for customization after application launch.

                FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
                          return true
            }

            private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
                // Override point for customization after application launch.
                return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
            }

            private func application(application: UIApplication, openURL url: URL, sourceApplication: String?, annotation: Any?) -> Bool {
                return FBSDKApplicationDelegate.sharedInstance().application(
                    application,
                    open: url as URL?,
                    sourceApplication: sourceApplication,
                    annotation: annotation)
            }
    func applicationDidBecomeActive(_ application: UIApplication) {
            // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
            FBSDKAppEvents.activateApp()
        }

    ----

// LOGIN VIEWCONTROLLER @IBAction

    @IBAction func fbLoginButtonTapped(_ sender: UIButton) {
            let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
            fbLoginManager.logIn(withReadPermissions: ["public_profile"], from: self) { (result, error) -> Void in
                if (error == nil) {

                    let fbloginresult : FBSDKLoginManagerLoginResult = result!
                    // if user cancel the login
                    if (result?.isCancelled)! {
                        print("LOGIN FB CANCELLED")
                        return
                    }
                    if(fbloginresult.grantedPermissions.contains("public_profile")) {
                        print("LOGIN FB SUCCESS")
                        /* DO MY STUFF */   
                    }
                } else {
                    print("LOGIN FB ERROR")
                }
            }
        }

   //Page Detail ViewController with share button @IBAction

     @IBAction func facebookButtonTapped(_ sender: UIButton) {
                if (FBSDKAccessToken.currentAccessTokenIsActive() == true) {
//THIS RETURNS TRUE AS USER IS ALWAYS LOGGED

                    let properties = ["og:type": "article",
                                      "og:title": "\(self.post?.title ?? "")",
                        "og:image": "\(self.post?.image ?? "")",
                        "og:description": "\(self.post?.content ?? "")"]

                    let object: FBSDKShareOpenGraphObject = FBSDKShareOpenGraphObject(properties: properties)

                    let action: FBSDKShareOpenGraphAction = FBSDKShareOpenGraphAction(type: "news.reads", object: object, key: "article")
                    let content: FBSDKShareOpenGraphContent = FBSDKShareOpenGraphContent()
                    content.action = action
                    content.previewPropertyName = "article"
                    FBSDKShareDialog.show(from: self, with: content, delegate: nil)
                }
        }

Info.plist for Facebook integrations

 <!-- FBSDK INTEGRATION -->
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>fb************</string>
            </array>
        </dict>
    </array>
    <key>FacebookAppID</key>
    <string>**************</string>
    <key>FacebookDisplayName</key>
    <string>*********</string>

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>fbapi</string>
        <string>fb-messenger-api</string>
        <string>fbauth2</string>
        <string>fbshareextension</string>
    </array>

First Login page shown after login button tapped:

Login page again on share button tapped, after first login success

I solved this issue with the following code:

 let shareDialog = FBSDKShareDialog()
 shareDialog.delegate = self
shareDialog.shareContent = content

if (UIApplication.shared.canOpenURL( URL(string: "fbauth2:/")!) ) {
    shareDialog.mode = .native
} else {
    shareDialog.mode = .web
}

shareDialog.show()

previously i used

shareDialog.mode = .auto

but if this isn't able to choose the appropriated things to do or it fails for some reason, it seems that re-instantiates a web browser without keeping the active session.

With modes .native and .web i can handle cases of native application installed or not, keeping active session.

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