2016-06-28 53 views
0

這裏是我的代碼:的iOS UIWebView中設置的UserAgent通過追加字符串不行

- (void)viewDidLoad { 
[super viewDidLoad]; 
NSURL *url = [NSURL URLWithString:urlString]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
// NSString *tagString = @"Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13F69/YoyaOrg-iOS"; 
// NSString *tag = @"YoyaOrg-iOS"; 
NSString *defaultUserAgent = [self.mainWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]; 
NSString *newUserAgent = [NSString stringWithFormat:@"%@ YoyaOrg-iOS",defaultUserAgent]; 
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil]; 
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; 
self.mainWebView.scrollView.bounces = NO; 
[self.mainWebView loadRequest:request]; 
} 
NSLog(@"webUserAgent:%@",defaultUserAgent); 

追加字符串後「YoyaOrg-IOS」,但日誌輸出仍然不包含「YoyaOrg-IOS」。如果我取代的UserAgent作爲:

NSString *tagString = @"Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13F69/YoyaOrg-iOS"; 
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:tagString, @"UserAgent", nil]; 
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; 

然後我得到的userAgent是我想要的;我不能做到,並浪費了很多時間; TKS的任何答案。 TKS任何幫助!

回答

0

我也在尋找一種方法來改變用戶代理。我知道我應該在使用代碼[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":customUserAgent}];等實例化webview時更改用戶代理,並使用代碼NSLog(@"UserAgent = %@", [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]);在方法webViewDidFinishLoad:中打印用戶代理以查看用戶代理是否已更改。

後來我發現,如果我把代碼[webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"] web視圖之前加載URL請求,這樣

 NSString *userAgent = [_webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]; 
    NSString *mystring = @" mystring"; 
    NSString *customUserAgent = [userAgent stringByAppendingString:mystring]; 

    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":customUserAgent}]; 

的結果不僅是用戶代理是不變的,但也是我用寫的OC-JS交互代碼JavaScriptCore已被禁用!

我找到了解決這個問題的方法:init第二個UIWebView什麼也不做,只是爲了得到默認的用戶代理。 這裏是我的最終解決方案追加的默認用戶代理字符串:

_webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; 
    _webView.scalesPageToFit = YES; 

    //init another webView to get the default user agent 
    NSString *userAgent = [[[UIWebView alloc] init] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]; 
    NSString *mystring = @" mystring"; 
    NSString *customUserAgent = [userAgent stringByAppendingString:mystring]; 

    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":customUserAgent}]; 

這個工程進展順利:)

1

只是爲了把後綴到您的用戶代理做的didFinishLaunchingWithOptions功能以下在AppDelegate中。 swift

/* changing User Agent system-wide */ 
    UserDefaults.standard.register(
     defaults: [ 
      "UserAgent" : "\(UIWebView().stringByEvaluatingJavaScript(from: "navigator.userAgent;")) YOUR_POSTFIX_TO_USER_AGENT" 
     ] 
    ) 

對於全新的用戶代理,您應該從UIWebView().stringByEvaluatingJavaScript(from: "navigator.userAgent;")調用中刪除。