WebView的自定义NSURLProtocol类在加载HTML5文档中的视频时不起作用 [英] Custom NSURLProtocol class for WebView doesn't work when loading video in HTML5 document

查看:517
本文介绍了WebView的自定义NSURLProtocol类在加载HTML5文档中的视频时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WebView加载其内容从某个位置像沙箱。所以我添加了NSURLProtocol的简单子类来处理这些文件。协议处理程序将管理URL方案像dummy:。当我尝试自定义url像dummy:///index.html,这应该从本地目录加载index.html。 Htmls和嵌入的图像等工作正常。

I'm working with WebView which loads its contents from certain location like sandbox. So I added simple child class of NSURLProtocol to handle those files. The protocol handler will manage URL scheme like "dummy:". When I tried custom url like dummy:///index.html, this should load index.html from a local directory. Htmls and embedded images etc. worked fine.

但是当我尝试html文件包括HTML5视频播放器使用标签,它不工作。 WebView甚至没有尝试方法canInitWithRequest:请求在我的自定义类的视频文件。

But when I tried an html file includes HTML5 video player using tag, it doesn't work. WebView even didn't tried the method canInitWithRequest:request in my custom class for the video file.

@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

我在applicationDidFinishLaunching中注册了协议处理程序:在AppDelegate.m

I registered the protocol handler in applicationDidFinishLaunching: in AppDelegate.m

if ([NSURLProtocol registerClass:[DummyURLProtocol class]]) {
    NSLog(@"URLProtocol registration successful.");
} else {
    NSLog(@"URLProtocol registration failed.");
}

然后我试过我的WebView与urldummy:/// HTML5_Video。 html。其他资源,如JavaScript文件,CSS文件,图像成功加载,但mp4文件没有传递到DummyURLProtocol。 HTML5_Video.html包括以下内容。

then I tried my WebView with the url "dummy:///HTML5_Video.html". Other resources like javascript files, css files, images are loaded successfully but mp4 file wasn't passed to the DummyURLProtocol. The HTML5_Video.html includes following.

  <video preload="metadata"> <!-- https://bugzilla.mozilla.org/show_bug.cgi?id=676422 -->
    <source src="assets/dizzy.mp4" type="video/mp4" />
    <source src="assets/dizzy.webm" type="video/webm" />
    <source src="assets/dizzy.ogv" type="video/ogv" />
  </video>

有任何想法或良好的出发点来解决这个问题吗?

Any ideas or good starting point to solve this problem?

谢谢。

推荐答案

Yup。

我在一年前与Apple提交了错误。

I've filed bugs with Apple over a year ago.

视频和其他多媒体类型(包括SVG)不会通过NSURL协议机制

Video and other multimedia types including SVG do not go through the NSURLProtocol mechanism

请参阅 http:// openradar.appspot.com/8446587

http:// openradar .appspot.com / 8692954

这篇关于WebView的自定义NSURLProtocol类在加载HTML5文档中的视频时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