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

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more