ios - 在 iOS 上为文件 ://URLs causes "Frame load interrupted" error 使用自定义 NSURLProtocol

标签 ios

我想将变化的文件加载到 UIWebView 中,我希望它们在 file://方案下加载,这样我就可以在我的页面中包含其他 file://资源(例如 html5 视频元素)。为此,我实现了自己的 NSURLProtocol 并覆盖了 file://URL 子集的加载。

我的 startLoading 方法看起来像这样:

- (void) startLoading {
    ... data is populated ...
    [protocolClient URLProtocol:self didLoadData:data];
    [protocolClient URLProtocolDidFinishLoading:self];
}

这在 iOS4/5 上工作正常,但在 iOS 6 beta 4 上我收到以下错误:

2012-08-21 16:06:07.236 TemplateApp[57283:1d403] 57283: CFNetwork internal error (0xc01a:/SourceCache/CFNetwork_Sim/CFNetwork-606/Connection/URLConnectionClient.cpp:2341)
/SourceCache/WebCore_Sim/WebCore-1634/wak/WKView.mm:385 void WKViewAddSubview(WKViewRef, WKViewRef):  invalid parameter
2012-08-21 16:06:07.272 TemplateApp[57283:1e603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSSetM addObject:]: object cannot be nil'
*** First throw call stack:
(0x29d0552 0x2733e7e 0x2a5dc08 0x69c6c1e 0x69c4c88 0x69c543b 0x5b40dbd 0x5b40f0f 0x5b3c240 0x5ff8060 0x5ff75d1 0x5eb8d08 0x65a4fb2 0x678486f 0x677fa3d 0x7b3ab1 0x7b2d63 0x7f0e5a 0x2972ebd 0x7f14dc 0x7f1455 0x6db410 0x29544ff 0x2953f2f 0x2976cf4 0x2976504 0x29763db 0x69d0530 0x907caed9 0x907ce6de)
libc++abi.dylib: terminate called throwing an exception

我通过向我的 startLoading 方法添加一个 didReceiveResponse 调用来解决这个问题,如下所示:

- (void) startLoading {
    ... data is populated ...
    [protocolClient URLProtocol:self didReceiveResponse:[[NSURLResponse alloc] init] cacheStoragePolicy:NSURLCacheStorageAllowed]
    [protocolClient URLProtocol:self didLoadData:data];
    [protocolClient URLProtocolDidFinishLoading:self];
}

但是现在在 iOS 5 和 6 上,我在尝试加载页面时遇到以下错误:

Webview error: Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted" UserInfo=0x9d4c0a0 {NSErrorFailingURLKey=file:///src/index.html, NSErrorFailingURLStringKey=file:///src/index.html, NSLocalizedDescription=Frame load interrupted}

我在 didReceiveResponse 方法中尝试了各种不同的 NSURLResponse 对象,但没有一个允许 WebView 加载页面。使用 file://以外的 URL 方案也可以,但我不能将 file://路径用于嵌入式资源,例如 HTML5 视频。

有没有办法将特定 file://URL 的动态内容加载到适用于 iOS 6 beta 4 的 UIWebView 中?

最佳答案

我目前在将内容加载到 UIWebView 时遇到一些其他问题,但我认为您需要使用 NSURLResponseinitWithURL:MIMEType:expectedContentLength:textEncodingName:

否则我认为您没有有效的响应对象。我遇到的问题是,在我的 startLoading 甚至被调用之前,我得到了“错误的 URL”。

更新:我现在开始工作了,显然我的 canonicalRequestForRequest: 很傻

关于ios - 在 iOS 上为文件 ://URLs causes "Frame load interrupted" error 使用自定义 NSURLProtocol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12058203/

相关文章:

ios - _OBJC_CLASS_$_ScringoAgent”,引用自 :

ios - 在 Objective-C 中计算具有不同语言和表情符号的文本的 UILabel 高度

ios - 当您为应用程序编写 UI 代码时,哪些工具可以帮助 Xcode 减少构建和运行的频率

ios - 表在滚动之前不会重新加载数据

ios - Swift:在纺车概念中为 subview 启用 userInteractionEnabled

ios - 在 URL 查询中使用俄语字符

ios - 将 CVImageBufferRef 转换为 CGImageRef 以直接处理像素有什么意义?

ios - 通过 VPN 连接 Swift 发送所有流量

ios - EXC_BAD_ACCESS 从 Facebook 登录返回

ios - 在已经有 png 图像的 UIButton 中画线