How to get UIWebView User-Agent

后端 未结 2 451
孤街浪徒
孤街浪徒 2020-12-08 05:16

I\'ve got a problem working with one remote server. My app makes a request to a server using [NSData initWithContentsOfURL:] method and as a response I get webs

相关标签:
2条回答
  • 2020-12-08 05:48

    As @nate mentioned above, you can invoke Javascript in a webview:

    UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    

    But invoking new Javascript is a bit hacky and creating a zero-size webView is gratuitous, since you already have a webView you're dealing with.

    Alternatively you can simply use native methods on your given webView:

    NSString* ua = [webView.request valueForHTTPHeaderField:@"User-Agent"];
    NSLog(@"User-Agent = %@", ua);
    
    0 讨论(0)
  • 2020-12-08 05:51

    I just ran into a similar issue and needed to make the user agent sent by an NSURLConnection match the one sent by a UIWebView. My solution was to create a UIWebView and then just use javascript to pull out the user agent.

    UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];

    0 讨论(0)
提交回复
热议问题