Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaced NSUrlConnection with NSUrlSession #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions OKSDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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];
}

Expand All @@ -362,6 +367,10 @@ - (void)clearAuth {
}
}

-(void) dealloc {
[self.urlSession invalidateAndCancel];
}

@end

@implementation OKSDK
Expand Down