-
-
Save anivaros/10273876 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
#import <Foundation/Foundation.h> | |
@interface URLConnection : NSObject <NSURLConnectionDataDelegate, NSURLConnectionDelegate> | |
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request | |
returningResponse:(NSURLResponse **)response | |
error:(NSError **)error; | |
@end |
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
#import "URLConnection.h" | |
@interface URLConnection () | |
@property (nonatomic, strong) NSURLConnection *connection; | |
@property (nonatomic, strong) NSURLCredential *credential; | |
@property (nonatomic, strong) NSURLResponse *response; | |
@property (atomic, strong) NSMutableData *responseData; | |
@property (nonatomic, strong) NSCondition *condition; | |
@property (nonatomic, strong) NSError *error; | |
@property (nonatomic) BOOL connectionDidFinishLoading; | |
@end | |
@implementation URLConnection | |
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error credential:(NSURLCredential *)credential { | |
return [[[URLConnection alloc] init] sendSynchronousRequest:request returningResponse:response error:error credential:credential]; | |
} | |
- (id)init { | |
self = [super init]; | |
if (self) { | |
self.condition = [[NSCondition alloc] init]; | |
self.connection = nil; | |
self.connectionDidFinishLoading = NO; | |
self.error = nil; | |
self.response = nil; | |
self.responseData = [[NSMutableData alloc] init]; | |
self.credential = nil; | |
} | |
return self; | |
} | |
- (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error credential:(NSURLCredential *)credential { | |
NSParameterAssert(request); | |
NSAssert(!self.connection, @"This method may only be called once"); | |
self.credential = credential; | |
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; | |
[self.connection setDelegateQueue:[[NSOperationQueue alloc] init]]; | |
[self.connection start]; | |
[self waitForConnectionToFinishLoading]; | |
if (self.error != nil) { | |
if (response) *response = nil; | |
if (error) *error = self.error; | |
return nil; | |
} | |
else { | |
if (response) *response = self.response; | |
if (error) *error = nil; | |
return self.responseData; | |
} | |
} | |
- (void)waitForConnectionToFinishLoading { | |
[self.condition lock]; | |
while (!self.connectionDidFinishLoading) { | |
[self.condition wait]; | |
} | |
[self.condition unlock]; | |
} | |
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { | |
self.response = response; | |
} | |
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { | |
[self.responseData appendData:data]; | |
} | |
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { | |
[self.condition lock]; | |
self.error = error; | |
self.connectionDidFinishLoading = YES; | |
[self.condition signal]; | |
[self.condition unlock]; | |
} | |
- (void)connectionDidFinishLoading:(NSURLConnection *)connection { | |
[self.condition lock]; | |
self.connectionDidFinishLoading = YES; | |
[self.condition signal]; | |
[self.condition unlock]; | |
} | |
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { | |
[[challenge sender] useCredential:self.credential forAuthenticationChallenge:challenge]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment