3

Since upgrading to iOS 9.1 my custom NSURLProtocol, won't invoke -(void)startLoading anymore. Has anyone else experienced this ?

Everything worked fine on iOS 8 ...

Code:

@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]);
}
4

1 回答 1

1

我遇到过同样的问题。就我而言,我正在使用 JavaScript 向页面动态添加 iframe,并在其中加载我的自定义协议内容。在 iOS 9.1 中,WebView当通过 https 访问主文档时拒绝加载 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.

于 2015-11-17T23:50:28.537 回答