Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set useragent in WKWebview

How do I set a custom useragent string in a WKWebView? I'm trying to embed the version of my app so that my server-side can see what features are available. I found the following method:

let userAgent = "MyApp/1.33.7" request.setValue(userAgent, forHTTPHeaderField: "User-Agent")  NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in     let content = NSString(data: data, encoding: NSUTF8StringEncoding)     self.web!.loadHTMLString(content!, baseURL: url) } self.web!.loadRequest(request); 

But this means the useragent is only set for that single request. The first other request (e.g. a forward), will mean the useragent is reset to default again. How can I more permanently configure the wkwebview to use my custom useragent string?

like image 622
Pvel Avatar asked Nov 18 '14 12:11

Pvel


1 Answers

You'll be happy to hear that WKWebView just gained a customUserAgent property in iOS 9 and OSX 10.11

Example:

wkWebView.customUserAgent = "your agent"  
like image 130
Era Avatar answered Sep 16 '22 23:09

Era