在玩 NSURLProtocol 时,我发现[NSURLProtocol propertyForKey:inRequest:]在stopLoading中总是返回 nil ,但对canInitWithRequest 来说效果很好:
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
if ([NSURLProtocol propertyForKey:@"URLProtocolHandledKey" inRequest:request]) {
return NO;
}
return YES;
}
- (void)startLoading
{
NSMutableURLRequest *newRequest = [self.request mutableCopy];
[NSURLProtocol setProperty:@YES forKey:@"URLProtocolHandledKey" inRequest:newRequest];
}
- (void)stopLoading
{
if ([NSURLProtocol propertyForKey:@"URLProtocolHandledKey" inRequest:self.request]) {
NSLog(@"Logging");
}
}
我的代码有什么问题吗?