這裏是我的代碼:的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任何幫助!