0

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

}

Naresh S
  • 17
  • 4
  • There seems to be no problem in the code you provided. Can you make sure that your `Facebook app` on Facebook developers website is correctly configured? The `Bundle ID` should match for it to work. – Hoang Tran Aug 15 '16 at 14:25
  • Hi Hoang, yes. I checked that. the problem is, it opens up a safari view requesting for permissions and once i click OK, it does nothing and stays in safari view. When i click Done, it thinks that i have cancelled it and returns null value. Do you know why it wouldnt redirect to the app. I am using xcode 8 beta 5 . i even tried the fix in this link - http://stackoverflow.com/questions/37903652/facebook-login-not-going-back-to-app-when-migrated-to-swift-3?rq=1 but it didnt work. – Naresh S Aug 15 '16 at 15:19
  • Can you share your `Info.plist` file? You can put it on GitHub Gist so I can see it. – Hoang Tran Aug 15 '16 at 15:22

1 Answers1

0

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 - https://stackoverflow.com/a/38799196/6652247 and it worked. I was able to get the user information now.

Thanks Hoang Tran..

Community
  • 1
  • 1
Naresh S
  • 17
  • 4