'' + (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
''
''
网友评论