Search code examples
iosgoogle-oauthuser-agentwkwebview

How can I fix impaired functionality caused by disallowed useragent?


I'm building a web browser an recently I came across an error while trying to login using my Google account on a website.

Screenshot

This is strange because I checked the user agent of my app and Safari's and they are both identical.

Any suggestions?

UPDATE

The WKWebView is nested 3 levels deep inside a tree of custom UIViews.

Here's the initialization code:

_webView = [[WKWebView alloc] init];
_webView.allowsBackForwardNavigationGestures = NO;
_webView.allowsLinkPreview = NO;
_webView.navigationDelegate = self;
_webView.UIDelegate = self;
_webView.frame = CGRectMake(0.0, 0.0, self.contentView.frame.size.width, self.contentView.frame.size.height);
[self.contentView addSubview:_webView];

Solution

  • From Google Modernizing OAuth interactions in Native Apps for Better Usability and Security article:

    On April 20, 2017, we will start blocking OAuth requests using web-views for all OAuth clients on platforms where viable alternatives exist.

    So, Google now allows sign-in with Google Account only in normal browsers, it restricts it in web-views due to security reasons and recommends Google SDKs for iOS and Android for this purpose.

    But if you still want to use WKWebView you may do a little trick, suggested in this answer, setting customUserAgent so it can pass validation:

    // Check for selector availability, as it is available only on iOS 9+
    if ([_webView respondsToSelector:@selector(setCustomUserAgent:)]) {
        _webView.customUserAgent = @"MyCustomUserAgent";
    }