gpt4 book ai didi

ios - NSURLProtocol - 未调用 startLoading

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:13:35 27 4
gpt4 key购买 nike

自升级到 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com