我正在使用WebView,它从沙盒等特定位置加载其内容.所以我添加了NSURLProtocol的简单子类来处理这些文件.协议处理程序将管理URL方案,如"dummy:".当我尝试像dummy:///index.html这样的自定义URL时,这应该从本地目录加载index.html.Htmls和嵌入式图像等工作正常.
但是当我尝试使用包含HTML5视频播放器的html文件时,它不起作用.WebView甚至没有尝试过canInitWithRequest方法:在我的自定义类中请求视频文件.
@interface DummyURLProtocol : NSURLProtocol { } + (BOOL)canInitWithRequest:(NSURLRequest *)request; + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request; + (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b; - (void)startLoading; - (void)stopLoading; @end @implementation DummyURLProtocol +(BOOL)canInitWithRequest:(NSURLRequest *)request { return [[[request URL] scheme] isEqualToString:@"dummy"]; } +(NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request { return request; } +(BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b { return [[[a URL] resourceSpecifier] isEqualToString:[[b URL] resourceSpecifier]]; } -(void)startLoading { NSURL *url = [[self request] URL]; NSString *pathString = [url resourceSpecifier]; NSString *path = [NSString stringWithFormat:@"/Users/cronos/tmp/video_demo/%@", pathString]; NSString *fullFilename = [pathString lastPathComponent]; NSString *extention = [fullFilename pathExtension]; NSString *mimeType = [[SSGHTMLUtil sharedUtil] mimeTypeForExtension:extention]; NSLog(@"DummyURLProtocol:FILEPATH: %@ EXTENSION: %@ MIME-TYPE: %@", path, extention, mimeType); NSURLResponse *response = [[NSURLResponse alloc] initWithURL:url MIMEType:mimeType expectedContentLength:-1 textEncodingName:nil]; FILE *fp = fopen([path UTF8String], "r"); if (fp) { char buf[32768]; size_t len; [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; while ((len = fread(buf,1,sizeof(buf),fp))) { [[self client] URLProtocol:self didLoadData:[NSData dataWithBytes:buf length:len]]; } fclose(fp); } [[self client] URLProtocolDidFinishLoading:self]; } -(void)stopLoading { } @end
我在AppDlegate.m中的applicationDidFinishLaunching中注册了协议处理程序
if ([NSURLProtocol registerClass:[DummyURLProtocol class]]) { NSLog(@"URLProtocol registration successful."); } else { NSLog(@"URLProtocol registration failed."); }
然后我用网址"dummy:///HTML5_Video.html"尝试了我的WebView.其他资源如javascript文件,css文件,图像都已成功加载,但mp4文件未传递给DummyURLProtocol.HTML5_Video.html包含以下内容.
有什么想法或好的出发点来解决这个问题?
谢谢.
对.它不会起作用.
我在一年多前就向苹果公司提出了漏洞.
视频和其他多媒体类型(包括SVG)不通过NSURLProtocol机制
见http://openradar.appspot.com/8446587
和http://openradar.appspot.com/8692954