Question 1 Description:
There is no problem loading URLs with WKWebView in iOS9 and iOS10, but iOS11 is blank
Maybe it is because of using NSMutableURLRequest. iOS11 does not seem to support NSMutableURLRequest. Whether using UIWebView or WKWebView, it does not support NSMutableURLRequest.
Solution reference
if #available(iOS 11, *) { let request = (url: (string: urlStr)!) (request as URLRequest) }else{ let request = (url: (string: urlStr)!, cachePolicy: , timeoutInterval: 60) = "GET" = ("token=" + tokenValue()).data(using: .utf8) (request as URLRequest) }
Question 2 Description: When entering the Hybrid project using the iPhone X emulator, I found that it crashed as soon as I entered, and the crash information was pitiful:
libc++: terminating with uncaught exception of type NSException
This thing will definitely not be able to locate bugs, but the global breakpoint still gives some information:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { NSString *requestString = ; //Do special treatments for external links, dialing and jumping appstore UIApplication *app = [UIApplication sharedApplication]; NSURL *url = [ URL]; //Telephone // Several business codes are omitted here if ([ containsString:@""]) { if ([app canOpenURL:url]) { [app openURL:url]; decisionHandler(WKNavigationActionPolicyCancel); } } if ([requestString hasPrefix:@"easy-js:"]) { [self handleRequestString:requestString webView:(EasyJSWebView *)]; decisionHandler(WKNavigationActionPolicyCancel); } if ([ respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)]) { [ webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler]; } decisionHandler(WKNavigationActionPolicyAllow);//Crash here}
Still don't know why the collapse is here? It's been OK before? ?
Tips:
In order to obtain some stack information to facilitate fast and accurate positioning problems, you can use the main function:
int main(int argc, char * argv[]) { @try { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } } @catch (NSException* exception) { NSDebugLog(@"Exception=%@\nStack Trace:%@", exception, [exception callStackSymbols]); } }
Finally, I got a key error:
Completion handler passed to -[WKPrivateNavigationDelegate webView:decidePolicyForNavigationAction:decisionHandler:] was called more than once
This means that the proxy method of WKWebView has been called many times.
if ([requestString hasPrefix:@"easy-js:"]) { [self handleRequestString:requestString webView:(EasyJSWebView *)]; decisionHandler(WKNavigationActionPolicyCancel); } if ([ respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)]) { [ webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler]; } decisionHandler(WKNavigationActionPolicyAllow);//Crash here
A brief analysis of the reasons for being called multiple times:
1. The system determines that this method has been executed multiple times, mainly to see whether decisionHandler() has been executed multiple times;
2. Since decisionHandler() will be executed in the if judgment, the last line of code will also be executed in decisionHandler(), and decisionHandler() will also be executed in the decisionHandler() handler, which causes decisionHandler() to be executed multiple times.
The direction of solving the problem is to modify the code to ensure that WKWebView only adjusts this proxy method once a single LoadRequest~
Modifications are as follows:
if ([requestString hasPrefix:@"easy-js:"]) { [self handleRequestString:requestString webView:(EasyJSWebView *)]; decisionHandler(WKNavigationActionPolicyCancel); } else if ([ respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)]) { [ webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler]; } else { decisionHandler(WKNavigationActionPolicyAllow); }
That is, it ensures that a single LoadRequest is executed only once decisionHandler()
Question 3 Description: iOS11 WKWebview get height is inaccurate
When I encountered this problem, I found that it was about 64 pixels away, which reminded me of tableView and collectionView.
Therefore, the solution is as follows:
if (@available(iOS 11.0, *)) { _webView. = UIScrollViewContentInsetAdjustmentNever; _webView. = UIEdgeInsetsMake(0, 0, 0, 0); _webView. = _webView.; }