Browse Source

#29 add IOS implementation for self-signed server

Ben Hsieh 8 years ago
parent
commit
af3c86a78d
4 changed files with 70 additions and 31 deletions
  1. 1
    0
      src/ios/RNFetchBlobConst.h
  2. 2
    0
      src/ios/RNFetchBlobConst.m
  3. 17
    20
      src/ios/RNFetchBlobNetwork.h
  4. 50
    11
      src/ios/RNFetchBlobNetwork.m

+ 1
- 0
src/ios/RNFetchBlobConst.h View File

22
 extern NSString *const CONFIG_USE_TEMP;
22
 extern NSString *const CONFIG_USE_TEMP;
23
 extern NSString *const CONFIG_FILE_PATH;
23
 extern NSString *const CONFIG_FILE_PATH;
24
 extern NSString *const CONFIG_FILE_EXT;
24
 extern NSString *const CONFIG_FILE_EXT;
25
+extern NSString *const CONFIG_TRUSTY;
25
 
26
 
26
 // fs events
27
 // fs events
27
 extern NSString *const FS_EVENT_DATA;
28
 extern NSString *const FS_EVENT_DATA;

+ 2
- 0
src/ios/RNFetchBlobConst.m View File

13
 extern NSString *const CONFIG_USE_TEMP = @"fileCache";
13
 extern NSString *const CONFIG_USE_TEMP = @"fileCache";
14
 extern NSString *const CONFIG_FILE_PATH = @"path";
14
 extern NSString *const CONFIG_FILE_PATH = @"path";
15
 extern NSString *const CONFIG_FILE_EXT = @"appendExt";
15
 extern NSString *const CONFIG_FILE_EXT = @"appendExt";
16
+extern NSString *const CONFIG_TRUSTY = @"trusty";
17
+
16
 
18
 
17
 extern NSString *const MSG_EVENT = @"RNFetchBlobMessage";
19
 extern NSString *const MSG_EVENT = @"RNFetchBlobMessage";
18
 extern NSString *const MSG_EVENT_LOG = @"log";
20
 extern NSString *const MSG_EVENT_LOG = @"log";

+ 17
- 20
src/ios/RNFetchBlobNetwork.h View File

12
 #import <Foundation/Foundation.h>
12
 #import <Foundation/Foundation.h>
13
 #import "RCTBridgeModule.h"
13
 #import "RCTBridgeModule.h"
14
 
14
 
15
-@interface RNFetchBlobNetwork : NSObject  <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate> {
16
-    
17
-    NSString * taskId;
18
-    int expectedBytes;
19
-    int receivedBytes;
20
-    NSMutableData * respData;
21
-    RCTResponseSenderBlock callback;
22
-    RCTBridge * bridge;
23
-    NSDictionary * options;
24
-    RNFetchBlobFS * fileStream;
25
-}
26
-@property (nonatomic) NSString * taskId;
15
+typedef void(^CompletionHander)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error);
16
+typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error);
17
+
18
+@interface RNFetchBlobNetwork : NSObject  <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate, UIApplicationDelegate>
19
+
20
+@property (nullable, nonatomic) NSString * taskId;
27
 @property (nonatomic) int expectedBytes;
21
 @property (nonatomic) int expectedBytes;
28
 @property (nonatomic) int receivedBytes;
22
 @property (nonatomic) int receivedBytes;
29
-@property (nonatomic) NSMutableData * respData;
30
-@property (nonatomic) RCTResponseSenderBlock callback;
31
-@property (nonatomic) RCTBridge * bridge;
32
-@property (nonatomic) NSDictionary * options;
33
-@property (nonatomic) RNFetchBlobFS * fileStream;
23
+@property (nullable, nonatomic) NSMutableData * respData;
24
+@property (nullable, nonatomic) RCTResponseSenderBlock callback;
25
+@property (nullable, nonatomic) RCTBridge * bridge;
26
+@property (nullable, nonatomic) NSDictionary * options;
27
+@property (nullable, nonatomic) RNFetchBlobFS * fileStream;
28
+@property (nullable, nonatomic) CompletionHander fileTaskCompletionHandler;
29
+@property (nullable, nonatomic) DataTaskCompletionHander dataTaskCompletionHandler;
30
+@property (nullable, nonatomic) NSError * error;
34
 
31
 
35
 
32
 
36
-- (id) init;
33
+- (nullable id) init;
37
 - (void) sendRequest;
34
 - (void) sendRequest;
38
 
35
 
