| | |
| | | [_webView setMultipleTouchEnabled:YES]; |
| | | [_webView setAutoresizesSubviews:YES]; |
| | | [_webView.scrollView setAlwaysBounceVertical:YES]; |
| | | // [_webView setCustomUserAgent:@"Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Mobile/15E148 Safari/604.1"]; |
| | | // 获取当前UserAgent, 并对其进行修改 |
| | | [_webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id userAgent, NSError * _Nullable error) { |
| | | if ([userAgent isKindOfClass:[NSString class]]) { |
| | | NSString *systemUserAgent =@""; |
| | | systemUserAgent = [NSString stringWithFormat:@"%@ %@", userAgent, @"Safari/604.1" ]; |
| | | // 关键代码, 必须实现这个方法, 否则第一次打开UA还是原始值, 待第二次打开webview才是正确的UA; |
| | | [_webView setCustomUserAgent:systemUserAgent]; |
| | | |
| | | } |
| | | }]; |
| | | // [_webView setCustomUserAgent:@"Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Mobile/15E148 Safari/604.1"]; |
| | | // 获取当前UserAgent, 并对其进行修改 |
| | | [_webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id userAgent, NSError * _Nullable error) { |
| | | if ([userAgent isKindOfClass:[NSString class]]) { |
| | | NSString *systemUserAgent =@""; |
| | | systemUserAgent = [NSString stringWithFormat:@"%@ %@", userAgent, @"Safari/604.1" ]; |
| | | // 关键代码, 必须实现这个方法, 否则第一次打开UA还是原始值, 待第二次打开webview才是正确的UA; |
| | | [_webView setCustomUserAgent:systemUserAgent]; |
| | | |
| | | } |
| | | }]; |
| | | |
| | | |
| | | // 这行代码可以是侧滑返回webView的上一级,而不是跟控制器(*指针对侧滑有效) |
| | |
| | | |
| | | } |
| | | |
| | | -(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{ |
| | | WKNavigationActionPolicy policy = WKNavigationActionPolicyAllow; |
| | | @try { |
| | | NSString *url; |
| | | url = navigationAction.request.URL.absoluteString; |
| | | NSLog(@"链接:%@",url); |
| | | |
| | | if([url hasPrefix:@"iqiyi"]){ |
| | | if ([[UIApplication sharedApplication] |
| | | canOpenURL:[NSURL URLWithString:url]]){ |
| | | [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; |
| | | } |
| | | }else{ |
| | | |
| | | |
| | | /* |
| | | 如果是域名,判断itunes的host链接 ; |
| | | 如果是scheme跳转,就判断是否是 App Store 的 scheme |
| | | */ |
| | | |
| | | if([[navigationAction.request.URL host] isEqualToString:@"itunes.apple.com"] || [[navigationAction.request.URL scheme] isEqualToString:@"itms-apps"]) { |
| | | if ( [[UIApplication sharedApplication] openURL:navigationAction.request.URL]) { |
| | | policy = WKNavigationActionPolicyCancel; |
| | | NSLog(@"已跳转至App Store"); |
| | | } |
| | | } |
| | | |
| | | } |
| | | } @catch (NSException *exception) { |
| | | |
| | | } @finally { |
| | | |
| | | } |
| | | decisionHandler(policy); |
| | | |
| | | } |
| | | |
| | | |
| | | -(BOOL)shouldAutorotate{ |
| | | return YES; |
| | |
| | | } |
| | | |
| | | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { |
| | | if ([keyPath isEqualToString:@"title"]) { |
| | | if ([keyPath isEqualToString:@"title"]) { |
| | | if (object == _webView) { |
| | | NSString *title = _webView.title; |
| | | if(webtitle==nil){ |
| | |
| | | } |
| | | }else if ([keyPath isEqualToString:@"estimatedProgress"]) { |
| | | if (object == _webView) { |
| | | // double progress = _webView.estimatedProgress; |
| | | // if(progress!=1){ |
| | | // [SVProgressHUD showProgress:progress status:@"加载中"]; |
| | | // }else{ |
| | | // [SVProgressHUD dismiss]; |
| | | // } |
| | | // double progress = _webView.estimatedProgress; |
| | | // if(progress!=1){ |
| | | // [SVProgressHUD showProgress:progress status:@"加载中"]; |
| | | // }else{ |
| | | // [SVProgressHUD dismiss]; |
| | | // } |
| | | } else { |
| | | [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; |
| | | } |