- (void)requestWithUserAgent {
[self.wkWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable response, NSError *error) {
NSString *res = response;
// 防止重复添加
if ([res containsString:@"xxxxxx"]) {
NSArray *agentArray = [res componentsSeparatedByString:@"xxxxxx"];
self.userAgent = agentArray.firstObject;
} else {
self.userAgent = response;
}
NSString *agent = [NSString stringWithFormat:@"%@%@", self.userAgent, [self.common convertUserAgentToJson]];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:agent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.wkWebView setCustomUserAgent:agent];
[self loadWKWebView];
}];
}
- (void)loadWKWebView {
NSString *urlString = [NSString stringWithFormat:@"%@/%@", [self.common host], URL_HTML];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSString *agent = [NSString stringWithFormat:@"%@%@", self.userAgent, [self.common convertUserAgentToJson]];
[request addValue:agent forHTTPHeaderField:@"UserAgent"];
[self.wkWebView loadRequest:request];
}