标签:hone iphone from eva table info ram oms pre
公司的前端要给项目的webview加一个区分,用来区别是iOS端访问、android访问还是在浏览器访问的,说是要加一个User Agent ,前端根据不同信息做适配,和我说来一头雾水,后来经过开发同事的指导和在网上查阅资料,才有了点头绪,在这里和大家分享一下。
一、获取UserAgent
UIWebView* tempWebView = [[UIWebView alloc] initWithFrame:CGRectZero]; NSString* userAgent = [tempWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]; NSLog(@"UIWebView默认的userAgent = %@", userAgent);
UIWebView默认的userAgent = Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Mobile/14E304
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIWebView* tempWebView = [[UIWebView alloc] initWithFrame:CGRectZero]; NSString* userAgent = [tempWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]; NSLog(@"UIWebView默认的userAgent = %@", userAgent); NSString *versionsss = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];; NSString *ua = [NSString stringWithFormat:@"%@/XYWY_YIMAI/iOS/%@", userAgent, versionsss];//自定义需要拼接的字符串 [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent" : ua, @"User-Agent" : ua}]; }
NSMutableURLRequest *request = ..... /** 获取系统的UA */ NSString *sysUA = request.allHTTPHeaderFields[@"User-Agent"];
最终得到修改后的:
Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Mobile/14E304/XYWY_YIMAI/iOS/5.5
标签:hone iphone from eva table info ram oms pre
原文地址:https://www.cnblogs.com/jingxin1992/p/9066476.html