Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 004196a

Browse files
committed
Add containsString polyfill for IOS7 #119
1 parent 105cca9 commit 004196a

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

src/ios/IOS7Polyfill.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 */

src/ios/RNFetchBlob.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
A15C30131CD25C330074CB35 /* RNFetchBlob.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = RNFetchBlob.m; path = RNFetchBlob/RNFetchBlob.m; sourceTree = "<group>"; };
4040
A1AAE2971D300E3E0051D11C /* RNFetchBlobReqBuilder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobReqBuilder.h; sourceTree = "<group>"; };
4141
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>"; };
4243
/* End PBXFileReference section */
4344

4445
/* Begin PBXFrameworksBuildPhase section */
@@ -62,6 +63,7 @@
6263
A15C30051CD25C330074CB35 = {
6364
isa = PBXGroup;
6465
children = (
66+
A1F950181D7E9134002A95A6 /* IOS7Polyfill.h */,
6567
A1AAE2981D300E4D0051D11C /* RNFetchBlobReqBuilder.m */,
6668
A1AAE2971D300E3E0051D11C /* RNFetchBlobReqBuilder.h */,
6769
A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */,

src/ios/RNFetchBlobFS.m

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#import "RCTEventDispatcher.h"
1414
#import "RNFetchBlobFS.h"
1515
#import "RNFetchBlobConst.h"
16+
#import "IOS7Polyfill.h"
1617
@import AssetsLibrary;
1718

1819

@@ -130,9 +131,10 @@ + (void) readStream:(NSString *)uri
130131
int read = 0;
131132
int chunkSize = bufferSize;
132133
// allocate buffer in heap instead of stack
133-
uint8_t * buffer = (uint8_t *) malloc(bufferSize);
134+
uint8_t * buffer;
134135
@try
135136
{
137+
buffer = (uint8_t *) malloc(bufferSize);
136138
if(path != nil)
137139
{
138140
if([[NSFileManager defaultManager] fileExistsAtPath:path] == NO)
@@ -166,6 +168,9 @@ + (void) readStream:(NSString *)uri
166168
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"detail": @"RNFetchBlob.readStream unable to resolve URI" };
167169
[event sendDeviceEventWithName:streamId body:payload];
168170
}
171+
// release buffer
172+
if(buffer != nil)
173+
free(buffer);
169174

170175
}
171176
@catch (NSError * err)
@@ -175,8 +180,6 @@ + (void) readStream:(NSString *)uri
175180
}
176181
@finally
177182
{
178-
// release buffer
179-
free(buffer);
180183
NSDictionary * payload = @{ @"event": FS_EVENT_END, @"detail": @"" };
181184
[event sendDeviceEventWithName:streamId body:payload];
182185
}
@@ -292,7 +295,7 @@ + (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString
292295
}
293296
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
294297
NSData * content = nil;
295-
if([encoding containsString:@"base64"]) {
298+
if([encoding RNFBContainsString:@"base64"]) {
296299
content = [[NSData alloc] initWithBase64EncodedString:data options:0];
297300
}
298301
else if([encoding isEqualToString:@"uri"]) {

src/ios/RNFetchBlobNetwork.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#import "RNFetchBlobNetwork.h"
1515
#import "RNFetchBlobConst.h"
1616
#import "RNFetchBlobReqBuilder.h"
17+
#import "IOS7Polyfill.h"
1718
#import <CommonCrypto/CommonDigest.h>
1819

1920
////////////////////////////////////////
@@ -207,19 +208,19 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
207208
if(respCType != nil)
208209
{
209210
NSArray * extraBlobCTypes = [options objectForKey:CONFIG_EXTRA_BLOB_CTYPE];
210-
if([respCType containsString:@"text/"])
211+
if([respCType RNFBContainsString:@"text/"])
211212
{
212213
respType = @"text";
213214
}
214-
else if([respCType containsString:@"application/json"])
215+
else if([respCType RNFBContainsString:@"application/json"])
215216
{
216217
respType = @"json";
217218
}
218219
// If extra blob content type is not empty, check if response type matches
219220
else if( extraBlobCTypes != nil) {
220221
for(NSString * substr in extraBlobCTypes)
221222
{
222-
if([respCType containsString:[substr lowercaseString]])
223+
if([respCType RNFBContainsString:[substr lowercaseString]])
223224
{
224225
respType = @"blob";
225226
respFile = YES;

src/ios/RNFetchBlobReqBuilder.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import "RNFetchBlobConst.h"
1313
#import "RNFetchBlobFS.h"
1414
#import "RCTLog.h"
15+
#import "IOS7Polyfill.h"
1516

1617
@interface RNFetchBlobReqBuilder()
1718
{
@@ -126,7 +127,7 @@ +(void) buildOctetRequest:(NSDictionary *)options
126127

127128
__block NSString * cType = [[self class]getHeaderIgnoreCases:@"content-type" fromHeaders:mheaders];
128129
// 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"])
130131
{
131132
__block NSString * ncType = [[cType stringByReplacingOccurrencesOfString:@";base64" withString:@""]stringByReplacingOccurrencesOfString:@";BASE64" withString:@""];
132133
if([mheaders valueForKey:@"content-type"] != nil)

0 commit comments

Comments
 (0)