WKWebView 添加 userAgent 信息

2017-05-22  本文已影响55人  溪鱼

'' + (void)initialize {

''

'' BaseViewController*webVc;

''

'' //获取自身UA

''

'' if([webVcclass] == [UIWebViewclass])

''

'' {

''

'' UIWebView* webView = [[UIWebViewalloc]initWithFrame:CGRectZero];

''

'' NSString* secretAgent = [webViewstringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];

''

'' //NSString *newUserAgent = [secretAgent stringByAppendingString:@" Appended Custom User Agent"];

''

'' NSString*newUserAgent = [NSStringstringWithFormat:@"%@ liuchang/%@",secretAgent,[[NSBundlemainBundle]objectForInfoDictionaryKey:@"CFBundleShortVersionString"]];

''

'' // Set user agent (the only problem is that we can't modify the User-Agent later in the program)

''

'' NSDictionary*dictionnary = [[NSDictionaryalloc]initWithObjectsAndKeys:newUserAgent,@"UserAgent",nil];

''

'' [[NSUserDefaultsstandardUserDefaults]registerDefaults:dictionnary];

''

'' }

''

'' else

''

'' {

''

'' WKWebView*wkWebView = [[WKWebViewalloc]initWithFrame:CGRectZero];

''

'' NSString* secretAgent = [wkWebViewstringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];

''

'' //NSString *newUserAgent = [secretAgent stringByAppendingString:@" Appended Custom User Agent"];

''

'' NSString*newUserAgent = [NSStringstringWithFormat:@"%@ liuchang/%@",secretAgent,[[NSBundlemainBundle]objectForInfoDictionaryKey:@"CFBundleShortVersionString"]];

''

'' // Set user agent (the only problem is that we can't modify the User-Agent later in the program)

''

'' NSDictionary*dictionnary = [[NSDictionaryalloc]initWithObjectsAndKeys:newUserAgent,@"UserAgent",nil];

''

'' [[NSUserDefaultsstandardUserDefaults]registerDefaults:dictionnary];

''

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

''

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

''

'' //

''

'' //if (result)

''

'' //{

''

'' //NSString *newUserAgent = [NSString stringWithFormat:@"%@ Wanxiao/%@",result,[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]];

''

'' //// Set user agent (the only problem is that we can't modify the User-Agent later in the program)

''

'' //NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];

''

'' //[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];

''

'' //}

''

'' //}];

''

'' }

''

'' }

''

分类

WKWebView+SynchronousEvaluateJavaScript.h

'' //

''

'' //WKWebView+SynchronousEvaluateJavaScript.h

''

'' //NCP

''

'' //

''

'' //Created by liuchang on 16/10/28.

''

'' //Copyright  All rights reserved.

''

'' //

''

'' #import

''

'' @interfaceWKWebView (SynchronousEvaluateJavaScript)

''

'' - (NSString*)stringByEvaluatingJavaScriptFromString:(NSString*)script;

''

'' @end

''

WKWebView+SynchronousEvaluateJavaScript.m

'' //

''

'' //

''

'' //Created by liuchang on 16/10/28.

''

'' //Copyright  All rights reserved.

''

'' //

''

'' #import"WKWebView+SynchronousEvaluateJavaScript.h"

''

'' @implementationWKWebView (SynchronousEvaluateJavaScript)

''

'' - (NSString*)stringByEvaluatingJavaScriptFromString:(NSString*)script

''

'' {

''

'' __blockNSString*resultString =nil;

''

'' __blockBOOLfinished =NO;

''

'' [selfevaluateJavaScript:scriptcompletionHandler:^(idresult,NSError*error) {

''

'' if(error ==nil) {

''

'' if(result !=nil) {

''

'' resultString = [NSStringstringWithFormat:@"%@", result];

''

'' }

''

'' }else{

''

'' NSLog(@"evaluateJavaScript error : %@", error.localizedDescription);

''

'' }

''

'' finished =YES;

''

'' }];

''

'' while(!finished)

''

'' {

''

'' [[NSRunLoopcurrentRunLoop]runMode:NSDefaultRunLoopModebeforeDate:[NSDatedistantFuture]];

''

'' }

''

'' returnresultString;

''

'' }

''

'' @end

''

''

上一篇 下一篇

猜你喜欢

热点阅读