保持相同的会话FBSDKLoginKit,FBSDKShareKit 4.36

时间:2018-09-15 10:39:16

标签: ios swift login fbsdkloginkit fbsdksharedialog

我正在使用FBSDKLoginKit和FBSDKShareKit进行登录,并共享了FBSDKShareOpenGraphContent之后。

我在android中有类似的行为,并且工作正常。

我能够迅速在Facebook中成功登录,但是当我调用FBSDKShareDialog.show(来自:self,with:content,委托:nil)时,可以从详细视图控制器,应用程序中共享我的FBSDKShareOpenGraphContent,以显示登录脸书页面再次在浏览器中! 我希望直接共享FBSDKShareOpenGraphContent,而无需再次登录!

如果FBSDKAccessToken.currentAccessTokenIsActive()返回true,为什么会出现这种情况? 如何在不同的ViewController之间保持相同的会话? 我读了十亿篇有关该机器人无法解决我问题的文章。

请注意,如果我键入用户名和密码并第二次重新登录,则FBSDKShareOpenGraphContent将正确共享! 但是这是糟糕的用户体验,我希望用户只能登录1次!

请帮助 这是我的代码:

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

    ----

//登录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)
                }
        }

用于Facebook集成的Info.plist

 <!-- 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

1 个答案:

答案 0 :(得分:1)

我用以下代码解决了这个问题:

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

以前我用过

shareDialog.mode = .auto

但是,如果这无法选择适当的操作或由于某种原因而失败,则似乎重新实例化了Web浏览器,而没有保持活动会话。

使用.native和.web模式,我可以处理是否安装本机应用程序的情况,并保持活动会话状态。