Procházet zdrojové kódy

#29 add IOS implementation for self-signed server

Ben Hsieh před 8 roky
rodič
revize
af3c86a78d

+ 1
- 0
src/ios/RNFetchBlobConst.h Zobrazit soubor

@@ -22,6 +22,7 @@ extern NSString *const FILE_PREFIX;
22 22
 extern NSString *const CONFIG_USE_TEMP;
23 23
 extern NSString *const CONFIG_FILE_PATH;
24 24
 extern NSString *const CONFIG_FILE_EXT;
25
+extern NSString *const CONFIG_TRUSTY;
25 26
 
26 27
 // fs events
27 28
 extern NSString *const FS_EVENT_DATA;

+ 2
- 0
src/ios/RNFetchBlobConst.m Zobrazit soubor

@@ -13,6 +13,8 @@ extern NSString *const FILE_PREFIX = @"RNFetchBlob-file://";
13 13
 extern NSString *const CONFIG_USE_TEMP = @"fileCache";
14 14
 extern NSString *const CONFIG_FILE_PATH = @"path";
15 15
 extern NSString *const CONFIG_FILE_EXT = @"appendExt";
16
+extern NSString *const CONFIG_TRUSTY = @"trusty";
17
+
16 18
 
17 19
 extern NSString *const MSG_EVENT = @"RNFetchBlobMessage";
18 20
 extern NSString *const MSG_EVENT_LOG = @"log";

+ 17
- 20
src/ios/RNFetchBlobNetwork.h Zobrazit soubor

@@ -12,32 +12,29 @@
12 12
 #import <Foundation/Foundation.h>
13 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 21
 @property (nonatomic) int expectedBytes;
28 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 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 40
 @end

+ 50
- 11
src/ios/RNFetchBlobNetwork.m Zobrazit soubor

@@ -23,6 +23,7 @@
23 23
 
24 24
 @implementation RNFetchBlobNetwork
25 25
 
26
+NSOperationQueue *taskQueue;
26 27
 
27 28
 @synthesize taskId;
28 29
 @synthesize expectedBytes;
@@ -31,10 +32,17 @@
31 32
 @synthesize callback;
32 33
 @synthesize bridge;
33 34
 @synthesize options;
35
+@synthesize fileTaskCompletionHandler;
36
+@synthesize dataTaskCompletionHandler;
37
+@synthesize error;
38
+
34 39
 
35 40
 // constructor
36 41
 - (id)init {
37 42
     self = [super init];
43
+    if(taskQueue == nil) {
44
+        taskQueue = [[NSOperationQueue alloc] init];
45
+    }
38 46
     return self;
39 47
 }
40 48
 
@@ -51,7 +59,8 @@
51 59
 }
52 60
 
53 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 64
     self.taskId = taskId;
56 65
     self.respData = [[NSMutableData alloc] initWithLength:0];
57 66
     self.callback = callback;
@@ -62,12 +71,15 @@
62 71
     
63 72
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
64 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 79
     // file will be stored at a specific path
69 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 83
             if(error != nil) {
72 84
                 callback(@[[error localizedDescription]]);
73 85
                 return;
@@ -81,12 +93,14 @@
81 93
                 return;
82 94
             }
83 95
             callback(@[[NSNull null], path]);
84
-        }];
96
+        };
97
+        NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:fileTaskCompletionHandler];
85 98
         [task resume];
86 99
     }
87 100
     // file will be stored at tmp path
88 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 104
             if(error != nil) {
91 105
                 callback(@[[error localizedDescription]]);
92 106
                 return;
@@ -101,14 +115,13 @@
101 115
                 return;
102 116
             }
103 117
             callback(@[[NSNull null], tmpPath]);
104
-        }];
118
+        };
119
+        NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:fileTaskCompletionHandler];
105 120
         [task resume];
106 121
     }
107 122
     // base64 response
108 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 125
             if(error != nil) {
113 126
                 callback(@[[error localizedDescription]]);
114 127
                 return;
@@ -116,7 +129,8 @@
116 129
             else {
117 130
                 callback(@[[NSNull null], [resp base64EncodedStringWithOptions:0]]);
118 131
             }
119
-        }];
132
+        };
133
+        NSURLSessionDataTask * task = [session dataTaskWithRequest:req completionHandler:dataTaskCompletionHandler];
120 134
         [task resume];
121 135
     }
122 136
 }
@@ -160,6 +174,7 @@
160 174
 
161 175
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
162 176
     NSLog([error localizedDescription]);
177
+    self.error = error;
163 178
 }
164 179
 
165 180
 // upload progress handler
@@ -177,4 +192,28 @@
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 219
 @end