注意:我已经检查了这个问题,其中的问题与多次加载相同的内容有关.
NSURLProtocol isn’t asked to load after YES response to canInitWithRequest
实际页面在我的自定义NSURLProtocol中加载很好,它从canInitWithRequest返回YES(几次),然后在大约第4或第5次后调用startLoading.然后它以类似的方式加载我的css文件.然后我看到我的视频对象的请求从canInitWithRequest返回YES,但没有任何反应.永远不会为我的视频资源请求调用startLoading.这是我加载页面时设备的简化日志:
@H_502_7@canInitWithRequest: https://WebpageURL canInitWithRequest: https://WebpageURL canInitWithRequest: https://WebpageURL canInitWithRequest: https://WebpageURL canInitWithRequest: https://WebpageURL START LOADING: https://WebpageURL canInitWithRequest: https://WebpageURL canInitWithRequest: https://WebpageURL canInitWithRequest: https://WebpageURL canInitWithRequest: https://cssResource START LOADING: https://cssResource canInitWithRequest: https://MyVideoResource当我从完全相同的设备类型运行相同iOS版本的模拟器加载完全相同的页面时,日志显示资源实际上由于某种原因开始加载:
@H_502_7@canInitWithRequest: https://WebpageURL canInitWithRequest: https://WebpageURL canInitWithRequest: https://WebpageURL START LOADING: https://WebpageURL canInitWithRequest: https://WebpageURL canInitWithRequest: https://cssResource START LOADING: https://cssResource canInitWithRequest: https://MyVideoResource canInitWithRequest: https://MyVideoResource START LOADING: https://MyVideoResource -- WHY NOT ON DEVICE?该页面留下了一个永远不会播放的视频 – 只有一个白色的盒子,它上面有“播放视频”按钮.页面看起来像是不断加载某些东西(状态栏中的活动指示器正在旋转)但没有发生任何事情,并且Xcode的Debug导航器中的网络活动是扁平化的.
TL;博士
有谁知道为什么从未在我的iPhone上尝试加载此资源?显然,模拟器知道它需要加载.
编辑:这是来自我的NSURLProtocol的略微编辑的相关代码
@H_502_7@@implementation CHHTTPURLProtocol { NSURLConnection *connection; NSHTTPURLResponse *httpResponse; NSData *responseData; } + (BOOL)canInitWithRequest:(NSURLRequest *)request { if (/* key exists on request */) return NO; else if (/* not http or https */) return NO; NSLog(@"canInitWithRequest: %@",request.URL.absoluteString); return YES; } + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request { return request; } - (void)startLoading { NSMutableURLRequest *request = [self.request mutableCopy]; NSLog(@"START LOADING: %@",request.URL.absoluteString); if (authenticate) authenticate(request); /* add key to request */ connection = [NSURLConnection connectionWithRequest:request delegate:self]; } - (void)stopLoading { [connection cancel]; connection = nil; } /* Other methods like connection:didReceiveData: and connectionDidFinishLoding: */ + (void)authenticateWithBlock:(AuthenticationBlock)block { authenticate = block; } static AuthenticationBlock authenticate;解决方法
实现可能因模拟器而异.
我得到你的协议使用canInitWithRequest调用进行轮询,但这并不一定意味着玩家将使用NSURLSession / NSURLRequest实际加载资源.
尝试通过自定义协议将视频加载到MPMoviePlayerViewController中的行为与您所看到的类似:How to play movie with a URL using a custom NSURLProtocol?