39
-+ (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers;
40
-- (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskId:(NSString *)taskId withRequest:(NSURLRequest *)req callback:(RCTResponseSenderBlock) callback;
36
++ (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
37
+- (void) sendRequest:(NSDictionary  * _Nullable )options bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
41
 
38
 
42
 
39
 
43
 @end
40
 @end

+ 50
- 11
src/ios/RNFetchBlobNetwork.m View File

23
 
23
 
24
 @implementation RNFetchBlobNetwork
24
 @implementation RNFetchBlobNetwork
25
 
25
 
26
+NSOperationQueue *taskQueue;
26
 
27
 
27
 @synthesize taskId;
28
 @synthesize taskId;
28
 @synthesize expectedBytes;
29
 @synthesize expectedBytes;
31
 @synthesize callback;
32
 @synthesize callback;
32
 @synthesize bridge;
33
 @synthesize bridge;
33
 @synthesize options;
34
 @synthesize options;
35
+@synthesize fileTaskCompletionHandler;
36
+@synthesize dataTaskCompletionHandler;
37
+@synthesize error;
38
+
34
 
39
 
35
 // constructor
40
 // constructor
36
 - (id)init {
41
 - (id)init {
37
     self = [super init];
42
     self = [super init];
43
+    if(taskQueue == nil) {
44
+        taskQueue = [[NSOperationQueue alloc] init];
45
+    }
38
     return self;
46
     return self;
39
 }
47
 }
40
 
48
 
51
 }
59
 }
52
 
60
 
53
 // send HTTP request
61
 // send HTTP request
54
-- (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskId:(NSString *)taskId withRequest:(NSURLRequest *)req callback:(RCTResponseSenderBlock) callback {
62
+- (void) sendRequest:(NSDictionary  * _Nullable )options bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback
63
+{
55
     self.taskId = taskId;
64
     self.taskId = taskId;
56
     self.respData = [[NSMutableData alloc] initWithLength:0];
65
     self.respData = [[NSMutableData alloc] initWithLength:0];
57
     self.callback = callback;
66
     self.callback = callback;
62
     
71
     
63
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
72
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
64
     NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
73
     NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
65
-
66
-    NSURLSession * session = [NSURLSession sharedSession];
74
+    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
75
+    NSURLSession * session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:taskQueue];
76
+    
77
+//    NSURLSession * session = [NSURLSession sharedSession];
67
     
78
     
68
     // file will be stored at a specific path
79
     // file will be stored at a specific path
69
     if( path != nil) {
80
     if( path != nil) {
70
-        NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
81
+        
82
+        self.fileTaskCompletionHandler = ^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
71
             if(error != nil) {
83
             if(error != nil) {
72
                 callback(@[[error localizedDescription]]);
84
                 callback(@[[error localizedDescription]]);
73
                 return;
85
                 return;
81
                 return;
93
                 return;
82
             }
94
             }
83
             callback(@[[NSNull null], path]);
95
             callback(@[[NSNull null], path]);
84
-        }];
96
+        };
97
+        NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:fileTaskCompletionHandler];
85
         [task resume];
98
         [task resume];
86
     }
99
     }
87
     // file will be stored at tmp path
100
     // file will be stored at tmp path
88
     else if ( [self.options valueForKey:CONFIG_USE_TEMP]!= nil ) {
101
     else if ( [self.options valueForKey:CONFIG_USE_TEMP]!= nil ) {
89
-        NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
102
+        
103
+        self.fileTaskCompletionHandler = ^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
90
             if(error != nil) {
104
             if(error != nil) {
91
                 callback(@[[error localizedDescription]]);
105
                 callback(@[[error localizedDescription]]);
92
                 return;
106
                 return;
101
                 return;
115
                 return;
102
             }
116
             }
103
             callback(@[[NSNull null], tmpPath]);
117
             callback(@[[NSNull null], tmpPath]);
104
-        }];
118
+        };
119
+        NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:fileTaskCompletionHandler];
105
         [task resume];
120
         [task resume];
106
     }
121
     }
107
     // base64 response
122
     // base64 response
108
     else {
123
     else {
109
-        NSURLSessionUploadTask * task =
110
-        
111
-        [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error) {
124
+        self.dataTaskCompletionHandler = ^(NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error) {
112
             if(error != nil) {
125
             if(error != nil) {
113
                 callback(@[[error localizedDescription]]);
126
                 callback(@[[error localizedDescription]]);
114
                 return;
127
                 return;
116
             else {
129
             else {
117
                 callback(@[[NSNull null], [resp base64EncodedStringWithOptions:0]]);
130
                 callback(@[[NSNull null], [resp base64EncodedStringWithOptions:0]]);
118
             }
131
             }
119
-        }];
132
+        };
133
+        NSURLSessionDataTask * task = [session dataTaskWithRequest:req completionHandler:dataTaskCompletionHandler];
120
         [task resume];
134
         [task resume];
121
     }
135
     }
122
 }
136
 }
160
 
174
 
161
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
175
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
162
     NSLog([error localizedDescription]);
176
     NSLog([error localizedDescription]);
177
+    self.error = error;
163
 }
178
 }
164
 
179
 
165
 // upload progress handler
180
 // upload progress handler
177
      ];
192
      ];
178
 }
193
 }
179
 
194
 
195
+- (void) application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler {
196
+    
197
+}
198
+
199
+- (void) URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
200
+{
201
+    if(self.dataTaskCompletionHandler != nil)
202
+    {
203
+        dataTaskCompletionHandler(self.respData, nil, error);
204
+    }
205
+    else if(self.fileTaskCompletionHandler != nil)
206
+    {
207
+        fileTaskCompletionHandler(nil, nil, self.error);
208
+    }
209
+}
210
+
211
+- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
212
+{
213
+    if([options valueForKey:CONFIG_TRUSTY] == YES)
214
+        completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
215
+    else
216
+        RCTLogError(@"counld not create connection with an unstrusted SSL certification, if you're going to create connection anyway, add `trusty:true` to RNFetchBlob.config");
217
+}
218
+
180
 @end
219
 @end