iOS11 WKWebView问题汇总
问题一描述:
ios9和ios10用wkwebview加载url都没有问题,ios11却是一片空白
可能是用了nsmutableurlrequest,ios11貌似不支持nsmutableurlrequest,无论是用uiwebview还是wkwebview,都不支持nsmutableurlrequest
解决方法参考
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的,不过全局断点还是给出了一点信息:
- (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函数里:
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]); } }
最终得到一条关键报错:
completion handler passed to -[wkprivatenavigationdelegate webview:decidepolicyfornavigationaction:decisionhandler:] was called more than once
意思就是wkwebview的这个代理方法被多次调用了。
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只调一次此代理方法~
修改如下:
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。
故解决办法如下:
if (@available(ios 11.0, *)) { _webview.scrollview.contentinsetadjustmentbehavior = uiscrollviewcontentinsetadjustmentnever; _webview.scrollview.contentinset = uiedgeinsetsmake(0, 0, 0, 0); _webview.scrollview.scrollindicatorinsets = _webview.scrollview.contentinset; }