- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
自升级到 iOS 9.1 后,我的自定义 NSURLProtocol
将不再调用 -(void)startLoading
。还有其他人遇到过这种情况吗?
在 iOS 8 上一切正常......
代码:
@implementation RZCustomProtocol
@dynamic request;
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
if ([request.URL.scheme isEqualToString:@"imsweb"]) {
NSLog(@"%@", @"YES");
return YES;
}
return NO;
}
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
return request;
}
+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b {
return [super requestIsCacheEquivalent:a toRequest:b];
}
- (void)startLoading {
NSLog(@"STARTLOADING: %@", [self.request.URL absoluteString]);
NSString *filename = [[self.request.URL lastPathComponent] stringByDeletingPathExtension];
NSLog(@"%@", filename);
NSString *videoUrl = [[NSBundle mainBundle] pathForResource:filename ofType:@"mp4"];
NSData *video = [NSData dataWithContentsOfFile:videoUrl];
NSLog(@"%lu", (unsigned long)video.length);
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.request.URL
statusCode:200 HTTPVersion:nil headerFields:@{
@"Content-Length": [NSString stringWithFormat:@"%lu", (unsigned long)video.length],
@"Content-Type": @"video/mp4",
}];
[self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[self.client URLProtocol:self didLoadData:video];
[self.client URLProtocolDidFinishLoading:self];
}
- (void)stopLoading {
NSLog(@"STOPLOADING: %@", [self.request.URL absoluteString]);
}
最佳答案
我遇到了同样的问题。在我的例子中,我使用 JavaScript 向页面动态添加了一个 iframe,并在其中加载了我的自定义协议(protocol)内容。在 iOS 9.1 中,当通过 https 访问主文档时,WebView
拒绝加载 iframe 内容,但它在 http 上工作正常。这看起来像是一种新的安全控制,以避免通过安全 session 加载不安全的资源。
我采用的修复方法是更改我的方案以使用 https
。例如,使用 https://imsweb/...
而不是 imsweb://
。这有点麻烦,但这是我能找到的最佳解决方案。
类似于:
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
if ([request.URL.scheme isEqualToString:@"https"] &&
[request.URL.host isEqualToString:@"imsweb"]) {
NSLog(@"%@", @"YES");
return YES;
}
return NO;
}
当然,您需要在 startLoading
中重建正确的 URL。
关于ios - NSURLProtocol - 未调用 startLoading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33318898/
自升级到 iOS 9.1 后,我的自定义 NSURLProtocol 将不再调用 -(void)startLoading。还有其他人遇到过这种情况吗? 在 iOS 8 上一切正常...... 代码:
突然之间,这对我不起作用: window.opener.startLoad(); self.close(); 我从 firebug 得到以下信息。 Permission denied to get
我正在查看 NSURLProtocol 并尝试插入一些 header 。 - (void)startLoading { NSMutableURLRequest *newRequest = [s
我正在 try catch 传出的 ajax 调用,以便通过向其添加客户端证书来更改请求。 看来可以通过子类化NSURLProtocol来实现。在 Phonegap 中已经有一个名为 CDVURLPr
我遇到了一个问题,我的 UIWebView 中的视频资源没有加载。这只是实际设备上的一个问题 - 一切都在模拟器上完美运行。 注意:我已经检查过了,这里的问题与多次加载相同的内容有关。 NSURLPr
我有我的自定义 URLProtocol,它有一些自定义响应生成。现在我有一个要求,我需要在获得实际响应之前通过 httpBody 向服务器发送一些信息(可以是 json/String 作为数据) 我的
我是一名优秀的程序员,十分优秀!