美文网首页iOS组件化
iOS 简单使用UserAgent

iOS 简单使用UserAgent

作者: 千寻_544f | 来源:发表于2017-11-28 17:06 被阅读45次

    使用场景:

    有个项目需求,要区分打开H5是在本地APP还是在手机浏览器,前端伙伴说需要配合修改默认的UserAgent,以便区分。

    一、如何获取UserAgent

    UIWebView方式:

    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];

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

    DLog(@"userAgent :%@", userAgent);

    WKWebView方式:

    // 注意这个方法是异步的

    WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:CGRectZero];

    [wkWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {

    DLog(@"userAgent :%@", result);

    }];

    默认UserAgent输出:

    Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12H143

    微信 iOS版的 :UserAgent

    mozilla/5.0 (iphone; cpu iphone os 5_1_1 like mac os x) applewebkit/534.46 (khtml, like gecko) mobile/9b206 micromessenger/5.0

    其中micromessenger就是自定义的

    二、如何修改UserAgent

    方案一,修改全局UserAgent值(这里是在原有基础上拼接自定义的字符串)

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];

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

    NSString *newUserAgent = [userAgent stringByAppendingString:@" native_iOS"];//自定义需要拼接的字符串

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];

    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

    }

    方案二,自定义UserAgent值

    WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview: wkWebView];

    NSString *customUserAgent = @"native_iOS";

    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":customUserAgent}];

    NSURL *url = [NSURL URLWithString:self.strUrl];

    NSURLRequest *request = [NSURLRequest requestWithURL:url

    cachePolicy:NSURLRequestUseProtocolCachePolicy

    timeoutInterval:10.f];

    [self.wkWebView loadRequest:request];

    方案三

    self.wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds];

    __weak typeof(self) weakSelf = self;

    [self.wkWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {

    __strong typeof(weakSelf) strongSelf = weakSelf;

    NSString *userAgent = result;

    NSString *newUserAgent = [userAgent stringByAppendingString:@" native_iOS"];

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];

    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

    // needs retain because `evaluateJavaScript:` is asynchronous

    strongSelf.wkWebView = [[WKWebView alloc] initWithFrame:strongSelf.view.bounds];

    }];

    [self.wkWebView loadRequest:request];

    实例:

    if(originUserAgent==nil) {

    [self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {

    originUserAgent = result;

    NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

    NSString *ua=[originUserAgent stringByAppendingFormat:@" xxxxx/%@",currentVersion];

    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":ua}];

    //必须有此句代码,否则第一次加载不会修改UserAgent

    [self.webView setCustomUserAgent:ua];

    }];

    }

    三、问题& 思考

    1.有时整个项目的UIWebView比较多,所以新建一个UIWebView的基类,然后在基类的初始化方法- (instancetype)initWithFrame:(CGRect)frame里添加以下代码:

    NSString * oldAgent = [self stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];

    NSString * newAgent = oldAgent;

    if (![oldAgent hasSuffix:@"panda"])

    {

    newAgent = [oldAgent stringByAppendingString:@"/panda"];

    }

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

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

    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];

    [[NSUserDefaults standardUserDefaults] synchronize];

    调试的发现,当手机或者模拟器连接Mac调试的时候,H5页面都能获得修改后的UIWebView的UserAgent,但是一旦断开调试模式,真机运行和模拟器运行的时候H5获得的UserAgent是UIWebView默认的UserAgent,而不是修改后的UserAgent。

    原因是:webView会替换成默认的UserAgent。

    若遇见这种情况可以换一种解决方式,直接在AppDelegate里面- (BOOL)application:(UIApplication)application didFinishLaunchingWithOptions:(NSDictionary)launchOptions修改默认的UserAgent,这种方式一劳永逸。

    UIWebView * tempWebView = [[UIWebView alloc] initWithFrame:CGRectZero];

    NSString * oldAgent = [tempWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];

    NSString * newAgent = oldAgent;

    if (![oldAgent hasSuffix:@"panda"])

    {

    newAgent = [oldAgent stringByAppendingString:@"/panda"];

    }

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

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

    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];

    [[NSUserDefaults standardUserDefaults] synchronize];

    解决了以上问题,但是至于为什么在每个UIWebView初始化的时候修改UserAgent,H5获取不到修改后的UserAgent,真的不太清楚,还请了解的大神指点一下。

    2.关于WKWebView 修改UserAgent

    // 要区分打开H5是在本地APP还是在手机浏览器,前端伙伴说需要配合修改默认的 UserAgent,以便区分。

    //    修改全局UserAgent值(这里是在原有基础上拼接自定义的字符串)

    [_mainWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {

    NSString *userAgent = result;

    NSString *newUserAgent = [userAgent stringByAppendingString:@" ios/jkbs/1.2.3"];

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];

    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

    [[NSUserDefaults standardUserDefaults] synchronize];

    // 在网上找到的没有下面这句话,结果只是更改了本地的UserAgent,没修改网页的,导致一直有问题,好低级的错误,这个函数是9.0之后才出现的,在这之前,把这段代码放在WKWebView的alloc之前才会有效

    [_mainWebView setCustomUserAgent:newUserAgent];

    //    判断网址类型

    if ([self.url YgContainsString:@"indexlocal"]) {

    //        本地html

    [_mainWebView YgLoadLocationUrl:self.url];

    }else{

    [_mainWebView YgLoadNoneParaUrl:self.url];

    }

    }];

    3.在测试的时候,发现方案二、三第一次运行的时候,在iOS9以下还是显示默认的值,第二次才会显示自定义的值,WKWebView原因如上结果只是更改了本地的UserAgent,没修改网页的,导致一直有问题;UIWebView的如有朋友解决麻烦告诉一下,谢谢。

    参考链接:iOS 修改默认 UserAgent

    相关文章

      网友评论

        本文标题:iOS 简单使用UserAgent

        本文链接:https://www.haomeiwen.com/subject/zqrebxtx.html