WKWebView set customUserAgent does nothing on iOS beta5

VERSION

iOS 12 beta5 (16A5339e)

ISSUE

Set customUserAgent on WKWebView not work on iOS 12 beta5 (16A5339e). While it will modify UserAgent successfully on iOS beta4 (16A5288q).

Sample

- (WKWebView *)webView {
if (!_webView) {
_webView = ({
WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
WKWebView* webView = [[WKWebView alloc] initWithFrame:self.view.bounds
configuration:configuration];
[self.view addSubview:webView];
webView;
});
}
return _webView;
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
NSString* originUserAgent = result;
NSString* newUserAgent = [NSString stringWithFormat:@"%@ %@", originUserAgent, @"NOTHING APPENDED ON BETA5"];
self.webView.customUserAgent = newUserAgent;
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google.com"]]];
}];
}

Replies

I can verify the same problem on iOS 12 beta 9, in detail:

- set customUserAgent the FIRST time on any WkWebView is not working

- set customUserAgent any time after the FIRST time it's working properly

In case someone else stumbles across this: I had the same issue. Set userAgent via UserDefaults, on WKWebView and on the request itself. Nothing worked on the first request, but all subsequent requests where fine. Turns out that the first request resulted in a redirect (https://www.foo.com => https://foo.com) and the custom userAgent was not sent. Once code was updated to go directly to the site without www it works as expected.