使用了第二种方法,略作修改,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//欢迎页显示3秒
[NSThread sleepForTimeInterval:2.0];
//修改app默认UA
@autoreleasepool {
UIWebView* tempWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString* userAgent = [tempWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
// NSLog(@"------%@",userAgent);
NSString *executableFile = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleExecutableKey];
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
NSString *ua = [NSString stringWithFormat:@"%@ %@/%@",
userAgent,
executableFile,version];
// NSLog(@"------%@",ua);
[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent" : ua, @"User-Agent" : ua}];
#if !__has_feature(objc_arc)
[tempWebView release];
#endif
}
}
在网页中查看效果
//在Safari中打开文章中的文档
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"ua=======%@",[request valueForHTTPHeaderField:@"User-Agent" ]);
//判断是否是单击
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *url = [request URL];
[[UIApplication sharedApplication]openURL:url];
return NO;
}
return YES;
}