问题一描述:
iOS9和iOS10用WKWebView加载URL都没有问题,iOS11却是一片空白
可能是用了NSMutableURLRequest,iOS11貌似不支持NSMutableURLRequest,无论是用UIWebView还是WKWebView,都不支持NSMutableURLRequest
解决方法参考
1
2
3
4
5
6
7
8
9
|
if #available(iOS 11, *) {
let request = NSURLRequest.init(url: URL.init(string: urlStr)!)
self.wkWebView.load(request as URLRequest)
} else {
let request = NSMutableURLRequest.init(url: URL.init(string: urlStr)!, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 60)
request.httpMethod = "GET"
request.httpBody = ( "token=" + tokenValue()).data( using : String.Encoding.utf8)
self.wkWebView.load(request as URLRequest)
}
|
问题二描述:在用iPhone X 的模拟器进入Hybrid项目时,发现一进去就崩溃,崩溃信息少的可怜:
libc++abi.dylib: terminating with uncaught exception of type NSException
靠这玩意儿肯定是定位不出bug的,不过全局断点还是给出了一点信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
- ( void )webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:( void (^)(WKNavigationActionPolicy))decisionHandler {
NSString *requestString = navigationAction.request.URL.absoluteString;
//对外链、拨号和跳转appstore做特殊处理
UIApplication *app = [UIApplication sharedApplication];
NSURL *url = [navigationAction.request URL];
//电话
//此处省略若干业务代码
if ([url.absoluteString containsString:@ "itunes.apple.com" ])
{
if ([app canOpenURL:url])
{
[app openURL:url];
decisionHandler(WKNavigationActionPolicyCancel);
}
}
if ([requestString hasPrefix:@ "easy-js:" ]) {
[self handleRequestString:requestString webView:(EasyJSWebView *)webView.superview];
decisionHandler(WKNavigationActionPolicyCancel);
}
if ([self.realDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)])
{
[self.realDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
}
decisionHandler(WKNavigationActionPolicyAllow); //崩在这里
}
|
仍然不知道为啥子崩在这儿?之前一直是没问题的啊??
小Tips:
为了获取一些堆栈信息以便于快准狠的定位问题,可以在main函数里:
1
2
3
4
5
6
7
8
9
10
11
12
|
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]);
}
}
|
最终得到一条关键报错:
1
|
Completion handler passed to -[WKPrivateNavigationDelegate webView:decidePolicyForNavigationAction:decisionHandler:] was called more than once
|
意思就是WKWebView的这个代理方法被多次调用了。
1
2
3
4
5
6
7
8
9
|
if ([requestString hasPrefix:@ "easy-js:" ]) {
[self handleRequestString:requestString webView:(EasyJSWebView *)webView.superview];
decisionHandler(WKNavigationActionPolicyCancel);
}
if ([self.realDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)])
{
[self.realDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
}
decisionHandler(WKNavigationActionPolicyAllow); //崩在这里
|
简单分析一下被多次调用的原因:
1、系统判断这个方法被多次执行,主要是看decisionHandler()是否被多次执行;
2、由于if判断里会执行decisionHandler(),最后一行代码也会执行decisionHandler(),并且self.realDelegate中也会执行decisionHandler(),这就导致了decisionHandler()这个handler可能会被多次执行。
那解决问题的方向就是修改代码保证WKWebView单次LoadRequest只调一次此代理方法~
修改如下:
1
2
3
4
5
6
7
8
9
10
|
if ([requestString hasPrefix:@ "easy-js:" ]) {
[self handleRequestString:requestString webView:(EasyJSWebView *)webView.superview];
decisionHandler(WKNavigationActionPolicyCancel);
}
else if ([self.realDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)])
{
[self.realDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}
|
即保证了单次LoadRequest只执行一次decisionHandler()
问题三描述:iOS11 WKWebview获取高度不准确
遇见这个问题的时候,我发现偏离了大概64像素,由此联想到了tableView和collectionView。
故解决办法如下:
1
2
3
4
5
|
if (@available(iOS 11.0, *)) {
_webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
_webView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
_webView.scrollView.scrollIndicatorInsets = _webView.scrollView.contentInset;
}
|
原文链接:http://www.jianshu.com/p/9efa333820f9