While playing with NSURLProtocol, I found [NSURLProtocol propertyForKey:inRequest:] always return nil in stopLoading, but work well for 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");
}
}
Anything wrong with my code?