Naresh S Naresh S - 3 months ago 73
iOS Question

Getting null value while logging to Facebook to get users email ID swift 3

i was trying to get user's information by allowing users to login via facebook. I tried to get the information but instead it returns null value. Am i doing anything wrong ? i use a custom login button that i created. Below is my current code and i get "No information available".

@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")
}

}
}

}


Below is my Appdelegate.swift code for fb

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()
}


}

Answer

I was finally able to get it working. I had to replace the below function

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)

}

with

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])
}

Once i changed it, i got the below error -

Optional(Error Domain=com.facebook.sdk.login Code=308 "(null)")

It looks like an issue with keychain and apple has changed the way of working in ios10. I referred the link - http://stackoverflow.com/a/38799196/6652247 and it worked. I was able to get the user information now.

Thanks Hoang Tran..

Comments