Tôi đang làm việc với WebView tải nội dung của nó từ vị trí nhất định như sandbox. Vì vậy, tôi đã thêm lớp con đơn giản của NSURLProtocol để xử lý các tệp đó. Trình xử lý giao thức sẽ quản lý lược đồ URL như "dummy:". Khi tôi thử url tùy chỉnh như dummy: ///index.html, điều này sẽ tải index.html từ một thư mục cục bộ. Html và hình ảnh nhúng, vv làm việc tốt.Lớp NSURLProtocol tùy chỉnh cho WebView không hoạt động khi tải video trong tài liệu HTML5
Nhưng khi tôi thử tệp html bao gồm trình phát video HTML5 sử dụng thẻ, nó không hoạt động. WebView thậm chí không thử phương thức canInitWithRequest: yêu cầu trong lớp tùy chỉnh của tôi cho tệp video.
@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
tôi đăng ký xử lý giao thức trong applicationDidFinishLaunching: trong AppDelegate.m
if ([NSURLProtocol registerClass:[DummyURLProtocol class]]) {
NSLog(@"URLProtocol registration successful.");
} else {
NSLog(@"URLProtocol registration failed.");
}
sau đó tôi đã cố gắng WebView của tôi với các url "giả: ///HTML5_Video.html". Các tài nguyên khác như tệp javascript, tệp css, hình ảnh được tải thành công nhưng tệp mp4 không được chuyển đến DummyURLProtocol. HTML5_Video.html bao gồm sau.
<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>
Bất kỳ ý tưởng hoặc điểm khởi đầu tốt nào để giải quyết vấn đề này?
Cảm ơn bạn.
Xin chào Tôi gặp vấn đề bằng cách sử dụng tùy chỉnh NSURLProtocol với WKWebView Class mới. Có ai cố gắng làm điều đó không? nó không tải các trang giả: //. –