场景
在下面回调函数中下断点,当前使用 UIWebview
加载 mp4
格式视频时,会捕捉到异常。
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
...
}
打印错误明细
(lldb) po error
Error Domain=WebKitErrorDomain Code=204 "Plug-in handled load"
UserInfo={
NSErrorFailingURLStringKey=file:///~/Library/Developer/CoreSimulator/Devices/5563EC11-037A-4FD8-8815-8B4867
Error Domain=WebKitErrorDomain
Code=204 "Plug-in handled load"
UserInfo={
NSErrorFailingURLStringKey=file:///~/Documents/263-E99658601-uat/CoursePacages/PackageCourse/252.mp4,
NSErrorFailingURLKey=file:///~/Documents/263-E99658601-uat/CoursePacages/PackageCourse/252.mp4,
WebKitErrorMIMETypeKey=video/mp4,
NSLocalizedDescription=Plug-in handled load
}
05BA58~/Documents/263-E99658601-uat/CoursePacages/PackageCourse/252.mp4,
NSErrorFailingURLKey=file:///~/Documents/263-E99658601-uat/CoursePacages/PackageCourse/252.mp4,
WebKitErrorMIMETypeKey=video/mp4,
NSLocalizedDescription=Plug-in handled load
}
解决方案
可忽略此错误,同理还有其他同类错误:
-
当网页内部链接跳转时
[error.domain isEqualToString:@"NSURLErrorDomain"] && error.code == NSURLErrorCancelled
-
当网页包含
appstore
链接时[error.domain isEqualToString:@"WebKitErrorDomain"] && error.code == 102
-
当链接就视频路径时(不影响视频正常播放)
[error.domain isEqualToString:@"WebKitErrorDomain"] && error.code == 204
具体代码
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
if ([error.domain isEqualToString:@"NSURLErrorDomain"] && error.code == NSURLErrorCancelled) {
NSLog(@"Canceled request: %@", webView.request.URL);
return;
}
else if ([error.domain isEqualToString:@"WebKitErrorDomain"] && (error.code == 102 || error.code == 204)) {
NSLog(@"ignore: %@", error);
return;
}
[ViewUtils simpleAlertView:self Title:@"error" Message:[error localizedDescription] ButtonTitle:@"sure"];
}
其他错误
解决上述问题时,发现这个问题 Domain=WebKitErrorDomain Code=101 "The operation couldn’t be completed. "
原因是链接字符串中含未转义的字符,比如空格、竖线、百分号、中文字等。
解决方案
// [error.domain isEqualToString:@"WebKitErrorDomain"] && error.code == 101
urlString =[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];