robertbabington robertbabington - 1 month ago 225
Swift Question

Facebook SDK sign in with Swift 3 iOS 10

I installed Facebook's 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 have followed the login instructions from Facebook's 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 sign in via Facebook and then the web view goes blank and nothing happens. The app shows in my Facebook apps, so some authorisation has taken place, but if I click the "Done" button in the web view, the result to the callback is
.cancelled
and "User cancelled login." is printed.

I have added Keychain Sharing to my entitlements, and I've 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've even changed my
NSAppTransportSecurity
settings.

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


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

Several errors are printed also:

[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 app installed on my device, I get:

-canOpenURL: failed for URL: "fbauth2:/" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"

Answer

You have to substitute didfinishlaunching and openurl AppDelegate methods with 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)
}
Comments