我将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])
}
实际上每当它调用openurl方法进行gmail集成时我怎样才能把条件调用为facebook整合的方法
答案 0 :(得分:3)
在您的代码中执行以下更改,它将起作用
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)
}
}