首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >保持相同的会话FBSDKLoginKit,FBSDKShareKit 4.36

保持相同的会话FBSDKLoginKit,FBSDKShareKit 4.36
EN

Stack Overflow用户
提问于 2018-09-15 10:39:16
回答 1查看 153关注 0票数 2

我使用FBSDKLoginKit和FBSDKShareKit登录和共享FBSDKShareOpenGraphContent之后。

我在android中也有类似的行为,而且效果很好。

我可以在Facebook上快速登录,但当我调用FBSDKShareDialog.show(从: self,with: content,委托: nil)从一个详细视图控制器共享我的FBSDKShareOpenGraphContent时,应用程序再次在浏览器中显示登录FACEBOOK页面!我原以为FBSDKShareOpenGraphContent是直接共享的,而不需要再次登录!

如果FBSDKAccessToken.currentAccessTokenIsActive()返回true,为什么会出现这种行为?如何在不同的视图控制器之间保持相同的会话?我读了很多关于它的帖子,这些解决方案并不能解决我的问题

请注意,如果我第二次输入用户和密码并重新登录,FBSDKShareOpenGraphContent将被正确共享!但是是一个糟糕的用户体验,我希望用户只需登录1次!

请帮助这里是我的密码:

代码语言:javascript
复制
//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

代码语言:javascript
复制
    @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")
                }
            }
        }
代码语言:javascript
复制
   //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

代码语言:javascript
复制
 <!-- 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>

登录按钮点击后显示的第一个登录页面:

再次登录页面,在第一次登录成功后,单击“共享”按钮。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-15 11:06:47

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

代码语言:javascript
复制
 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()

以前我用过

代码语言:javascript
复制
shareDialog.mode = .auto

但是,如果这不能选择要做的适当的事情,或者由于某种原因而失败,那么似乎重新实例化了一个web浏览器,而没有保持活动会话。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52343898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档