在WKWebview中设置useragent

如何在WKWebView中设置一个自定义的useragentstring? 我试图embedded我的应用程序的版本,以便我的服务器端可以看到什么function可用。 我发现了以下方法:

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); 

但是这意味着useragent只为这个单一的请求设置。 第一个其他请求(例如转发),将意味着useragent被重新设置为默认值。 我如何更永久地configurationwkwebview使用我的自定义useragentstring?

您将很高兴听到customUserAgentiOS 9OSX 10.11获得了一个customUserAgent属性

例:

 wkWebView.customUserAgent = "your agent" 

更新:

从iOS 9.0开始,可以直接设置用户代理(如其他答案中所述)。 但重要的是要注意,设置它将完全覆盖默认的用户代理。 如果出于某种原因,您只需附加一个自定义用户代理,请使用以下方法之一。

 webView.evaluateJavaScript("navigator.userAgent") { [weak webView] (result, error) in if let webView = webView, let userAgent = result as? String { webView.customUserAgent = userAgent + "/Custom Agent" } } 

或通过使用牺牲的UIWebView

 webView.customUserAgent = (UIWebView().stringByEvaluatingJavaScript(from: "navigator.userAgent") ?? "") + "/Custom agent" 

老答案:

正如在我的评论中指出的,您可以使用与此处所述相同的方法: 在UIWebView(iPhone SDK)中更改用户代理

现在,如果你想获得用户代理,你需要有一个WKWebView的实例,并评估这个JavaScript:

 navigator.userAgent 

问题是,如果您在实例化WKWebView之后设置自定义用户代理,您将始终获得相同的用户代理。 要解决这个问题,你必须重新实例化Web视图。 这里是一个示例如何看起来:

 self.wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds]; __weak typeof(self) weakSelf = self; [self.wkWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) { __strong typeof(weakSelf) strongSelf = weakSelf; NSString *userAgent = result; NSString *newUserAgent = [userAgent stringByAppendingString:@" Appended Custom User Agent"]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; strongSelf.wkWebView = [[WKWebView alloc] initWithFrame:strongSelf.view.bounds]; // After this point the web view will use a custom appended user agent [strongSelf.wkWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) { NSLog(@"%@", result); }]; }]; 

上面的代码将logging:

 Mozilla/5.0 (iPhone; CPU iPhone OS 8_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B411 Appended Custom User Agent 

替代

这可以通过使用“牺牲”UIWebView更简单,因为它同步评估的JavaScript。

 UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]; NSString *newUserAgent = [userAgent stringByAppendingString:@" Appended Custom User Agent"]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; self.wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds]; [self.wkWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) { NSLog(@"%@", result); }]; 

其中logging相同的东西:

 Mozilla/5.0 (iPhone; CPU iPhone OS 8_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B411 Appended Custom User Agent 

现在,UIWebView和WKWebView使用相同的用户代理,但如果将来发生更改,则此方法可能会导致问题。

WKWebView Swift 3例子:

 let userAgentValue = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4" webView.customUserAgent = userAgentValue 

注意那些试图使用Storyboard或Interface Builder来做这件事的人:不幸的是,Xcode目前不支持在Storyboard(Xcode版本8.3.2)中使用WKWebView ,所以你必须在代码中手动添加Web视图。

UIWebView Swift 3的例子:

 UserDefaults.standard.register(defaults: ["UserAgent": userAgentValue]) 

在我的swift 3的情况下,我需要整个应用程序使用自定义的userAgent,这是我在AppDelegate的解决scheme。 这里使用UIWebview是因为我不需要设置WKWebViewConfiguration ,因为我只需要userAgentstring

  fileprivate func setupGlobalWebviewUserAgent() { let webview = UIWebView() var newUserAgent = webview.stringByEvaluatingJavaScript(from: "navigator.userAgent") newUserAgent = newUserAgent?.appending("custom user agent") let dictionary = Dictionary(dictionaryLiteral: ("UserAgent", newUserAgent)) UserDefaults.standard.register(defaults: dictionary) }