From 862771437ce159fc92e5b82779c71b132aba970a Mon Sep 17 00:00:00 2001 From: Christoph Wendt Date: Tue, 1 Sep 2015 17:54:21 -0700 Subject: [PATCH] Implement NSURLSessionTaskDelegate methods (BITUpdateManager) --- Classes/BITUpdateManager.m | 41 +++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/Classes/BITUpdateManager.m b/Classes/BITUpdateManager.m index b29ccf4497..7266de9def 100644 --- a/Classes/BITUpdateManager.m +++ b/Classes/BITUpdateManager.m @@ -1155,6 +1155,7 @@ typedef NS_ENUM(NSInteger, BITUpdateAlertViewTag) { return newRequest; } + - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { if ([response respondsToSelector:@selector(statusCode)]) { NSInteger statusCode = [((NSHTTPURLResponse *)response) statusCode]; @@ -1183,12 +1184,51 @@ typedef NS_ENUM(NSInteger, BITUpdateAlertViewTag) { // api call returned, parsing - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [self finishLoading]; +} + +#pragma mark - NSURLSession + +- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { + dispatch_async(dispatch_get_main_queue(), ^{ + if(error){ + [self handleError:error]; + }else{ + [self finishLoading]; } + }); +} + +- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data { + [_receivedData appendData:data]; +} + +- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler { + + if ([response respondsToSelector:@selector(statusCode)]) { + NSInteger statusCode = [((NSHTTPURLResponse *)response) statusCode]; + if (statusCode == 404) { + [dataTask cancel]; + NSString *errorStr = [NSString stringWithFormat:@"Hockey API received HTTP Status Code %ld", (long)statusCode]; + [self reportError:[NSError errorWithDomain:kBITUpdateErrorDomain + code:BITUpdateAPIServerReturnedInvalidStatus + userInfo:[NSDictionary dictionaryWithObjectsAndKeys:errorStr, NSLocalizedDescriptionKey, nil]]]; + completionHandler(NSURLSessionResponseCancel); + return; } + completionHandler(NSURLSessionResponseAllow); } + self.receivedData = [NSMutableData data]; + [_receivedData setLength:0]; +} + +- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *))completionHandler { + NSURLRequest *newRequest = request; + if (response) { + newRequest = nil; } + completionHandler(newRequest); } - (BOOL)hasNewerMandatoryVersion { @@ -1207,7 +1247,6 @@ typedef NS_ENUM(NSInteger, BITUpdateAlertViewTag) { return result; } - #pragma mark - Properties - (void)setCurrentHockeyViewController:(BITUpdateViewController *)aCurrentHockeyViewController {