Browse Source

Add containsString polyfill for IOS7 #119

Ben Hsieh 8 years ago
parent
commit
004196a9e2

+ 21
- 0
src/ios/IOS7Polyfill.h View File

1
+//
2
+//  IOS7Polyfill.h
3
+//  RNFetchBlob
4
+//
5
+//  Created by Ben Hsieh on 2016/9/6.
6
+//  Copyright © 2016年 wkh237.github.io. All rights reserved.
7
+//
8
+
9
+#ifndef IOS7Polyfill_h
10
+#define IOS7Polyfill_h
11
+
12
+@implementation NSString (Contains)
13
+
14
+- (BOOL)RNFBContainsString:(NSString*)other {
15
+    NSRange range = [self rangeOfString:other];
16
+    return range.length != 0;
17
+}
18
+
19
+
20
+@end
21
+#endif /* IOS7Polyfill_h */

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

39
 		A15C30131CD25C330074CB35 /* RNFetchBlob.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = RNFetchBlob.m; path = RNFetchBlob/RNFetchBlob.m; sourceTree = "<group>"; };
39
 		A15C30131CD25C330074CB35 /* RNFetchBlob.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = RNFetchBlob.m; path = RNFetchBlob/RNFetchBlob.m; sourceTree = "<group>"; };
40
 		A1AAE2971D300E3E0051D11C /* RNFetchBlobReqBuilder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobReqBuilder.h; sourceTree = "<group>"; };
40
 		A1AAE2971D300E3E0051D11C /* RNFetchBlobReqBuilder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobReqBuilder.h; sourceTree = "<group>"; };
41
 		A1AAE2981D300E4D0051D11C /* RNFetchBlobReqBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobReqBuilder.m; sourceTree = "<group>"; };
41
 		A1AAE2981D300E4D0051D11C /* RNFetchBlobReqBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobReqBuilder.m; sourceTree = "<group>"; };
42
+		A1F950181D7E9134002A95A6 /* IOS7Polyfill.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IOS7Polyfill.h; sourceTree = "<group>"; };
42
 /* End PBXFileReference section */
43
 /* End PBXFileReference section */
43
 
44
 
44
 /* Begin PBXFrameworksBuildPhase section */
45
 /* Begin PBXFrameworksBuildPhase section */
62
 		A15C30051CD25C330074CB35 = {
63
 		A15C30051CD25C330074CB35 = {
63
 			isa = PBXGroup;
64
 			isa = PBXGroup;
64
 			children = (
65
 			children = (
66
+				A1F950181D7E9134002A95A6 /* IOS7Polyfill.h */,
65
 				A1AAE2981D300E4D0051D11C /* RNFetchBlobReqBuilder.m */,
67
 				A1AAE2981D300E4D0051D11C /* RNFetchBlobReqBuilder.m */,
66
 				A1AAE2971D300E3E0051D11C /* RNFetchBlobReqBuilder.h */,
68
 				A1AAE2971D300E3E0051D11C /* RNFetchBlobReqBuilder.h */,
67
 				A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */,
69
 				A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */,

+ 7
- 4
src/ios/RNFetchBlobFS.m View File

13
 #import "RCTEventDispatcher.h"
13
 #import "RCTEventDispatcher.h"
14
 #import "RNFetchBlobFS.h"
14
 #import "RNFetchBlobFS.h"
15
 #import "RNFetchBlobConst.h"
15
 #import "RNFetchBlobConst.h"
16
+#import "IOS7Polyfill.h"
16
 @import AssetsLibrary;
17
 @import AssetsLibrary;
17
 
18
 
18
 
19
 
130
         int read = 0;
131
         int read = 0;
131
         int chunkSize = bufferSize;
132
         int chunkSize = bufferSize;
132
         // allocate buffer in heap instead of stack
133
         // allocate buffer in heap instead of stack
133
-        uint8_t * buffer = (uint8_t *) malloc(bufferSize);
134
+        uint8_t * buffer;
134
         @try
135
         @try
135
         {
136
         {
137
+            buffer = (uint8_t *) malloc(bufferSize);
136
             if(path != nil)
138
             if(path != nil)
137
             {
139
             {
138
                 if([[NSFileManager defaultManager] fileExistsAtPath:path] == NO)
140
                 if([[NSFileManager defaultManager] fileExistsAtPath:path] == NO)
166
                 NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"detail": @"RNFetchBlob.readStream unable to resolve URI" };
168
                 NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"detail": @"RNFetchBlob.readStream unable to resolve URI" };
167
                 [event sendDeviceEventWithName:streamId body:payload];
169
                 [event sendDeviceEventWithName:streamId body:payload];
168
             }
170
             }
171
+            // release buffer
172
+            if(buffer != nil)
173
+                free(buffer);
169
             
174
             
170
         }
175
         }
171
         @catch (NSError * err)
176
         @catch (NSError * err)
175
         }
180
         }
176
         @finally
181
         @finally
177
         {
182
         {
178
-            // release buffer
179
-            free(buffer);
180
             NSDictionary * payload = @{ @"event": FS_EVENT_END, @"detail": @"" };
183
             NSDictionary * payload = @{ @"event": FS_EVENT_END, @"detail": @"" };
181
             [event sendDeviceEventWithName:streamId body:payload];
184
             [event sendDeviceEventWithName:streamId body:payload];
182
         }
185
         }
292
         }
295
         }
293
         NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
296
         NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
294
         NSData * content = nil;
297
         NSData * content = nil;
295
-        if([encoding containsString:@"base64"]) {
298
+        if([encoding RNFBContainsString:@"base64"]) {
296
             content = [[NSData alloc] initWithBase64EncodedString:data options:0];
299
             content = [[NSData alloc] initWithBase64EncodedString:data options:0];
297
         }
300
         }
298
         else if([encoding isEqualToString:@"uri"]) {
301
         else if([encoding isEqualToString:@"uri"]) {

+ 4
- 3
src/ios/RNFetchBlobNetwork.m View File

14
 #import "RNFetchBlobNetwork.h"
14
 #import "RNFetchBlobNetwork.h"
15
 #import "RNFetchBlobConst.h"
15
 #import "RNFetchBlobConst.h"
16
 #import "RNFetchBlobReqBuilder.h"
16
 #import "RNFetchBlobReqBuilder.h"
17
+#import "IOS7Polyfill.h"
17
 #import <CommonCrypto/CommonDigest.h>
18
 #import <CommonCrypto/CommonDigest.h>
18
 
19
 
19
 ////////////////////////////////////////
20
 ////////////////////////////////////////
207
         if(respCType != nil)
208
         if(respCType != nil)
208
         {
209
         {
209
             NSArray * extraBlobCTypes = [options objectForKey:CONFIG_EXTRA_BLOB_CTYPE];
210
             NSArray * extraBlobCTypes = [options objectForKey:CONFIG_EXTRA_BLOB_CTYPE];
210
-            if([respCType containsString:@"text/"])
211
+            if([respCType RNFBContainsString:@"text/"])
211
             {
212
             {
212
                 respType = @"text";
213
                 respType = @"text";
213
             }
214
             }
214
-            else if([respCType containsString:@"application/json"])
215
+            else if([respCType RNFBContainsString:@"application/json"])
215
             {
216
             {
216
                 respType = @"json";
217
                 respType = @"json";
217
             }
218
             }
219
             else if( extraBlobCTypes !=  nil) {
220
             else if( extraBlobCTypes !=  nil) {
220
                 for(NSString * substr in extraBlobCTypes)
221
                 for(NSString * substr in extraBlobCTypes)
221
                 {
222
                 {
222
-                    if([respCType containsString:[substr lowercaseString]])
223
+                    if([respCType RNFBContainsString:[substr lowercaseString]])
223
                     {
224
                     {
224
                         respType = @"blob";
225
                         respType = @"blob";
225
                         respFile = YES;
226
                         respFile = YES;

+ 2
- 1
src/ios/RNFetchBlobReqBuilder.m View File

12
 #import "RNFetchBlobConst.h"
12
 #import "RNFetchBlobConst.h"
13
 #import "RNFetchBlobFS.h"
13
 #import "RNFetchBlobFS.h"
14
 #import "RCTLog.h"
14
 #import "RCTLog.h"
15
+#import "IOS7Polyfill.h"
15
 
16
 
16
 @interface RNFetchBlobReqBuilder()
17
 @interface RNFetchBlobReqBuilder()
17
 {
18
 {
126
                     
127
                     
127
                     __block NSString * cType = [[self class]getHeaderIgnoreCases:@"content-type" fromHeaders:mheaders];
128
                     __block NSString * cType = [[self class]getHeaderIgnoreCases:@"content-type" fromHeaders:mheaders];
128
                     // when content-type is application/octet* decode body string using BASE64 decoder
129
                     // when content-type is application/octet* decode body string using BASE64 decoder
129
-                    if([[cType lowercaseString] hasPrefix:@"application/octet"] || [[cType lowercaseString] containsString:@";base64"])
130
+                    if([[cType lowercaseString] hasPrefix:@"application/octet"] || [[cType lowercaseString] RNFBContainsString:@";base64"])
130
                     {
131
                     {
131
                         __block NSString * ncType = [[cType stringByReplacingOccurrencesOfString:@";base64" withString:@""]stringByReplacingOccurrencesOfString:@";BASE64" withString:@""];
132
                         __block NSString * ncType = [[cType stringByReplacingOccurrencesOfString:@";base64" withString:@""]stringByReplacingOccurrencesOfString:@";BASE64" withString:@""];
132
                         if([mheaders valueForKey:@"content-type"] != nil)
133
                         if([mheaders valueForKey:@"content-type"] != nil)