User Agent中文名為用戶代理歪沃,簡稱 UA嗦锐,它是一個特殊字符串頭,使得服務器能夠識別客戶使用的操作系統(tǒng)及版本沪曙、CPU 類型奕污、瀏覽器及版本、瀏覽器渲染引擎珊蟀、瀏覽器語言菊值、瀏覽器插件等。
那么育灸,設置User Agent有什么用呢腻窒?
比如你在做移動APP開發(fā)時,有個網站在你手機端APP打開之后和直接在瀏覽器中打開之后看到的東西不一樣磅崭,網頁會根據UA判斷你是app打開的還是直接瀏覽器打開的儿子,如果沒有找到事先商定的UA信息就認定這個鏈接是在瀏覽器打開的,如果查詢到事先商定的UA就是用app打開的砸喻,兩種情況可以讓你看到不同的東西柔逼,做不同的操作蒋譬,比如有些東西開發(fā)者只想讓你使用app打開才能看見。
當然愉适,這也許只是UA的一種作用犯助,其實UA還有很多用處,大家有興趣的可以自行查閱資料或者留言维咸。
獲取 iOS 系統(tǒng)默認的 User Agent剂买,我知道的有2種方式:
方式一:
NSString *sysUA = request.allHTTPHeaderFields[@"User-Agent"];
方式二:
// 獲取 iOS 默認的 UserAgent瞬哼,可以很巧妙地創(chuàng)建一個空的UIWebView來獲茸俊:NSString *userAgent = [[[UIWebView alloc] init] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
下面是我獲取到的系統(tǒng)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系統(tǒng)放硬件版本的信息的地方? ? 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