I’m using facebook sign in api (SDK version Optional("4.1.0")
) in Swift
in my ios application. I placed the fb login button on my view Controller and when user clicks it – it opens facebook login panel in safari. When user puts the credentials and clicks log in – he sees the popup message with a question:
Open this page in APP_NAME?
Cancel OPEN
I want to get rid of that and open the app directly.
My code that handles the behavior of the button is like this:
class ViewController: UIViewController, FBSDKLoginButtonDelegate {
@IBOutlet weak var fbLoginButton: FBSDKLoginButton!
override func viewDidLoad() {
super.viewDidLoad()
fbLoginButton.readPermissions = ["public_profile", "email"]
fbLoginButton.layer.cornerRadius = 5
fbLoginButton.delegate = self
self.fbLoginButton.delegate = self
FBSDKProfile.enableUpdates(onAccessTokenChange: true)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if (FBSDKAccessToken.current() != nil){
let tokenAsAParam = [
"access_token":FBSDKAccessToken.current().tokenString!
]
let loginManager = FBSDKLoginManager()
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
let data:[String:AnyObject] = result as! [String : AnyObject]
let defaults = UserDefaults.standard
let firstName:String = data["first_name"] as! String!
defaults.set(firstName, forKey: "facebook_display_name")
defaults.synchronize()
}else {
print("fb error")
}
})
Alamofire.request("(serverURL)/auth/facebook", method: .post, parameters: tokenAsAParam)
.validate()
.responseJSON { response in
switch(response.result) {
case .success:
self.performSegue(withIdentifier: "openMainApp", sender: self)
break
case .failure:
print(response.result.error)
loginManager.logOut()
break
}
}
}
In the code above I’m fetching user’s data from facebook and sends the token to my webservice to validate there whether the user is real or not and do some backend stuff.
My info.plist file is:
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>fbXXXXXXXXXX</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>2XXXXXXXXXXX</string>
<key>FacebookDisplayName</key>
<string>xxxxxxxx</string>
<key>LSApplicationCategoryType</key>
<string></string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
Also, in my appDelegate I have the following methods:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
print("SDK version (FBSDKSettings .sdkVersion())")
...
return true
}
func application(_ application: UIApplication,
open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}
// [END openurl]
@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String?, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
yet it still show this weird poppup instead of opening the app normally.
Does anyone know what I could do to fix this problem?
3
Answers
This is one of the better way to integration of FB for native iOS device, you get your solution from here :
http://www.theappguruz.com/blog/facebook-integration-using-swift
Also check the App Setting from the developer FB account
I guess if might be happening on iOS 9. You hace to use safariViewController to deal with this.
I guess this link might help you.
Get your Facebook login ready for iOS9 with SFSafariViewController
You can change it form Build Setting -> product name change it to your app display name.
By default it take target name.