给UIWebView调整UserAgent字段

+(void)setWebViewUserAgent:(NSString *)suffix
{
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    NSString *oldAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSLog(@"old agent :%@", oldAgent);
    if ([CommonHelper isEmpty:suffix])
        suffix = @"TTSWebView";
    NSString *newAgent = [oldAgent stringByAppendingFormat:@"%@-iOS-%@",suffix,[CommonHelper appVersion]];
    NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
}

在应用启动时调用一次即可.

原文地址:https://www.cnblogs.com/decwang/p/5481030.html