NSURLProtocol - 未调用 startLoading
NSURLProtocol - startLoading not invoked
自从升级到 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,并在其中加载了我的自定义协议内容。在 iOS 9.1 中,当通过 https 访问主文档时,WebView
拒绝加载 iframe 内容,但它在 http 上工作正常。这看起来像一个新的安全控制,以避免在安全会话中加载不安全的资源。
我采用的修复方法是更改我的方案以使用 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 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,并在其中加载了我的自定义协议内容。在 iOS 9.1 中,当通过 https 访问主文档时,WebView
拒绝加载 iframe 内容,但它在 http 上工作正常。这看起来像一个新的安全控制,以避免在安全会话中加载不安全的资源。
我采用的修复方法是更改我的方案以使用 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。