給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];
}

在應用啓動時調用一次便可.web

相關文章
相關標籤/搜索