Set useragent in WKWebview

前端 未结 6 2146
野趣味
野趣味 2021-01-31 14:25

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 followi

6条回答
  •  没有蜡笔的小新
    2021-01-31 15:05

    Update:

    As of iOS 9.0 it is possible to set the user agent directly (as stated in other answers). But it is important to note that setting it will completely override the default user agent. If for some reason you need to just append a custom user agent use one of the following approaches.

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

    or by using a sacrificial UIWebView

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


    Old answer:

    As noted in my comment you can use the same approach as described here: Change User Agent in UIWebView (iPhone SDK)

    Now if you want to get the user agent you need to have an instance of a WKWebView and evaluate this javascript on it:

    navigator.userAgent
    

    The problem is that if you set a custom user agent after a WKWebView has been instantiated you will always get the same user agent. To solve this problem you have to reinstantiate the web view. Here is a sample how this might look:

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

    The code above will log:

    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
    

    Alternative

    This could be made even simpler by using a "sacrificial" UIWebView since it evaluates javascript synchronously.

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

    Which logs the same thing:

    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
    

    Right now UIWebView and WKWebView use the same user agent but this approach might cause problems if that changes in the future.

提交回复
热议问题