52

如何在 WKWebView 中设置自定义用户代理字符串?我正在尝试嵌入我的应用程序版本,以便我的服务器端可以看到可用的功能。我找到了以下方法:

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

但这意味着仅针对该单个请求设置用户代理。第一个其他请求(例如转发)将意味着用户代理再次重置为默认值。如何更永久地配置 wkwebview 以使用我的自定义用户代理字符串?

4

6 回答 6

93

你会很高兴听到WKWebView刚刚customUserAgentiOS 9OSX 10.11

例子:

wkWebView.customUserAgent = "your agent" 
于 2015-07-17T19:44:46.703 回答
35

更新:

从 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"


老答案:

如我的评论中所述,您可以使用与此处所述相同的方法:Change User Agent in 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);
    }];
}];

上面的代码将记录:

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

其中记录了同样的事情:

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 使用相同的用户代理,但是如果将来发生变化,这种方法可能会导致问题。

于 2014-11-21T09:27:29.993 回答
22

自定义用户代理

要设置自定义用户代理,您可以使用customUserAgent属性:

let webConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.customUserAgent = "ExampleApp/1.0 (iPhone)"

可用:iOS 9+

附加到默认用户代理

要在默认用户代理的和附加自定义字符串,您可以使用applicationNameForUserAgent属性:

let webConfiguration = WKWebViewConfiguration()
webConfiguration.applicationNameForUserAgent = "ExampleApp/1.0 (iPhone)"
let webView = WKWebView(frame: .zero, configuration: webConfiguration)

然后它将看起来像:

Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7
(KHTML, like Gecko) ExampleApp/1.0 (iPhone)
                    ^^^^^^^^^^^^^^^^^^^^^^^

可用:iOS 9+

于 2018-06-18T13:02:54.547 回答
2

WKWebView斯威夫特 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 目前不支持WKWebView在 Storyboards(Xcode 版本 8.3.2)中使用,因此您必须在代码中手动添加 Web 视图。

UIWebView斯威夫特 3 示例:

UserDefaults.standard.register(defaults: ["UserAgent": userAgentValue])
于 2017-05-31T18:23:10.477 回答
1

WKWebView 中的默认用户代理为

Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X)

您可以自定义 WKWebView 用户代理

webView.customUserAgent = "zgpeace User-Agent"

我为 WKWebView 写了一个演示:

func requestWebViewAgent() {
        print("requestWebViewAgent")

        let webView = WKWebView()
        webView.evaluateJavaScript("navigator.userAgent") { (userAgent, error) in
            if let ua = userAgent {
                print("default WebView User-Agent > \(ua)")
            }

            // customize User-Agent
            webView.customUserAgent = "zgpeace User-Agent"
        }
    }

警告:当 webView 发布时,webView 中的“User-Agent”为零。您可以将 webView 对象设置为属性以保留 webView。

NSURLSession 默认发送 User-Agent
默认的 User-Agent 样式。

"User-Agent" = "UserAgentDemo/1 CFNetwork/1121.2.1 Darwin/19.2.0";

我们可以自定义 User-Agent。

let config = URLSessionConfiguration.default
config.httpAdditionalHeaders = ["User-Agent": "zgpeace User-Agent"]

我在下面为 URLSession 编写了一个演示。

     func requestUrlSessionAgent() {
        print("requestUrlSessionAgent")

        let config = URLSessionConfiguration.default
        // default User-Agent: "User-Agent" = "UserAgentDemo/1 CFNetwork/1121.2.1 Darwin/19.2.0";
        // custom User-Agent
        config.httpAdditionalHeaders = ["User-Agent": "zgpeace User-Agent"]
        let session = URLSession(configuration: config)

        let url = URL(string: "https://httpbin.org/anything")!
        var request = URLRequest(url: url)
        request.httpMethod = "GET"

        let task = session.dataTask(with: url) { data, response, error in

            // ensure there is no error for this HTTP response
            guard error == nil else {
                print ("error: \(error!)")
                return
            }

            // ensure there is data returned from this HTTP response
            guard let content = data else {
                print("No data")
                return
            }

            // serialise the data / NSData object into Dictionary [String : Any]
            guard let json = (try? JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [String: Any] else {
                print("Not containing JSON")
                return
            }

            print("gotten json response dictionary is \n \(json)")
            // update UI using the response here
        }

        // execute the HTTP request
        task.resume()

    }

NSURLConnection 默认发送 User-Agent
默认的 User-Agent 样式。

"User-Agent" = "UserAgentDemo/1 CFNetwork/1121.2.1 Darwin/19.2.0";

我们可以自定义 User-Agent。

urlRequest.setValue("URLConnection zgpeace User-Agent", forHTTPHeaderField: "User-Agent")

我在下面为 URLConnection 编写了一个演示。

func requestUrlConnectionUserAgent() {
    print("requestUrlConnectionUserAgent")

    let url = URL(string: "https://httpbin.org/anything")!
    var urlRequest = URLRequest(url: url)
    urlRequest.httpMethod = "GET"
    // default User-Agent: "User-Agent" = "UserAgentDemo/1 CFNetwork/1121.2.1 Darwin/19.2.0";
    urlRequest.setValue("URLConnection zgpeace User-Agent", forHTTPHeaderField: "User-Agent")

    NSURLConnection.sendAsynchronousRequest(urlRequest, queue: OperationQueue.main) { (response, data, error) in
        // ensure there is no error for this HTTP response
        guard error == nil else {
            print ("error: \(error!)")
            return
        }

        // ensure there is data returned from this HTTP response
        guard let content = data else {
            print("No data")
            return
        }

        // serialise the data / NSData object into Dictionary [String : Any]
        guard let json = (try? JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [String: Any] else {
            print("Not containing JSON")
            return
        }

        print("gotten json response dictionary is \n \(json)")
        // update UI using the response here
    }

  }

github中的演示:
https ://github.com/zgpeace/UserAgentDemo.git

于 2020-03-31T01:09:52.110 回答
0

在我的 swift 3 案例中,我需要使用自定义 userAgent 的整个应用程序,这是我在 AppDelegate 中的解决方案。这里使用 UIWebview 是因为我不需要设置WKWebViewConfiguration,因为我只需要 userAgent 字符串

 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)
}
于 2017-06-21T10:36:09.160 回答