- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 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 机制
关于objective-c - 在 HTML5 文档中加载视频时,WebView 的自定义 NSURLProtocol 类不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8062871/
我实现了一个自定义 NSURLProtocol,它允许我使用网站的静态压缩版本作为 webView 的目标。它可以随时打开 zip 并加载所需的数据。但问题是 NSURLProtocol 似乎无法正确
我正在通过“http://”和“https://”进行通信。何时以及为何使用 NSURLProtocol? 我读到它用于新的/自定义方案,对于上述方案我们不需要使用 NSURLProtocol。 最佳
我正在尝试创建 NSURLProtocol 的子类。但是,我注意到当我注册我的子类时我似乎失去了缓存功能,即使它不应该做任何事情。例如,如果我对同一个 URL 多次执行 [NSURLConnectio
我在通过 NSURLProtocol 子类中的特殊协议(protocol)方案处理视频请求时遇到了一些问题。所有其他资源(图像/文本)都得到了正确处理,但是,当发送视频请求时,我只收到对“canIni
我一直在我的应用程序中使用自定义 NSURLProtocol 来检测资源并从缓存目录加载(如果可用),或者重定向回我的应用程序的服务器。但是,我发现缺少有关抽象方法的文档,而且我不确定如何处理我们需要
我已经编写了 NSURLProtocol 的自定义子类,但是似乎每当我使用该协议(protocol)在 UIWebView 中加载请求时,它都会假设数据是内容类型“text/html”。有没有办法指定
我想加载远程url并使用本地资源(image,css,js...),所以我继承了NSURLProtocol。 import Foundation class MyURLProtocol: NSURLP
我正在尝试在我的应用程序中实现一个 NSURLProtocol,它将获取以 myApp://... 开头的 URL 我在新的 SWIFT 文件中创建了协议(protocol)并在 AppDelegat
我的应用程序中的 UIWebviews 和一些自定义 NSURLProtocols 存在问题。 我所有的非 web View 请求都是用 NSURLSession 调用的,所以为了让这些请求通过协议(
确定当前在应用程序中注册的所有 NSURLProtocol 子类的正确方法是什么? In the docs ,我只看到 + (BOOL)registerClass:(Class)protocolCla
自升级到 iOS 9.1 后,我的自定义 NSURLProtocol 将不再调用 -(void)startLoading。还有其他人遇到过这种情况吗? 在 iOS 8 上一切正常...... 代码:
我的应用程序使用 NSURLProtocol 的子类。应用程序中有多个 UIWebView,对于 NSURLProtocol 中实现的特定算法,我需要知道哪一个 UIWebView 发送了请求。 我的
我想使用自定义 NSURLProtocol类为测试提供静态数据,我已经在应用程序本身中实现了协议(protocol)并将其注册到 NSURLSessionConfiguration.protocolC
我使用 NSURLProtocol 为所有从 UIWebview 发出的请求添加自定义 header 。 在此 WebView 上执行某些操作时,会触发 AJAX 调用以显示消息。此显示消息的操作使用
我正在使用 MonoTouch 构建 iOS 应用程序并尝试实现自定义 NSUrlProtocol。 当我注册自己的协议(protocol)实现时,出现以下错误: WebKit discarded a
首先是服务器端:有一个内部可访问的 apache 服务器,带有多个虚拟主机。对于 http(无 's'!) 请求,我使用 IP 作为 URL,并在 Host-Header 字段中添加主机名。 效果非常
这是NSURLProtocol中的canInitWithRequest: + (BOOL)canInitWithRequest:(NSURLRequest *)request { // only
我已经将 NSURLProtocol 子类化并使用以下代码在 didFinishLaunchingWithOptions 中注册它: + (void) registerProtocol { st
我已经实现了 NSURLProtocol 的子类。我需要它来模拟单元测试的 URL 响应。我已在单元测试的 setUp 方法中注册了我的类。 - (void)setUp { [NSURLPro
我尝试按照以下教程中的说明实现 NSURLProtocol:http://www.raywenderlich.com/76735/using-nsurlprotocol-swift 在 iOS8 中一
我是一名优秀的程序员,十分优秀!