DEV Community

Dênis Mendes
Dênis Mendes

Posted on

Append a new User-Agent to WKWebview in iOS

Sometimes we need to pass another user-agent when opening a URL in the WkWebView, so here how to do it in iOS app:

[_wkWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
        NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"];

        NSString *userAgent = [result stringByAppendingFormat:@" %@/%@",@"Custom User Agent",version];

        [self.wkWebView setCustomUserAgent: userAgent];

        [self.wkWebView loadRequest:request];
}];
Enter fullscreen mode Exit fullscreen mode

Discussion (0)