Facebook SDK logs in with Swift 3 iOS 10

I installed the Facebook Swift SDK via Cocoapods:

pod 'FacebookCore', :git => "https://github.com/facebook/facebook-sdk-swift" pod 'FacebookLogin', :git => "https://github.com/facebook/facebook-sdk-swift" 

I followed the login instructions from the Facebook Swift SDK ( https://developers.facebook.com/docs/swift/login ), but cannot get it working.

Inside my view controller, I have the following:

 @objc fileprivate func facebookSignIn() { let loginManager = LoginManager() print("LOGIN MANAGER: \(loginManager)") loginManager.logIn([ .publicProfile, .email ], viewController: self) { loginResult in print("LOGIN RESULT! \(loginResult)") switch loginResult { case .failed(let error): print("FACEBOOK LOGIN FAILED: \(error)") case .cancelled: print("User cancelled login.") case .success(let grantedPermissions, let declinedPermissions, let accessToken): print("Logged in!") print("GRANTED PERMISSIONS: \(grantedPermissions)") print("DECLINED PERMISSIONS: \(declinedPermissions)") print("ACCESS TOKEN \(accessToken)") } } } 

"Login Manager: etc." prints when i click the button and the web view opens. I can log in via Facebook and then the web view becomes blank and nothing happens. The application is displayed in my Facebook applications, so some authorizations have occurred, but if I click the Finish button in the web view, the result of the callback will be .cancelled and "User’s canceled login". is printed.

I added key separation to my rights, and I updated my info.plist:

 <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string> [ FB STRING ] </string> </array> </dict> </array> <key>FacebookAppID</key> <string>[ APP ID ]</string> <key>FacebookDisplayName</key> <string>[ APP NAME ]</string> <key>LSApplicationQueriesSchemes</key> <array> <string>fbapi</string> <string>fb-messenger-api</string> <string>fbauth2</string> <string>fbshareextension</string> </array> 

I even changed the settings of NSAppTransportSecurity .

 <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> 

I know this is a duplicate of iOS 9 Facebook login simulator -canOpenURL: failed URL: "fbauth2: ///" - error: "(null)" and How to use the SDK for iOS for iOS 10 , but none of the solutions seem to be doesn't work for me if I missed something? Any suggestions are welcome.

Several errors are also printed:

 [App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction [error] error: (6922) I/O error for database at /var/mobile/Containers/Data/Application/F3D8E126-B0CC-4C06-81A5-D7A7F849B3BD/Documents/OfflineModel2.sqlite. SQLite error code:6922, 'disk I/O error' 

And when I don't have the FB application installed on my device, I get:

 -canOpenURL: failed for URL: "fbauth2:/" - error: "The operation couldn't be completed. (OSStatus error -10814.)" 
+8
ios login facebook swift swift3
source share
8 answers

You should replace the dofinishlaunching and openurl AppDelegate methods below:

 public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions) } public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { return SDKApplicationDelegate.shared.application(app, open: url, options: options) } 
+17
source share

Swift 3 and Swift 4

add this to your AppDelegate.swift

 import FBSDKCoreKit import FBSDKLoginKit func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) } func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options) } 
+14
source share

FBSDK libraries are deprecated. Newer libraries can be found on the Facebook Github repository .

For this specific error, implement the following application methods.

 func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { return SDKApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) } func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { return SDKApplicationDelegate.shared.application(app, open: url, options: options) } 
+7
source share

Swift 3.0

on your AppDelete.swift:

 import FBSDKCoreKit import FBSDKLoginKit 

inside your:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

add:

 FBSDKApplicationDelegate .sharedInstance() .application(application, didFinishLaunchingWithOptions: launchOptions) 

then create a new function as follows:

 func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options) } 
+5
source share

Using Cocoapods in Swift 3.0

Install cocoapods

 pod 'FacebookCore' pod 'FacebookLogin' 

in AppDelegate.swift

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

