我正在使用 FBSDKLoginKit 和 FBSDKShareKit 进行登录,然后共享一个 FBSDKShareOpenGraphContent。
我在android中有类似的行为,它工作正常。
在 swift 我能够在 Facebook 成功登录但是当我调用 FBSDKShareDialog.show(from: self, with: content, delegate: nil) 从详细视图控制器共享我的 FBSDKShareOpenGraphContent 时,应用程序在浏览器中再次显示登录 FACEBOOK PAGE !我希望 FBSDKShareOpenGraphContent 直接共享,无需再次登录!
如果 FBSDKAccessToken.currentAccessTokenIsActive() 返回 true,为什么会有这种行为?如何在不同的视图控制器之间保持相同的会话?我阅读了十亿篇关于它的帖子,这些解决方案并不能解决我的问题
请注意,如果我第二次输入用户名和密码并重新登录,则 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()
}
----
// 登录视图控制器@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>