设置WebView的UserAgent

1、对于osx,设置WebView的UserAgent的方法

    WebView *webview = [[WebView alloc]initWithFrame:NSZeroRect];
    NSString *olderUserAgent = [webview stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSString *newUserAgent = @"Appended Custom User Agent";
    NSString *userAgent = [NSString stringWithFormat:@"%@ %@",olderUserAgent,newUserAgent];
    [webview setCustomUserAgent:userAgent];

2、对于osx,设置WKWebView的UserAgent的方法

[webView setValue:userAgent forKey:@"customUserAgent"];//对于osx10.11以及以上使用该函数
[webView setValue:userAgent forKey:@"applicationNameForUserAgent"];//对于osx10.10以及以下使用该函数,该函数是私有函数

3、对于ios,设置WebView或WKWebView的UserAgent的方法

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

 

posted @ 2017-09-06 19:37  燃烧吧,少年  阅读(1816)  评论(0编辑  收藏  举报