From 9ee9994d966fb8871590ea26ebc6c39ccc41b05b Mon Sep 17 00:00:00 2001 From: Ruslan Nikolaev Date: Tue, 14 Nov 2017 14:30:17 +0200 Subject: [PATCH] replaced NSUrlConnection with NSUrlSession --- OKSDK.m | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/OKSDK.m b/OKSDK.m index 434114e..36e3c7d 100644 --- a/OKSDK.m +++ b/OKSDK.m @@ -169,7 +169,7 @@ @interface OKConnection : NSObject @property(nonatomic,copy) NSString *oauthRedirectScheme; @property(nonatomic,copy) NSString *oauthRedirectUri; -@property(nonatomic,strong) NSOperationQueue *queue; +@property(nonatomic,strong) NSURLSession *urlSession; @property(nonatomic,weak) UIViewController *safariVC; @property(nonatomic,strong) NSString *accessToken; @@ -193,9 +193,14 @@ + (NSError *)sdkError:(NSInteger)code format:(NSString *)format, ... { - (instancetype)initWithSettings:(OKSDKInitSettings *)settings { if(self = [super init]) { _settings = settings; - _queue = [[NSOperationQueue alloc] init]; - _queue.name = @"OK-API-Requests"; - _queue.maxConcurrentOperationCount = OK_MAX_CONCURRENT_REQUESTS; + + NSOperationQueue *queue = [[NSOperationQueue alloc] init]; + queue.name = @"OK-API-Requests"; + queue.maxConcurrentOperationCount = OK_MAX_CONCURRENT_REQUESTS; + + NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; + _urlSession = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:nil delegateQueue:queue]; + _oauthRedirectScheme = [NSString stringWithFormat:@"ok%@", _settings.appId]; _oauthRedirectUri = [NSString stringWithFormat:@"%@://authorize", _oauthRedirectScheme]; _completitionHandlers = [NSMutableDictionary new]; @@ -298,10 +303,12 @@ - (void)invokeMethod:(NSString *)method arguments:(NSDictionary *)methodParams s NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:OK_REQUEST_TIMEOUT]; [request setValue:[NSString stringWithFormat:@"OK-IOS-SDK %@",OK_SDK_VERSION] forHTTPHeaderField:@"User-Agent"]; - [NSURLConnection sendAsynchronousRequest:request queue:self.queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { + [[self.urlSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { + if (error) { return errorBlock(error); } + NSError *jsonParsingError = nil; id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonParsingError]; if(jsonParsingError) { @@ -323,9 +330,7 @@ - (void)invokeMethod:(NSString *)method arguments:(NSDictionary *)methodParams s return successBlock(result); } return errorBlock([OKConnection sdkError:OKSDKErrorCodeBadApiReponse format:@"unknown api response: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]]); - - }]; - + }] resume]; } - (void)showWidget:(NSString *)command arguments:(NSDictionary *)arguments options:(NSDictionary *)options success:(OKResultBlock)successBlock error:(OKErrorBlock)errorBlock { @@ -342,7 +347,7 @@ - (void)showWidget:(NSString *)command arguments:(NSDictionary *)arguments optio } - (void)shutdown { - [self.queue cancelAllOperations]; + [self.urlSession invalidateAndCancel]; [self.safariVC dismissViewControllerAnimated:NO completion:nil]; } @@ -362,6 +367,10 @@ - (void)clearAuth { } } +-(void) dealloc { + [self.urlSession invalidateAndCancel]; +} + @end @implementation OKSDK