这真的很难弄清楚,但我想我已经成功地弄清楚了。例如,如果您尝试构建进度指示器,或者在加载页面的生命周期中的某个时间点执行某些操作,则此代码会很有用。
使用此代码,您现在可以查看正在下载的每个 URL,无论是图像、css 文件等。
@interface CustomURLProtocol : NSURLProtocol
@end
@implementation CustomURLProtocol
+ (void)enable
{
[NSURLProtocol registerClass:[CustomURLProtocol class]];
}
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
// Do something here with request.URL.absolutestring
return NO;
}
@end
现在,只需调用 [CustomURLProtocol enable]。