gpt4 book ai didi

objective-c - 在 HTML5 文档中加载视频时,WebView 的自定义 NSURLProtocol 类不起作用

转载 作者:太空狗 更新时间:2023-10-30 03:20:42 26 4
gpt4 key购买 nike

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

但是当我尝试使用标签包含 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

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

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

然后我用 url“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>

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

谢谢。

最佳答案

是的。这是行不通的。

我在一年多前就向 Apple 提交了错误。

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

参见 http://openradar.appspot.com/8446587

http://openradar.appspot.com/8692954

关于objective-c - 在 HTML5 文档中加载视频时,WebView 的自定义 NSURLProtocol 类不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8062871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com