修改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];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原文 在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新。 1.UITableView的Group...
    无沣阅读 792评论 0 2
  • iOS 開発の結構 画面 UI UIWebview [[UIApplication sharedApplicati...
    RencaiXiong阅读 599评论 0 0
  • 使用场景: 有个项目需求,要区分打开H5是在本地APP还是在手机浏览器,前端伙伴说需要配合修改默认的UserAge...
    千寻_544f阅读 5,570评论 0 4
  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,141评论 1 6
  • 总结这几年所遇见的坑 一、 iPhone Size 二、 给navigation Bar 设置 title 颜色 ...
    无沣阅读 531评论 0 0