修改webview默认userAgent

-(void)modifyWebViewUserAgentWithWebview:(UIWebView*)webview

{

      NSString*originalAgent = [webview stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];

     NSString*key =@"bbtree_P/";

     NSArray*array = [originalAgent componentsSeparatedByString:key];

     NSString*version = [[NSBundle      mainBundle]objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

    NSString*newAgent = NULLSTRING;

if([array count]>=2)

{

     newAgent = [NSString stringWithFormat:@"%@%@%@",              [arrayobjectAtIndex:0],key,version];

}else

{

      newAgent = [NSString stringWithFormat:@"%@ %@%@",originalAgent,key,version];

}

     DEBUGLog(@"original agent :%@", originalAgent);

     DEBUGLog(@"new agent :%@", newAgent);

      //regist the new agent

     NSDictionary*dictionnary = [[NSDictionary      alloc]initWithObjectsAndKeys:newAgent,@"UserAgent",nil];

     [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];

     [[NSUserDefaults standardUserDefaults] synchronize];

}

你可能感兴趣的:(修改webview默认userAgent)