user2977578 user2977578 - 1 month ago 17
iOS Question

Facebook Login Issue with WebView not Closing using (Swift 3.0 FacebookLogin 0.2.0)

The pods I'm using from my podfile:

PODS:

- Bolts (1.8.4):

- Bolts/AppLinks (= 1.8.4)

- Bolts/Tasks (= 1.8.4)

- Bolts/AppLinks (1.8.4):

- Bolts/Tasks

- Bolts/Tasks (1.8.4)

- FacebookCore (0.2.0):

- Bolts (~> 1.8)

- FBSDKCoreKit (~> 4.15)

- FacebookLogin (0.2.0):

- Bolts (~> 1.8)

- FacebookCore (~> 0.2)

- FBSDKCoreKit (~> 4.15)

- FBSDKLoginKit (~> 4.15)

- FBSDKCoreKit (4.16.0):

- Bolts (~> 1.7)

- FBSDKLoginKit (4.16.1):

- FBSDKCoreKit

My LoginViewController.swift code:

import FacebookLogin
class LoginViewController: UIViewController, LoginButtonDelegate {
/*!
@abstract Sent to the delegate when the button was used to login.
@param loginButton the sender
@param result The results of the login
@param error The error (if any) from the login
*/


override func viewDidLoad() {
super.viewDidLoad()
let loginButton = LoginButton(readPermissions: [ .publicProfile ])
loginButton.center = view.center
loginButton.delegate = self;
view.addSubview(loginButton)

}

/**
Called when the button was used to login and the process finished.

- parameter loginButton: Button that was used to login.
- parameter result: The result of the login.
*/
public func loginButtonDidCompleteLogin(_ loginButton: FacebookLogin.LoginButton, result: FacebookLogin.LoginResult){
print("hello!")
}

/**
Called when the button was used to logout.

- parameter loginButton: Button that was used to logout.
*/
public func loginButtonDidLogOut(_ loginButton: FacebookLogin.LoginButton){
print("goodbye")
}


My modified AppDelegate.swift Code:

public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
// Override point for customization after application launch.
return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
}

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


Context:

I downloaded everything with pods, and was prompted by xcode to convert all code to swift 3.0 so I did so. I followed this guide for getting started and logging in: https://developers.facebook.com/docs/swift
If I run without the second function in my app delegate, I am forced to press "done" from the web view that pops up after logging in, then I get a response "hello" and if I set a breakpoint at that point I see that my login was perceived as canceled. In addition, the facebook button I initialized still prompts me to log in so it definitely did not work. In addition I also get this error:


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


HOWEVER, if I leave that method in, it closes but the delegate never gets called. Also the button proceeds to prompt me to log in.

My Info.plist:

<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fbMYID</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>MYID</string>
<key>FacebookDisplayName</key>
<string>MYAPPNAME</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>


Extra Information:

I'm new to swift but I can tell that I need the method I took out to allow it to work but am unsure of how to approach this. I don't have anything called FBSDKApplicationDelegate (viewed from other questions similar that I've browsed) since I'm using the new swift SDK from here: https://developers.facebook.com/docs/swift. I'm guessing that doc for login is outdated given the current version of swift or I missed a link pointing to some pre-requisite. Please let me know if you have any idea of the solution to my issue. My info.plist should be all set or else I would have errors attempting to log in. I am able to reach the screen where it says You have already granted MYAPP permission. Then I proceed to press ok, then if I had the openURL method in my app delegate it closes, or it stays blank. I STRONGLY SUSPECT it has to do with the options that I left with a blank dict from the open method when calling the SDKApplicationDelegate. Thanks

Answer

So the problem was with my keychain sharing. As soon as I went to mytarget > capabilities > Keychain sharing and then enabled it, it worked.