我遇到了一个问题,我的 UIWebView
中的视频资源没有加载。这只是实际设备上的一个问题 - 一切都在模拟器上完美运行。
注意:我已经检查过了,这里的问题与多次加载相同的内容有关。 NSURLProtocol isn't asked to load after YES response to canInitWithRequest
实际页面加载正常 - 在我的自定义 NSURLProtocol
中,它从 canInitWithRequest
返回 YES
(多次)然后 startLoading
在大约第 4 次或第 5 次之后被调用。然后它以类似的方式加载我的 css 文件。然后我看到对我的视频对象的请求从 canInitWithRequest
返回了 YES
,但它没有任何反应。 startLoading
永远不会为我的视频资源请求调用。这是我加载页面时来 self 的设备的简化日志:
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 版本的模拟器加载完全相同的页面时,日志显示资源实际上由于某种原因从此处开始加载:
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 的调试导航器中的网络事件是扁平的。
长话短说
有谁知道为什么我的 iPhone 从未尝试加载此资源?显然模拟器知道它需要加载。
编辑:这是我的 NSURLProtocol 中经过轻微编辑的相关代码
@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;
最佳答案
我最好的猜测是 UIWebView
中使用的视频播放器没有使用通过管道传输的基础服务(NSURLSession/NSURLRequest
等)加载它的内容 NSUR 协议(protocol)
。视频播放器可能正在使用较低级别的 CFNetwork
api 发出请求。
实现可能因模拟器而异。
我知道你的协议(protocol)是通过 canInitWithRequest
调用进行轮询的,但这并不一定意味着播放器将使用 NSURLSession/NSURLRequest
实际加载资源>。
尝试通过自定义协议(protocol)将视频加载到 MPMoviePlayerViewController 中的行为与您所看到的类似:How to play movie with a URL using a custom NSURLProtocol?
关于ios - NSURLProtocol - canInitWithRequest 为页面资源返回 YES 后未调用 startLoading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33089318/