-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathARTURLSessionServerTrust.m
54 lines (45 loc) · 1.88 KB
/
ARTURLSessionServerTrust.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// ARTURLSessionServerTrust.m
// ably
//
// Created by Ricardo Pereira on 20/11/15.
// Copyright © 2015 Ably. All rights reserved.
//
#import "ARTURLSessionServerTrust.h"
@interface ARTURLSessionServerTrust() {
NSURLSession *_session;
dispatch_queue_t _queue;
}
@end
@implementation ARTURLSessionServerTrust
- (instancetype)init:(dispatch_queue_t)queue {
if (self = [super init]) {
_session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
_queue = queue;
}
return self;
}
- (void)finishTasksAndInvalidate {
[_session finishTasksAndInvalidate];
}
- (NSObject<ARTCancellable> *)get:(NSURLRequest *)request completion:(void (^)(NSHTTPURLResponse *_Nullable, NSData *_Nullable, NSError *_Nullable))callback {
NSURLSessionDataTask *task = [_session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_async(self->_queue, ^{
callback((NSHTTPURLResponse *)response, data, error);
});
}];
[task resume];
return task;
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
if (challenge.protectionSpace.serverTrust) {
completionHandler(NSURLSessionAuthChallengeUseCredential, [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust]);
}
else if ([challenge.sender respondsToSelector:@selector(performDefaultHandlingForAuthenticationChallenge:)]) {
[challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];
}
else {
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
}
}
@end