Add NSURLSession support (BITAuthenticator)

This commit is contained in:
Christoph Wendt 2015-09-02 13:35:59 -07:00
parent 37727b46f4
commit dd101d0e59

View File

@ -447,34 +447,53 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44};
NSParameterAssert(email && email.length); NSParameterAssert(email && email.length);
NSParameterAssert(self.identificationType == BITAuthenticatorIdentificationTypeHockeyAppEmail || (password && password.length)); NSParameterAssert(self.identificationType == BITAuthenticatorIdentificationTypeHockeyAppEmail || (password && password.length));
NSURLRequest* request = [self requestForAuthenticationEmail:email password:password]; NSURLRequest* request = [self requestForAuthenticationEmail:email password:password];
__weak typeof (self) weakSelf = self; __weak typeof (self) weakSelf = self;
id nsurlsessionClass = NSClassFromString(@"NSURLSessionUploadTask");
if (nsurlsessionClass && !bit_isRunningInAppExtension()) {
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
typeof (self) strongSelf = weakSelf;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
[strongSelf handleAuthenticationWithResponse:httpResponse email:email data:data completion:completion];
}];
[task resume];
}else{
BITHTTPOperation *operation = [self.hockeyAppClient operationWithURLRequest:request BITHTTPOperation *operation = [self.hockeyAppClient operationWithURLRequest:request
completion:^(BITHTTPOperation *operation, NSData* responseData, NSError *error) { completion:^(BITHTTPOperation *operation, NSData* responseData, NSError *error) {
typeof (self) strongSelf = weakSelf; typeof (self) strongSelf = weakSelf;
[strongSelf handleAuthenticationWithResponse:operation.response email:email data:responseData completion:completion];
}];
[self.hockeyAppClient enqeueHTTPOperation:operation];
}
}
- (void)handleAuthenticationWithResponse:(NSHTTPURLResponse *)response email:(NSString *)email data:(NSData *)data completion:(void (^)(BOOL, NSError *))completion{
NSError *authParseError = nil; NSError *authParseError = nil;
NSString *authToken = [strongSelf.class authenticationTokenFromURLResponse:operation.response NSString *authToken = [self.class authenticationTokenFromURLResponse:response
data:responseData data:data
error:&authParseError]; error:&authParseError];
BOOL identified; BOOL identified;
if(authToken) { if(authToken) {
identified = YES; identified = YES;
[strongSelf storeInstallationIdentifier:authToken withType:strongSelf.identificationType]; [self storeInstallationIdentifier:authToken withType:self.identificationType];
[strongSelf dismissAuthenticationControllerAnimated:YES completion:nil]; [self dismissAuthenticationControllerAnimated:YES completion:nil];
strongSelf->_authenticationController = nil; self->_authenticationController = nil;
BOOL success = [self addStringValueToKeychain:email forKey:kBITAuthenticatorUserEmailKey]; BOOL success = [self addStringValueToKeychain:email forKey:kBITAuthenticatorUserEmailKey];
if (!success) { if (!success) {
[strongSelf alertOnFailureStoringTokenInKeychain]; [self alertOnFailureStoringTokenInKeychain];
} }
} else { } else {
identified = NO; identified = NO;
} }
strongSelf.identified = identified; self.identified = identified;
completion(identified, authParseError); completion(identified, authParseError);
if(strongSelf.identificationCompletion) strongSelf.identificationCompletion(identified, authParseError); if(self.identificationCompletion) self.identificationCompletion(identified, authParseError);
strongSelf.identificationCompletion = nil; self.identificationCompletion = nil;
}];
[self.hockeyAppClient enqeueHTTPOperation:operation];
} }
- (NSURLRequest *) requestForAuthenticationEmail:(NSString*) email password:(NSString*) password { - (NSURLRequest *) requestForAuthenticationEmail:(NSString*) email password:(NSString*) password {