【问题标题】:Custom NSURLProtocol class for WebView doesn't work when loading video in HTML5 document在 HTML5 文档中加载视频时,WebView 的自定义 NSURLProtocol 类不起作用
【发布时间】:2011-11-09 09:30:47
【问题描述】:

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

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

@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 中注册了协议处理程序

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 包括以下内容。

  <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>

有解决这个问题的想法或好的起点吗?

谢谢。

【问题讨论】:

  • 您好,我在使用带有新 WKWebView 类的自定义 NSURLProtocol 时遇到问题。有没有人试图这样做?它不会加载 dummy:// 页面。

标签: objective-c macos cocoa webkit


【解决方案1】:

是的。它不会工作。

一年多前我已经向 Apple 提交了错误。

视频和包括 SVG 在内的其他多媒体类型不通过 NSURLProtocol 机制

http://openradar.appspot.com/8446587

http://openradar.appspot.com/8692954

【讨论】:

  • 如何使用 http: 方案但使用自定义标头?
  • 我遇到了同样的障碍。我不得不使用嵌入式网络服务器 (CocoaHTTPServer) 为应用程序提供多媒体文件(从应用程序内部)
  • 4年后还是这样吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-14
  • 1970-01-01
  • 2013-04-03
  • 2019-07-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多