杀死应用程序后 Facebook AccessToken 为零 (iOS)
Facebook AccessToken nil after killing App (iOS)
我正在开发一个 iOS 应用程序,使用 FacebookSDK 登录。
登录杀掉App后,currentAccessToken
始终为nil。仅当我按下主页按钮并在后台 仍然 运行 时重新打开应用程序时才有效 .
我开始使用警报,因为在终止应用程序后日志无法正常工作。
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
let accessToken = FBSDKAccessToken.current()
if accessToken == nil {
Auth.auth().addStateDidChangeListener { (auth, user) in
alert(title: user?.uid ?? "1")
}
alert(title: "2")
} else {
alert(title: accessToken?.userID ?? "3")
}
}
它总是在警报中显示“1”。
这是一个类似的问题:
要么
我试过:
- 钥匙串不工作
- 无法通过 Cocoapods 降级到 4.17
- 更改传输安全性也不起作用
Help/Suggestions,
提前致谢。
AccessToken.current
不会持续存在,因此您必须手动保存访问令牌字符串并在连续启动时重新创建它。
基本上:
获取访问令牌后保存
let strAuthenticationToken = accessToken.authenticationToken
UserDefaults.standard.set(strAuthenticationToken,
forKey: "AccessToken_Facebook")
获取访问令牌字符串并在下次启动时重新创建 AccessToken
对象
if let strAuthenticationToken = UserDefaults.standard.string(forKey: "AccessToken_Facebook") {
//Create AccessToken for facebook login
let accessToken = AccessToken(authenticationToken: strAuthenticationToken)
//...Skip Login and directly proceed to get facebook profile data with the AccessToken
}
注意:为方便此示例,Facebook 访问令牌字符串被保存到 UserDefaults
,但理想情况下应将其保存到钥匙串。
示例中:
func loginWithFacebook() {
//Check for previous Access Token
if let accessToken = AccessToken.current {
//AccessToken was obtained during same session
getAccountDetails(withAccessToken: accessToken)
}
else if let strAuthenticationToken = UserDefaults.standard.string(forKey: "AccessToken_Facebook") {
//A previous access token string was saved so create the required AccessToken object
let accessToken = AccessToken(authenticationToken: strAuthenticationToken)
//Skip Login and directly proceed to get facebook profile data with an AccessToken
getAccountDetails(withAccessToken: accessToken)
}
else {
//Access Token was not available so do the normal login flow to obtain the Access Token
doFacebookLogin()
}
}
func doFacebookLogin() {
LoginManager().logIn(readPermissions: [.publicProfile, .email], viewController: nil) { loginResult in
switch loginResult {
case .failed(let error):
print(error)
case .cancelled:
print("User cancelled login.")
case .success(let grantedPermissions,
let declinedPermissions,
let accessToken):
//Save Access Token string for silent login purpose later
let strAuthenticationToken = accessToken.authenticationToken
UserDefaults.standard.set(strAuthenticationToken,
forKey: "AccessToken_Facebook")
//Proceed to get facebook profile data
self.getAccountDetails(withAccessToken: accessToken)
}
}
}
func getAccountDetails(withAccessToken accessToken: AccessToken) {
let graphRequest: GraphRequest = GraphRequest(graphPath : "me",
parameters : ["fields" : "id, name, email"],
accessToken : accessToken,
httpMethod : GraphRequestHTTPMethod.GET,
apiVersion : GraphAPIVersion.defaultVersion)
graphRequest.start { (response, result) in
switch result {
case .success(let resultResponse):
print(resultResponse)
case .failed(let error):
print(error)
}
}
}
(Swift 4 / Facebook SDK 4.30)
我正在开发一个 iOS 应用程序,使用 FacebookSDK 登录。
登录杀掉App后,currentAccessToken
始终为nil。仅当我按下主页按钮并在后台 仍然 运行 时重新打开应用程序时才有效 .
我开始使用警报,因为在终止应用程序后日志无法正常工作。
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
let accessToken = FBSDKAccessToken.current()
if accessToken == nil {
Auth.auth().addStateDidChangeListener { (auth, user) in
alert(title: user?.uid ?? "1")
}
alert(title: "2")
} else {
alert(title: accessToken?.userID ?? "3")
}
}
它总是在警报中显示“1”。
这是一个类似的问题:
我试过:
- 钥匙串不工作
- 无法通过 Cocoapods 降级到 4.17
- 更改传输安全性也不起作用
Help/Suggestions,
提前致谢。
AccessToken.current
不会持续存在,因此您必须手动保存访问令牌字符串并在连续启动时重新创建它。
基本上:
获取访问令牌后保存
let strAuthenticationToken = accessToken.authenticationToken UserDefaults.standard.set(strAuthenticationToken, forKey: "AccessToken_Facebook")
获取访问令牌字符串并在下次启动时重新创建
AccessToken
对象if let strAuthenticationToken = UserDefaults.standard.string(forKey: "AccessToken_Facebook") { //Create AccessToken for facebook login let accessToken = AccessToken(authenticationToken: strAuthenticationToken) //...Skip Login and directly proceed to get facebook profile data with the AccessToken }
注意:为方便此示例,Facebook 访问令牌字符串被保存到 UserDefaults
,但理想情况下应将其保存到钥匙串。
示例中:
func loginWithFacebook() {
//Check for previous Access Token
if let accessToken = AccessToken.current {
//AccessToken was obtained during same session
getAccountDetails(withAccessToken: accessToken)
}
else if let strAuthenticationToken = UserDefaults.standard.string(forKey: "AccessToken_Facebook") {
//A previous access token string was saved so create the required AccessToken object
let accessToken = AccessToken(authenticationToken: strAuthenticationToken)
//Skip Login and directly proceed to get facebook profile data with an AccessToken
getAccountDetails(withAccessToken: accessToken)
}
else {
//Access Token was not available so do the normal login flow to obtain the Access Token
doFacebookLogin()
}
}
func doFacebookLogin() {
LoginManager().logIn(readPermissions: [.publicProfile, .email], viewController: nil) { loginResult in
switch loginResult {
case .failed(let error):
print(error)
case .cancelled:
print("User cancelled login.")
case .success(let grantedPermissions,
let declinedPermissions,
let accessToken):
//Save Access Token string for silent login purpose later
let strAuthenticationToken = accessToken.authenticationToken
UserDefaults.standard.set(strAuthenticationToken,
forKey: "AccessToken_Facebook")
//Proceed to get facebook profile data
self.getAccountDetails(withAccessToken: accessToken)
}
}
}
func getAccountDetails(withAccessToken accessToken: AccessToken) {
let graphRequest: GraphRequest = GraphRequest(graphPath : "me",
parameters : ["fields" : "id, name, email"],
accessToken : accessToken,
httpMethod : GraphRequestHTTPMethod.GET,
apiVersion : GraphAPIVersion.defaultVersion)
graphRequest.start { (response, result) in
switch result {
case .success(let resultResponse):
print(resultResponse)
case .failed(let error):
print(error)
}
}
}
(Swift 4 / Facebook SDK 4.30)