自从升级到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]);
}发布于 2015-11-17 23:50:28
我也有过同样的问题。在我的例子中,我使用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。
https://stackoverflow.com/questions/33318898
复制相似问题