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)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay