UIWebView & WKWebView 의 UA 설정

UIWebView 의 UA 설정
UIWebView 를 사용 할 때 user - agent 를 변경 합 니 다. 원래 시스템 UA 뒤에 만 무언 가 를 추가 할 수 있 습 니 다. stackoverflow 위 에 방법 이 있 습 니 다.
http://stackoverflow.com/questions/478387/change-user-agent-in-uiwebview-iphone-sdk/23654363#23654363
 //get the original user-agent of webview
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString *oldAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSLog(@"old agent :%@", oldAgent);
//add my info to the new agent
NSString *newAgent = [oldAgent stringByAppendingString:@" Jiecao/2.4.7 ch_appstore"];
NSLog(@"new agent :%@", newAgent);
//regist the new agent
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];

WKWebView 의 UA 설정
stackover flow 위 에 있 는 방법 이기 도 해 요.
@property(nonatomic, strong) WKWebView *webView;


 [self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        
        NSString *userAgent = result;
        NSString *newUserAgent = [userAgent stringByAppendingString:@" WeBank-Wepower/1.0.0"];
        
        NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];
        [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
        
        strongSelf.webView = [[WKWebView alloc] initWithFrame:strongSelf.view.bounds];
        strongSelf.webView.allowsBackForwardNavigationGestures = YES;
        strongSelf.webView.UIDelegate = self;
        strongSelf.webView.navigationDelegate = self;
        
        if (nil != self.urlString) {
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.urlString]];
            [request setTimeoutInterval:15];
            [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
            [strongSelf.webView loadRequest:request];
        }
        
        [strongSelf.view addSubview:self.webView];

        // After this point the web view will use a custom appended user agent
        [strongSelf.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
            NSLog(@"%@", result);
        }];
    }];

좋은 웹페이지 즐겨찾기