and callback cancellation method

 func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { return FBSDKApplicationDelegate.sharedInstance().application(application, open: url as URL!, sourceApplication: sourceApplication, annotation: annotation)return FBSDKApplicationDelegate.sharedInstance().application(application, open: url as URL!, sourceApplication: sourceApplication, annotation: annotation) } 

in your viewController

 import FacebookCore import FacebookLogin import FBSDKLoginKit 

after that write down this code in your fblogin button

 @IBAction func faceBookLoginBtnAction(_ sender: Any) { let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager() fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in if (error == nil){ let fbloginresult : FBSDKLoginManagerLoginResult = result! if fbloginresult.grantedPermissions != nil { if(fbloginresult.grantedPermissions.contains("email")) { if((FBSDKAccessToken.current()) != nil){ FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in if (error == nil){ self.dict = result as! [String : AnyObject] print(result!) print(self.dict) } }) } } } } } } 

and another important thing is add your facebookid to your project. In the "Project> Goal> Information> URL Types" section, set the correct Facebook ID and URL scheme.

+2
source share

updated for quick 3:

// In the AppDelegate () application:

//Step 1:

 import FacebookCore 

// Step - 2: In didFinishLaunchingWithOptions ()

  // FB Settings SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions) 

// Instance Method ():

 func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { print("URL AbsoluteString = \(url.absoluteString)") if url.absoluteString.contains("fb1279300242197274") { return SDKApplicationDelegate.shared.application(app, open: url, options: options) } else { //return PDKClient.sharedInstance().handleCallbackURL(url) print("Error while open URL from app delegate facebook sign in") } return true } 
0
source share

The answers here recommend using the following implementation of application(_,didFinishLaunchingWithOptions:) :

 public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions) } 

However, note that the official documentation does not use the return value

 SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions) 

as the return value of application(_,didFinishLaunchingWithOptions:) .

Now, when this may seem insignificant, I had big problems integrating the SDK into facebook with a deep link to the branch, because it is this line that causes me problems. Therefore, if you want to prevent some unnecessary problems later, use the following solution:

 public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions) // always return true return true } public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { return SDKApplicationDelegate.shared.application(app, open: url, options: options) } 
0
source share

First of all, you need to create an application to use Facebook login in your application. You can create a facebook application from here

You need to set the URL types , FacebookAppID , FacebookDisplayName and LSApplicationQueriesSchemes in LSApplicationQueriesSchemes .

  • Manually add the Facebook framework or you can use cocoa pods as follows:

      pod 'FacebookCore' //latest one pod 'FacebookLogin' // for fb login pod 'FacebookShare' //for sharing on fb 

    and then pod install to install packages for your application.

  • import FacebookCore in the application deletion to use Facebook login

  • Configure the following settings in the AppDelegate.swift file:

     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions) } func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { if url.absoluteString.contains(<Your FBAppID>) { return SDKApplicationDelegate.shared.application(app, open: url, options: options) } else { debugPrint("Error in openURL from AppDelegate Facebook Sign in") } return true } 

And now you have successfully set up Facebook login in your application.

In your view controller, you need to do the following:

 import FacebookLoginKit 

In your IBAction method, do the following:

 @IBAction func btnFacebookSignInTapped(_ sender: UIButton) { let fbManager = FBSDKLoginManager() fbManager.logIn(withReadPermissions: ["public_profile", "email"], from: self) { (result, error) in if let loginResult = result, error == nil { if let permissions = loginResult.grantedPermissions { if permissions.contains("email") || permissions.contains("public_profile") { if FBSDKAccessToken.current() != nil { FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: { (connection, graphResult, error) in if error == nil { debugPrint("Result: \(graphResult!)") if let data = graphResult as? [String: Any] { debugPrint("User details from FB: \(data)") } } }) } else { debugPrint("Invalid FB access token"); } } } else { self.showAlert(msg: "Fail to login using facebook.") } } else { self.showAlert(msg: "Error in facebook login") } } } 

This way you can log in to Facebook.

0
source share

All Articles