Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Login button does not work within WKWebView

We are developing an application for iOS 8.0 and above. This application requires authentication (or that the user registers for it). On the website, we use html forms to register and login users. These forms are responsive. User can also click on the "Log me in with Facebook button" to log in with FB credentials. This button is implemented with FB's SDK for Javascript (v2.4)

As such, we do not want to implement native screen on our iOS application for registration and log, but we rather implemented a view controller with a WKWebView element to handle this browsing experience.

However, we have noted that, when users tap on the "Log me in with Facebook button", nothing happens. The typical pop-up that FB opens to ask a user for login and grant permissions never appears.

Here is how we are initializing the WKWebView:

class LoginViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
var webView: WKWebView
@IBOutlet weak var cancelButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(webView)
    webView.setTranslatesAutoresizingMaskIntoConstraints(false)
    let height = NSLayoutConstraint(item: webView, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 1, constant: 0)
    let width = NSLayoutConstraint(item: webView, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 1, constant: 0)
    view.addConstraints([height, width])

    loadDefaultUrl()
}

required init(coder aDecoder: NSCoder){
    let config = WKWebViewConfiguration()
    config.preferences.javaScriptCanOpenWindowsAutomatically = true
    self.webView = WKWebView(frame: CGRectZero, configuration: config)
    super.init(coder: aDecoder)
    webView.navigationDelegate = self
    webView.UIDelegate = self
}

Would appreciate any pointers on how to make the FB login button work within a WKWebView (or, if doable, within a UIWebView).

like image 491
Luis Delgado Avatar asked Aug 13 '15 12:08

Luis Delgado


People also ask

What does UIWebView mean on Facebook?

Read more here – (http://www.80vul.com/android/android-0days.txt) iOS. In the simple case, a UIWebView is just a web browser tab sharing its cookie storage with the app. However, more complex interactions are often needed between the embedded JavaScript and the app.

What is a WKWebView?

Overview. A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.

Is it possible to login to the WebView app using Facebook?

The website has a Sign in with Facebookoption, this works fine in a mobile browser and on the website itself. Whenever I try to use the WebViewapp to login using Facebook, it results in a white screen. I am having difficulties finding a solution to get this to work.

Why doesn't the login with Facebook button create an account?

If your "login with facebook" button does not create an account or log in customer to your shop - this means that there is something wrong with facebook application (usually). To verify and fix the problem please follow suggestions below. How to check if problem is related to Facebook APP ID?

Why can't I load a URL on my Facebook app?

Can't Load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and subdomains of your app to the App Domains field in your app settings. These errors are stricly related to facebook application settings.

What to do if you can’t log in to Facebook?

On the other hand, if you can’t log in to Facebook using the stand-alone app on your phone, try updating the app. Clearing the cache may also help. As a last resort, you can uninstall and reinstall the app. Recover Your Account Go to facebook.com/login/identify. Enter the email or phone number you used when you first registered with Facebook.


2 Answers

A little late to answer, but I ran across the same problem lately and couldn't find any satisfying answer.

I have a solution but with iOS 9.0 by implementing two methods of WKUIDelegate, which you already registered yourself as for your webView. The authentication flow with the Facebook JS library will first open a popup window allowing the user to input its credentials, allow the information transmission, etc. and then the window will close returning to the parent window.

In order to do allow that flow, you first need to intercept the call to open the popup with the following method:

func webView(webView: WKWebView, createWebViewWithConfiguration configuration: WKWebViewConfiguration, forNavigationAction navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
    // A nil targetFrame means a new window (from Apple's doc)
    if (navigationAction.targetFrame == nil) {
        // Let's create a new webview on the fly with the provided configuration, 
        // set us as the UI delegate and return the handle to the parent webview
        let popup = WKWebView(frame: self.view.frame, configuration: configuration)
        popup.UIDelegate = self
        self.view.addSubview(popup)
        return popup
    }
    return nil;
}

Now that this is done, the new request will automatically be loaded in the "popup" WKWebView that was just created. The next step is to make sure to close it once the flow is done. Since we registered self as the popup.UIDelegate, we can remove the view when it will be closed by the external authentication flow:

func webViewDidClose(webView: WKWebView) {
    // Popup window is closed, we remove it
    webView.removeFromSuperview()
}

I hope this helps.

like image 153
bernyfox Avatar answered Sep 24 '22 02:09

bernyfox


This is caused by WKWebView blocking links with target=_blank. The solution is to capture that situation and instead load the link directly in the view like this>

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
    if navigationAction.targetFrame?.isMainFrame == nil {
        webView.load(navigationAction.request)
    }
    return nil
}

Solution derived from here Why is WKWebView not opening links with target="_blank"

like image 31
Josef Richter Avatar answered Sep 24 '22 02:09

Josef Richter