美文网首页
【转】什么是User Agent?简单了解一下

【转】什么是User Agent?简单了解一下

作者: 棒棒德 | 来源:发表于2021-09-06 10:20 被阅读0次

    User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。

    那么,设置User Agent有什么用呢?

    比如你在做移动APP开发时,有个网站在你手机端APP打开之后和直接在浏览器中打开之后看到的东西不一样,网页会根据UA判断你是app打开的还是直接浏览器打开的,如果没有找到事先商定的UA信息就认定这个链接是在浏览器打开的,如果查询到事先商定的UA就是用app打开的,两种情况可以让你看到不同的东西,做不同的操作,比如有些东西开发者只想让你使用app打开才能看见。

    当然,这也许只是UA的一种作用,其实UA还有很多用处,大家有兴趣的可以自行查阅资料或者留言。

    获取 iOS 系统默认的 User Agent,我知道的有2种方式:

    方式一:

    NSString *sysUA = request.allHTTPHeaderFields[@"User-Agent"];

    方式二:

    // 获取 iOS 默认的 UserAgent,可以很巧妙地创建一个空的UIWebView来获取:NSString *userAgent = [[[UIWebView alloc] init] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];

    下面是我获取到的系统UA:

    Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Mobile/14A345

    来看一下Coding-iOS中的代码:

    - (void)registerUserAgent{    struct utsname systemInfo;  // 这是LINUX系统放硬件版本的信息的地方    uname(&systemInfo);    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];    NSString *userAgent = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleExecutableKey] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleIdentifierKey], (__bridge id)CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey) ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleVersionKey], deviceString, [[UIDevice currentDevice] systemVersion], ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] ? [[UIScreen mainScreen] scale] : 1.0f)];    NSDictionary *dictionary = @{@"UserAgent" : userAgent};//User-Agent    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];}

    以上代码中,相信大部分大码大家都能看到,但是我是菜鸟,有一些是不太懂的,譬如获取设备信息。我了解到的方式有2种:

    获取硬件版本信息

    方法一:

    struct utsname systemInfo;uname(&systemInfo);NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

    方法二:

    size_t size;sysctlbyname("hw.machine", NULL, &size, NULL, 0);char *machine = malloc(size);sysctlbyname("hw.machine", machine, &size, NULL, 0);NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];free(machine);

    具体返回的字符串代表什么设备,可以查看设备清单

    举个简单的例子:

    在application:didFinishLaunchingWithOptions:方法中添加以下代码:

    NSString *userAgent = [[[UIWebView alloc] init] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];NSString * appName = [[NSBundle mainBundle] infoDictionary][@"CFBundleDisplayName"];NSString *version = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];NSString *customUserAgent = [userAgent stringByAppendingFormat:@" %@/%@", appName, version];[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":customUserAgent}];

    在controller中添加以下代码:

    UIWebView *webView = [[UIWebView alloc] init];webView.delegate = self;[webView loadRequest:[NSURLRequest requestWithURL:[NSURLURLWithString:@"https://www.baidu.com/"]]];[self.view addSubview:webView];

    - (void)webViewDidFinishLoad:(UIWebView *)webView{NSLog(@"UserAgent = %@", [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]);}

    此时User Agent的值为

    Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Mobile/14A345  捷友家/1.0

    相关文章

      网友评论

          本文标题:【转】什么是User Agent?简单了解一下

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