Explorar el Código

Add containsString polyfill for IOS7 #119

Ben Hsieh hace 8 años
padre
commit
004196a9e2

+ 21
- 0
src/ios/IOS7Polyfill.h Ver fichero

@@ -0,0 +1,21 @@
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 Ver fichero

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

+ 7
- 4
src/ios/RNFetchBlobFS.m Ver fichero

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

+ 4
- 3
src/ios/RNFetchBlobNetwork.m Ver fichero

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

+ 2
- 1
src/ios/RNFetchBlobReqBuilder.m Ver fichero

@@ -12,6 +12,7 @@
12 12
 #import "RNFetchBlobConst.h"
13 13
 #import "RNFetchBlobFS.h"
14 14
 #import "RCTLog.h"
15
+#import "IOS7Polyfill.h"
15 16
 
16 17
 @interface RNFetchBlobReqBuilder()
17 18
 {
@@ -126,7 +127,7 @@
126 127
                     
127 128
                     __block NSString * cType = [[self class]getHeaderIgnoreCases:@"content-type" fromHeaders:mheaders];
128 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 132
                         __block NSString * ncType = [[cType stringByReplacingOccurrencesOfString:@";base64" withString:@""]stringByReplacingOccurrencesOfString:@";BASE64" withString:@""];
132 133
                         if([mheaders valueForKey:@"content-type"] != nil)