如何修复由不允许的用户代理导致的功能受损?

How can I fix impaired functionality caused by disallowed useragent?

我正在构建网络浏览器,最近我在尝试使用网站上的 Google 帐户登录时遇到错误。

这很奇怪,因为我检查了我的应用程序和 Safari 的用户代理,它们都是相同的。

有什么建议吗?

更新

WKWebView 嵌套在自定义 UIView 树中的 3 层深处。

初始化代码如下:

_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];

来自GoogleModernizing OAuth interactions in Native Apps for Better Usability and Security文章:

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

因此,Google 现在仅允许在普通浏览器中使用 Google 帐户登录,出于安全原因,它在网络视图中限制了它,并推荐 Google SDK iOS and Android 用于此目的。

但如果你仍然想使用 WKWebView 你可以做一些小技巧,在 this 答案中建议,设置 customUserAgent 以便它可以通过验证:

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