Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the WKWebview's user agent in OS X Yosemite?

How do you change the user agent used by WKWebview?

With the older WebView, I could write the following to change the user agent:

[myWebView setCustomUserAgent:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4)
 AppleWebKit/537.77.4 (KHTML,like Gecko) Version/7.0.5 Safari/537.77.4"];
like image 332
Jim Bak Avatar asked Oct 17 '14 22:10

Jim Bak


People also ask

What is IOS WKWebView?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.

How do I create a WKWebView app for Macos?

Step 1 - Install XCode 11 and open it. Step 2 - Select "File" and "New" and "Project" from the main menu. Step 3 - Select "App" (top left) and Select "Next" (bottom right). Step 4 - Give your app a name, for User Interface select "Storyboard" and then select "Next".

How do I migrate to WKWebView?

You can implement WKWebView in Objective-C, here is simple example to initiate a WKWebView : WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init]; WKWebView *webView = [[WKWebView alloc] initWithFrame:self. view. frame configuration:theConfiguration]; webView.


4 Answers

Very simple in Swift. Just place the following into your App DelegatedidFinishLaunchingWithOptions.

NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent" : "Custom Agent"])

If you want to append to the existing agent string then:

let userAgent = UIWebView().stringByEvaluatingJavaScriptFromString("navigator.userAgent")! + " Custom Agent"
NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent" : userAgent])

Note: You will need to uninstall and reinstall the App to avoid appending to the existing agent string.

like image 122
PassKit Avatar answered Oct 03 '22 00:10

PassKit


I don't have an answer for this. However, some pointers from my research so far:

In iOS, it's possible to set a custom user agent for a UIWebView like this:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:agent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

In OSX, there was a setCustomUserAgent method for WebView elements that did the trick.

However, this doesn't work for WKWebView (at least, in OSX). I couldn't find any documentation about it from Apple, either.

Hope somebody can help!

like image 35
micho Avatar answered Oct 03 '22 01:10

micho


I ran into the same issue, but managed to work around it using a combination of loadHTMLString on the WKWebView and a NSMutableURLRequest to do the heavy lifting.

My search on how to call some method on the WKWebView itself lead me to http://trac.webkit.org/changeset/165594, which implies there is a private method _setCustomUserAgent to do this. I'm not proficient enough in cocoa/swift to figure this one out.

I ended up using the code below, as I really only need to fetch the contents of a single URL and display it, but it may be helpful in some way. What it does is simply loading the contents of an URL into the WKWebView as string, I suspect you may lose back/forward navigation and such, and it will only work for the initial page display, as the WKWebView will take over clicks and asset loading.

(please note, this example is written in Swift and not Objective-C)

self.webView = WKWebView(frame: webViewRect, configuration: webViewConfig)

//  create the request
let url = NSURL(string: "https://example.com/")
let request = NSMutableURLRequest(URL: url!)
request.setValue("YourUserAgent/1.0", forHTTPHeaderField: "User-Agent")

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
    let content = NSString(data: data, encoding: NSUTF8StringEncoding)
    self.webView!.loadHTMLString(content!, baseURL: url)
}
like image 35
Rogier Spieker Avatar answered Oct 03 '22 01:10

Rogier Spieker


It wouldn't be ideal but you could likely implement a custom NSURLProtocol handler to intercept HTTP requests and modify them with your custom user-agent header. I don't think this would work on iOS since WKWebView makes requests out-of-process and bypasses any registered NSURLProtocols. But it might work on OS X?

like image 29
TomSwift Avatar answered Oct 02 '22 23:10

TomSwift