我遇到了一个问题,我的视频资源UIWebView
没有加载。这只是实际设备上的一个问题 - 一切都在模拟器上完美运行。
注意:我已经检查过了,这里的问题与多次加载相同的内容有关。 在对 canInitWithRequest 的 YES 响应后不要求加载 NSURLProtocol
实际页面在我的自定义中加载得NSURLProtocol
很好,它YES
从canInitWithRequest
(几次)返回,然后startLoading
在大约第 4 次或第 5 次之后被调用。然后它以类似的方式加载我的 css 文件。然后我看到对我的视频对象的请求YES
从返回canInitWithRequest
,但没有任何反应。startLoading
从来没有要求我的视频资源的请求。这是我加载页面时设备上的简化日志:
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 的调试导航器中的网络活动是扁平的。
tl; dr
有谁知道为什么我的 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;