- 时间:2022-08-20 00:52 编辑: 来源: 阅读:290
- 扫一扫,手机访问
摘要:IOS WKWebView崩溃闪回完成处理程序已传递至-[xxwebviewcwebview:runjavascriptalertpanelwithm...
[源码交易]
提醒完成处理程序崩溃-[webview VC webview:runjavascriptalertpanelwithmessage:initiated by frame:completion handler:]原因分析在WKWebView的WKUIDelegate回调方法中没有调用,(void () (void)) CompletionHandler和(void () (bool)) CompletionHandler,无论是YES还是No,都必须执行 如果当前的WebViewVC被删除,completionHandler还没有执行,就会报出这个崩溃提醒(我延迟了completionHandler的执行时间,崩溃现象再次出现) 该方法可以直接在代码上处理,无需添加额外的属性。可以通过在使用(void () (bool)) CompletionHandler和(void () (void)) CompletionHandler的WKUIDelegate的几个回调方法中添加判断来处理。 # pragma mark-wkuidelegate-(void)webview:(wk webview *)webview runjavascript alertpanelwithmessage:(ns string *)message initiated by frame:(wkframeinfo *)frame completion handler:(void(^)(void))completionhandler{ if([self . navigation controller visible viewcontroller]!= self){ completion handler();返回;} UIAlertController * alert controller =[UIAlertController alertcontrollerwithttitle:@ " " message:message preferred style:UIAlertControllerStyleAlert];[Alert Controller add Action:[UIAlertaction Action with title:@ " Confirm " style:UIAlertaction style cancel Handler:(UIAlertaction * Action){ completion Handler();}]];if([self . navigation controller visible view controller]= = self){[self present view controller:alert controller animated:YES completion:nil];} else { completion handler();} }//显示两个按钮,通过completionHandler回调确定客户是点击确认还是取消按钮-(void)webview:(wk webview *)webview run JavaScript confirimpanelmessage:(nsstring *)message initiated by frame:(wkframeinfo *)frame completion handler:(void(^)(bool))completionhandler{ if([self . navigation controller visible viewcontroller]!= self){ completion handler(NO);返回;} UIAlertController * alert controller =[UIAlertController alertcontrollerwithttitle:message message:nil preferred style:UIAlertControllerStyleAlert];[Alert Controller add Action:[UIAlertaction Action with title:@ " OK " style:UIAlertaction style default Handler:(UIAlertaction * _ nonnull Action){ completion Handler(yes);}]];[Alert Controller add Action:[UIAlertaction Action with title:@ " Cancel " Style:UIAlertaction Style Cancel Handler:(UIAlertaction * _ nonnull Action){ completion Handler(no);}]];if([self . navigation controller visible view controller]= = self){[self present view controller:alert controller animated:YES completion:nil];} else { completion handler(NO);} }//显示一个带有输入框和确定按钮的,回调客户通过completion handler-(void)webview输入的内容:(wk webview *)webview runjavascripttextinputpanelwithprompt:(ns string *)提示默认文本:(ns string *)default text initiated by frame:(wkframeinfo *)frame completion handler:(void(^)(nsstring * _ nullable))completion handler { if([self . navigation controller visibleviewcontroller]!= self) { completionHandler(@“错误”);返回;} UIAlertController * alert controller =[UIAlertController alertcontrollerwithttitle:@ " " message:nil preferred style:UIAlertControllerStyleAlert];[alert controller addtextfieldwithconfigurationhandler:^(uitextfield * _ nonnull textfield){ }];[Alert Controller add Action:[UIAlertaction Action with title:@ " OK " style:UIAlertaction style default Handler:(UIAlertaction * _ nonnull Action){ completion Handler(Alert Controller . text fields . lastobject . text);}]];if([self . navigation controller visible view controller]= = self){[self present view controller:alert controller animated:YES completion:nil];} else { completion handler(@ " error ");}}