Created
September 17, 2013 12:55
-
-
Save odrobnik/6593951 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation DTProgressReportingURLProtocol | |
{ | |
NSURLConnection *_connection; | |
dispatch_semaphore_t _semaphore; | |
} | |
+ (BOOL)canInitWithRequest:(NSURLRequest *)request | |
{ | |
NSLog(@"%@", [self propertyForKey:@"UserAgentSet" inRequest:request]); | |
if ([[self propertyForKey:@"UserAgentSet" inRequest:request] boolValue]) | |
{ | |
return NO; | |
} | |
return YES; | |
} | |
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request | |
{ | |
return request; | |
} | |
+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a | |
toRequest:(NSURLRequest *)b | |
{ | |
return [a.URL isEqual:b.URL]; | |
} | |
- (void)startLoading | |
{ | |
NSLog(@"%s %@", __PRETTY_FUNCTION__, self.request.URL); | |
NSMutableURLRequest *mutableRequest = [self.request mutableCopy]; | |
[NSURLProtocol setProperty:@YES forKey:@"UserAgentSet" inRequest:mutableRequest]; | |
_semaphore = dispatch_semaphore_create(0); | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ | |
// startNext downloading | |
_connection = [[NSURLConnection alloc] initWithRequest:mutableRequest delegate:self startImmediately:NO]; | |
// without this special it would get paused during scrolling of scroll views | |
[_connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; | |
//[_connection setDelegateQueue:[NSOperationQueue mainQueue]]; | |
[_connection start]; | |
}); | |
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER); | |
} | |
- (void)stopLoading | |
{ | |
NSLog(@"%s %@", __PRETTY_FUNCTION__, self.request.URL); | |
[_connection cancel]; | |
_connection = nil; | |
} | |
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data | |
{ | |
NSLog(@"%s %@", __PRETTY_FUNCTION__, self.request.URL); | |
[self.client URLProtocol:self didLoadData:data]; | |
} | |
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error | |
{ | |
NSLog(@"%s %@", __PRETTY_FUNCTION__, self.request.URL); | |
[self.client URLProtocol:self didFailWithError:error]; | |
_connection = nil; | |
dispatch_semaphore_signal(_semaphore); | |
} | |
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response | |
{ | |
NSLog(@"%s %@", __PRETTY_FUNCTION__, self.request.URL); | |
[self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed]; | |
} | |
- (void)connectionDidFinishLoading:(NSURLConnection *)connection | |
{ | |
NSLog(@"%s %@", __PRETTY_FUNCTION__, self.request.URL); | |
[self.client URLProtocolDidFinishLoading:self]; | |
_connection = nil; | |
dispatch_semaphore_signal(_semaphore); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment