Browse Source

Enhance ios native error handling

Ben Hsieh 8 years ago
parent
commit
1e2d86b7bd
2 changed files with 16 additions and 45 deletions
  1. 0
    36
      src/ios/RNFetchBlob.xcodeproj/project.pbxproj
  2. 16
    9
      src/ios/RNFetchBlob/RNFetchBlob.m

+ 0
- 36
src/ios/RNFetchBlob.xcodeproj/project.pbxproj View File

11
 		A166D1AA1CE0647A00273590 /* RNFetchBlob.h in Sources */ = {isa = PBXBuildFile; fileRef = A15C30111CD25C330074CB35 /* RNFetchBlob.h */; };
11
 		A166D1AA1CE0647A00273590 /* RNFetchBlob.h in Sources */ = {isa = PBXBuildFile; fileRef = A15C30111CD25C330074CB35 /* RNFetchBlob.h */; };
12
 /* End PBXBuildFile section */
12
 /* End PBXBuildFile section */
13
 
13
 
14
-/* Begin PBXContainerItemProxy section */
15
-		A1BAA89E1CF415DF003A9374 /* PBXContainerItemProxy */ = {
16
-			isa = PBXContainerItemProxy;
17
-			containerPortal = ADC1D945EE804D3DA47CF622 /* RNFetchBlob.xcodeproj */;
18
-			proxyType = 2;
19
-			remoteGlobalIDString = A15C300E1CD25C330074CB35;
20
-			remoteInfo = RNFetchBlob;
21
-		};
22
-/* End PBXContainerItemProxy section */
23
-
24
 /* Begin PBXCopyFilesBuildPhase section */
14
 /* Begin PBXCopyFilesBuildPhase section */
25
 		A15C300C1CD25C330074CB35 /* CopyFiles */ = {
15
 		A15C300C1CD25C330074CB35 /* CopyFiles */ = {
26
 			isa = PBXCopyFilesBuildPhase;
16
 			isa = PBXCopyFilesBuildPhase;
37
 		A15C300E1CD25C330074CB35 /* libRNFetchBlob.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNFetchBlob.a; sourceTree = BUILT_PRODUCTS_DIR; };
27
 		A15C300E1CD25C330074CB35 /* libRNFetchBlob.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNFetchBlob.a; sourceTree = BUILT_PRODUCTS_DIR; };
38
 		A15C30111CD25C330074CB35 /* RNFetchBlob.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = RNFetchBlob.h; path = RNFetchBlob/RNFetchBlob.h; sourceTree = "<group>"; };
28
 		A15C30111CD25C330074CB35 /* RNFetchBlob.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = RNFetchBlob.h; path = RNFetchBlob/RNFetchBlob.h; sourceTree = "<group>"; };
39
 		A15C30131CD25C330074CB35 /* RNFetchBlob.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = RNFetchBlob.m; path = RNFetchBlob/RNFetchBlob.m; sourceTree = "<group>"; };
29
 		A15C30131CD25C330074CB35 /* RNFetchBlob.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = RNFetchBlob.m; path = RNFetchBlob/RNFetchBlob.m; sourceTree = "<group>"; };
40
-		ADC1D945EE804D3DA47CF622 /* RNFetchBlob.xcodeproj */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = "wrapper.pb-project"; name = RNFetchBlob.xcodeproj; path = "../../node_modules/react-native-fetch-blob/ios/RNFetchBlob.xcodeproj"; sourceTree = "<group>"; };
41
 /* End PBXFileReference section */
30
 /* End PBXFileReference section */
42
 
31
 
43
 /* Begin PBXFrameworksBuildPhase section */
32
 /* Begin PBXFrameworksBuildPhase section */
54
 		8BD9ABDFAF76406291A798F2 /* Libraries */ = {
43
 		8BD9ABDFAF76406291A798F2 /* Libraries */ = {
55
 			isa = PBXGroup;
44
 			isa = PBXGroup;
56
 			children = (
45
 			children = (
57
-				ADC1D945EE804D3DA47CF622 /* RNFetchBlob.xcodeproj */,
58
 			);
46
 			);
59
 			name = Libraries;
47
 			name = Libraries;
60
 			sourceTree = "<group>";
48
 			sourceTree = "<group>";
77
 			name = Products;
65
 			name = Products;
78
 			sourceTree = "<group>";
66
 			sourceTree = "<group>";
79
 		};
67
 		};
80
-		A1BAA8981CF415DF003A9374 /* Products */ = {
81
-			isa = PBXGroup;
82
-			children = (
83
-				A1BAA89F1CF415DF003A9374 /* libRNFetchBlob.a */,
84
-			);
85
-			name = Products;
86
-			sourceTree = "<group>";
87
-		};
88
 /* End PBXGroup section */
68
 /* End PBXGroup section */
89
 
69
 
90
 /* Begin PBXNativeTarget section */
70
 /* Begin PBXNativeTarget section */
129
 			mainGroup = A15C30051CD25C330074CB35;
109
 			mainGroup = A15C30051CD25C330074CB35;
130
 			productRefGroup = A15C300F1CD25C330074CB35 /* Products */;
110
 			productRefGroup = A15C300F1CD25C330074CB35 /* Products */;
131
 			projectDirPath = "";
111
 			projectDirPath = "";
132
-			projectReferences = (
133
-				{
134
-					ProductGroup = A1BAA8981CF415DF003A9374 /* Products */;
135
-					ProjectRef = ADC1D945EE804D3DA47CF622 /* RNFetchBlob.xcodeproj */;
136
-				},
137
-			);
138
 			projectRoot = "";
112
 			projectRoot = "";
139
 			targets = (
113
 			targets = (
140
 				A15C300D1CD25C330074CB35 /* RNFetchBlob */,
114
 				A15C300D1CD25C330074CB35 /* RNFetchBlob */,
142
 		};
116
 		};
143
 /* End PBXProject section */
117
 /* End PBXProject section */
144
 
118
 
145
-/* Begin PBXReferenceProxy section */
146
-		A1BAA89F1CF415DF003A9374 /* libRNFetchBlob.a */ = {
147
-			isa = PBXReferenceProxy;
148
-			fileType = archive.ar;
149
-			path = libRNFetchBlob.a;
150
-			remoteRef = A1BAA89E1CF415DF003A9374 /* PBXContainerItemProxy */;
151
-			sourceTree = BUILT_PRODUCTS_DIR;
152
-		};
153
-/* End PBXReferenceProxy section */
154
-
155
 /* Begin PBXSourcesBuildPhase section */
119
 /* Begin PBXSourcesBuildPhase section */
156
 		A15C300A1CD25C330074CB35 /* Sources */ = {
120
 		A15C300A1CD25C330074CB35 /* Sources */ = {
157
 			isa = PBXSourcesBuildPhase;
121
 			isa = PBXSourcesBuildPhase;

+ 16
- 9
src/ios/RNFetchBlob/RNFetchBlob.m View File

122
 
122
 
123
 // request complete
123
 // request complete
124
 - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
124
 - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
125
-    NSData * data = [NSData dataWithData:respData];
125
+    
126
+    NSData * data;
127
+    if(respData != nil)
128
+        data = [NSData dataWithData:respData];
129
+    else
130
+        data = [[NSData alloc] init];
131
+    
126
     callback(@[[NSNull null], [data base64EncodedStringWithOptions:0]]);
132
     callback(@[[NSNull null], [data base64EncodedStringWithOptions:0]]);
127
 }
133
 }
128
 
134
 
168
             NSString * name = [field valueForKey:@"name"];
174
             NSString * name = [field valueForKey:@"name"];
169
             NSString * content = [field valueForKey:@"data"];
175
             NSString * content = [field valueForKey:@"data"];
170
             // field is a text field
176
             // field is a text field
171
-            if([field valueForKey:@"filename"] == nil) {
177
+            if([field valueForKey:@"filename"] == nil || content == [NSNull null]) {
172
                 [postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
178
                 [postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
173
                 [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", name] dataUsingEncoding:NSUTF8StringEncoding]];
179
                 [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", name] dataUsingEncoding:NSUTF8StringEncoding]];
174
                 [postData appendData:[[NSString stringWithFormat:@"Content-Type: text/plain\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
180
                 [postData appendData:[[NSString stringWithFormat:@"Content-Type: text/plain\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
225
     // if method is POST or PUT, convert data string format
231
     // if method is POST or PUT, convert data string format
226
     if([[method lowercaseString] isEqualToString:@"post"] || [[method lowercaseString] isEqualToString:@"put"]) {
232
     if([[method lowercaseString] isEqualToString:@"post"] || [[method lowercaseString] isEqualToString:@"put"]) {
227
         
233
         
228
-        // generate octet-stream body
229
-        NSData* blobData = [[NSData alloc] initWithBase64EncodedString:body options:0];
230
-        NSMutableData* postBody = [[NSMutableData alloc] init];
231
-        [postBody appendData:[NSData dataWithData:blobData]];
232
-        [request setHTTPBody:postBody];
233
-        [mheaders setValue:@"application/octet-stream" forKey:@"content-type"];
234
-        
234
+        if(body != nil) {
235
+            // generate octet-stream body
236
+            NSData* blobData = [[NSData alloc] initWithBase64EncodedString:body options:0];
237
+            NSMutableData* postBody = [[NSMutableData alloc] init];
238
+            [postBody appendData:[NSData dataWithData:blobData]];
239
+            [request setHTTPBody:postBody];
240
+            [mheaders setValue:@"application/octet-stream" forKey:@"content-type"];
241
+        }
235
     }
242
     }
236
     
243
     
237
     [request setHTTPMethod: method];
244
     [request setHTTPMethod: method];