登录Facebook时获取空值以获取用户电子邮件ID swift 3

时间:2016-08-14 01:11:29

标签: ios facebook swift3

我试图通过允许用户通过Facebook登录来获取用户的信息。我试图获取信息,但它返回null值。我做错了吗?我使用我创建的自定义登录按钮。下面是我目前的代码,我得到“没有信息”。

@IBAction func fbLogin(_ sender: AnyObject) {

     let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) -> Void in
        if (error == nil){
            let fbloginresult : FBSDKLoginManagerLoginResult = result!
            if fbloginresult.grantedPermissions != nil {
            if(fbloginresult.grantedPermissions.contains("email"))
            {
                self.getFBUserData()
            }
            }else {
                print("No information available")     
            }

        }
    }

}

下面是我对fb的Appdelegate.swift代码

class AppDelegate:UIResponder,UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    }

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {

    return FBSDKApplicationDelegate.sharedInstance().application(application, open: url as URL!, sourceApplication: sourceApplication, annotation: annotation)

}

func applicationDidBecomeActive(_ application: UIApplication) {
    FBSDKAppEvents.activateApp()
}

}

1 个答案:

答案 0 :(得分:0)

我终于能够让它运转起来了。我不得不替换以下功能

func应用程序(应用程序:UIApplication,openURL url:NSURL,sourceApplication:String?,annotation:AnyObject?) - >布尔{

返回FBSDKApplicationDelegate.sharedInstance()。application(application,open:url as URL!,sourceApplication:sourceApplication,annotation:annotation)

}

func application(_ app: UIApplication, open url: URL, options: [String : AnyObject] = [:]) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(app,open: url,sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String,annotation: options [UIApplicationOpenURLOptionsAnnotationKey])
}

一旦我改变它,我得到以下错误 -

可选(错误域= com.facebook.sdk.login代码= 308“(null)”)

看起来钥匙串和苹果的问题已经改变了ios10的工作方式。我提到了链接 - https://stackoverflow.com/a/38799196/6652247并且它有效。我现在能够获得用户信息。

感谢Hoang Tran ..