NSURLConnection+Https.m

//

//  NSURLConnection+Https.m

 

 

#import <objc/runtime.h>

#import “NSURLConnection+Https.h”

 

typedef void (^URLConnectionCompletionHandler)(NSURLResponse *response, NSData *data, NSError *error);

 

@interface URLConnectionDelegate : NSObject <NSURLConnectionDataDelegate>

 

@property (nonatomic, strong) NSURLResponse *response;

@property (nonatomic, strong) NSMutableData *data;

@property (nonatomic, strong) NSOperationQueue *queue;

@property (nonatomic, copy) URLConnectionCompletionHandler handler;

@property (nonatomic, strong) NSError *error;

@property (nonatomic, assign) BOOL isSynchronous;

 

@end

 

@implementation URLConnectionDelegate

 

– (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)theResponse {

    self.response = theResponse;

    [_datasetLength:0]; // reset data

}

 

– (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData {

    [_dataappendData:theData]; // append incoming data

}

 

– (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    [UIApplicationsharedApplication].networkActivityIndicatorVisible = NO;

    self.data = nil;

    _error = error;

    if (_handler) { [_queue addOperationWithBlock:^{ _handler(_response, nil, error); }]; }

    if (_isSynchronous) { CFRunLoopStop(CFRunLoopGetCurrent()); }

}

 

– (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    [UIApplicationsharedApplication].networkActivityIndicatorVisible = NO;

    if (_handler) { [_queueaddOperationWithBlock:^{ _handler(_response, _data, nil); }]; }

    if (_isSynchronous) { CFRunLoopStop(CFRunLoopGetCurrent()); }

}

 

 

– (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{

    [challenge.senderuseCredential:[NSURLCredentialcredentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

    [challenge.sendercontinueWithoutCredentialForAuthenticationChallenge:challenge];

}

 

– (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {

    return [protectionSpace.authenticationMethodisEqualToString:NSURLAuthenticationMethodServerTrust];

}

 

@end

 

 

@implementation NSURLConnection (Https)

 

+ (void)sendAsynchronousRequestHttps:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse *, NSData *, NSError *))handler {

        

    URLConnectionDelegate *connectionDelegate = [[URLConnectionDelegatealloc] init];

    connectionDelegate.data = [NSMutableData data];

    connectionDelegate.queue = queue;

    connectionDelegate.handler = handler;

    NSURLConnection *connection = [NSURLConnectionconnectionWithRequest:request

                                                                delegate:connectionDelegate];

    NSAssert(connection, nil);

}

 

+ (NSData *)sendSynchronousRequestHttps:(NSURLRequest *)request returningResponse:(NSURLResponse *__autoreleasing *)response error:(NSError *__autoreleasing *)error

{

    URLConnectionDelegate *connectionDelegate = [[URLConnectionDelegatealloc] init];

    connectionDelegate.data = [NSMutableData data];

    connectionDelegate.queue = [[NSOperationQueue alloc] init];

    NSURLConnection *connection = [NSURLConnectionconnectionWithRequest:request

                                                                delegate:connectionDelegate];

    NSAssert(connection, nil);

    

    connectionDelegate.isSynchronous = YES;

    

    [connection start];

    CFRunLoopRun();

    

    if (nil != response) {

        (*response) = connectionDelegate.response;

    }

    

    if (nil != error) {

        (*error) = connectionDelegate.error;

    }

    

    return connectionDelegate.data;

}

 

@end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s