问题描述: 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)
}
|
ios11 xcode9 wkwebview崩溃问题解决方案
正式版的ios11&xcode 9已经发布,乘着版本空档期,赶紧花点时间完成适配工作。
在用iphone x 的模拟器进入hybrid项目时,发现一进去就崩溃,崩溃信息少的可怜:
1
|
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]);
}
}
|
最终得到一条关键报错:
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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.jianshu.com/p/909afcbc401e