WebViewを使用して、サンドボックスのような特定の場所からコンテンツをロードしています。そこで、NSURLProtocolの単純な子クラスを追加してそれらのファイルを処理しました。プロトコルハンドラは "dummy:"のようなURLスキームを管理します。ダミー:///index.htmlのようなカスタムURLを試したとき、ローカルディレクトリからindex.htmlをロードする必要があります。 HTMLや埋め込み画像などはうまくいきました。WebViewのカスタムNSURLProtocolクラスがHTML5ドキュメントにビデオをロードするときに機能しません
しかし、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
私はapplicationDidFinishLaunchingでのプロトコルハンドラを登録:AppDelegate.m
if ([NSURLProtocol registerClass:[DummyURLProtocol class]]) {
NSLog(@"URLProtocol registration successful.");
} else {
NSLog(@"URLProtocol registration failed.");
}
に、私は、URL "ダミー:///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>
この問題を解決するためのアイデアや良い出発点はありますか?
ありがとうございます。
を参照してください。誰かそれをしようとしましたか?それはダミー://ページをロードしません。 –