我在我的应用程序中集成了facebook。但是在登录FBSDKAccessToken.currentAccessToken()之后,找到了nil。
这是我的代码:
override func viewDidLoad() {
if (FBSDKAccessToken.currentAccessToken() != nil)
{
print("Not loggedin...")
}
else
{
print("logged in...")
}
loginbutton.delegate = self
loginbutton.readPermissions = ["public_profile", "email", "user_friends"]
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
if(error == nil)
{
print("login complete")
print(result)
print(result.grantedPermissions)
}
else{
print(error.localizedDescription)
}
if let userToken = result.token
{
let token:FBSDKAccessToken = result.token
print("TOKEN = \(FBSDKAccessToken.currentAccessToken().tokenString)")
print("User ID = \(FBSDKAccessToken.currentAccessToken().userID)")
}
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"name, email"])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else
{
print("fetched user: \(result)")
let userEmail : NSString = result.valueForKey("email") as! NSString
print("User Email is: \(userEmail)")
let userName : NSString = result.valueForKey("name") as! NSString
print("User Name is: \(userName)")
}
})
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
print("User Logged Out")
}
登录后,我得到一个空白屏幕。它会打印登录成功消息,但不会显示以下内容:
if let userToken = result.token
{
let token:FBSDKAccessToken = result.token
print("TOKEN = \(FBSDKAccessToken.currentAccessToken().tokenString)")
print("User ID = \(FBSDKAccessToken.currentAccessToken().userID)")
}
所以,我没有得到任何象征。当它去打印电子邮件时,它显示了这个错误:
这是我在appdelegate中的代码
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
{
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
func applicationDidBecomeActive(application: UIApplication) {
FBSDKAppEvents.activateApp()
}
func application(application: UIApplication,
openURL url: NSURL, options options: [String: AnyObject]) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey]! as! String,
annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}
实际上每次它调用gmail集成的openurl方法,我怎么能把条件放在调用facebook集成的方法上呢
最佳答案
在代码中进行以下更改,它将起作用
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
println("User Logged In")
if ((error) != nil)
{
// Process error
}
else if result.isCancelled {
// Handle cancellations
}
else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if result.grantedPermissions.contains("email")
{
// Do work
self.returnUserData()
}
}
}
调用此方法获取用户电子邮件id
func returnUserData()
{
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters:["fields":"name,email,first_name,last_name"])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
println("fetched user: \(result)")
let userName : NSString = result.valueForKey("name") as! NSString
println("User Name is: \(userName)")
let userEmail : NSString = result.valueForKey("email") as! NSString
println("User Email is: \(userEmail)")
}
})
}
请删除此方法
func application(application: UIApplication,
openURL url: NSURL, options options: [String: AnyObject]) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey]! as! String,
annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}
更改此方法实现
func application(application: UIApplication,
openURL url: NSURL,
sourceApplication: String?,
annotation: AnyObject?) -> Bool {
if url.scheme == "fbxxxxx"
{
//xxxxx is your APP ID
return FBSDKApplicationDelegate.sharedInstance().application(
application,
openURL: url,
sourceApplication: sourceApplication,
annotation: annotation)
}else if url.scheme == "com.googleusercontent.apps.xxxxxx"
{
// string is reverse client ID
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: sourceApplication,
annotation: annotation)
}
}
关于ios - 每次发现FBSDKAccessToken.currentAccessToken()为零,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35311454/