iOS设置UIWebView的UserAgent
接入第三方时,别人又需求,要求传入我们的信息。
// 获取 iOS 默认的 UserAgent,可以很巧妙地创建一个空的UIWebView来获取: NSString *userAgent = [[[UIWebView alloc] init] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]; // 获取App名称,我的App有本地化支持,所以是如下的写法 NSString *customUserAgent = [userAgent stringByAppendingFormat:@" %@", @"86wx/1.0.0"]; [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":customUserAgent}];
- (void)webViewDidFinishLoad:(UIWebView *)webView { // 打印信息 NSLog(@"UserAgent = %@", [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]); }