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

Commit 0bcf47e

Browse files
committed
Add network status indicator support
1 parent 24df33c commit 0bcf47e

File tree

7 files changed

+36
-18
lines changed

7 files changed

+36
-18
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# react-native-fetch-blob [![npm version](https://img.shields.io/badge/npm package-0.5.5-brightgreen.svg)](https://badge.fury.io/js/react-native-fetch-blob) ![](https://img.shields.io/badge/PR-Welcome-brightgreen.svg) ![](https://img.shields.io/badge/in progress-0.6.0-yellow.svg)
1+
# react-native-fetch-blob [![npm version](https://img.shields.io/badge/npm package-0.5.6-brightgreen.svg)](https://badge.fury.io/js/react-native-fetch-blob) ![](https://img.shields.io/badge/PR-Welcome-brightgreen.svg) ![](https://img.shields.io/badge/in progress-0.6.0-yellow.svg)
22

33
A module provides upload, download, and files access API. Supports file stream read/write for process large files.
44

@@ -847,6 +847,7 @@ A `session` is an object that helps you manage files. It simply maintains a list
847847

848848
| Version | |
849849
|---|---|
850+
| 0.5.6 | Add support for IOS network status indicator. Fix file stream ASCII reader bug. |
850851
| 0.5.5 | Remove work in progress code added in 0.5.2 which may cause memory leaks. |
851852
| 0.5.4 | Fix #30 #31 build build error, and improve memory efficiency. |
852853
| 0.5.3 | Add API for access untrusted SSL server |

src/ios/RNFetchBlobConst.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern NSString *const CONFIG_USE_TEMP;
2323
extern NSString *const CONFIG_FILE_PATH;
2424
extern NSString *const CONFIG_FILE_EXT;
2525
extern NSString *const CONFIG_TRUSTY;
26+
extern NSString *const CONFIG_INDICATOR;
2627

2728
// fs events
2829
extern NSString *const FS_EVENT_DATA;

src/ios/RNFetchBlobConst.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
extern NSString *const CONFIG_FILE_PATH = @"path";
1515
extern NSString *const CONFIG_FILE_EXT = @"appendExt";
1616
extern NSString *const CONFIG_TRUSTY = @"trusty";
17+
extern NSString *const CONFIG_INDICATOR = @"indicator";
1718

1819

1920
extern NSString *const MSG_EVENT = @"RNFetchBlobMessage";

src/ios/RNFetchBlobFS.m

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
264264
chunkSize = 4095;
265265
if(self.bufferSize > 0)
266266
chunkSize = self.bufferSize;
267-
uint8_t * buf = (uint8_t *)malloc(chunkSize);
267+
// uint8_t * buf = (uint8_t *)malloc(chunkSize);
268+
uint8_t buf[chunkSize];
268269
unsigned int len = 0;
269270
len = [(NSInputStream *)stream read:buf maxLength:chunkSize];
270271
// still have data in stream
@@ -281,6 +282,7 @@ - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
281282
NSString * asciiStr = @"[";
282283
if (chunkData.length > 0)
283284
{
285+
// unsigned char *bytePtr = (unsigned char *)[chunkData bytes];
284286
unsigned char *bytePtr = (unsigned char *)[chunkData bytes];
285287
NSInteger byteLen = chunkData.length/sizeof(uint8_t);
286288
for (int i = 0; i < byteLen; i++)
@@ -291,7 +293,7 @@ - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
291293
else
292294
asciiStr = [asciiStr stringByAppendingFormat:@"%d", val];
293295
}
294-
free(bytePtr);
296+
// free(bytePtr);
295297
}
296298
asciiStr = [asciiStr stringByAppendingString:@"]"];
297299
[self.bridge.eventDispatcher
@@ -301,10 +303,10 @@ - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
301303
@"detail": asciiStr
302304
}
303305
];
304-
free(buf);
305-
asciiStr = nil;
306-
buf = nil;
307-
chunkData = nil;
306+
// free(buf);
307+
// asciiStr = nil;
308+
// buf = nil;
309+
// chunkData = nil;
308310
return;
309311
}
310312
// convert byte array to base64 data chunks
@@ -330,8 +332,8 @@ - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
330332
@"detail": encodedChunk
331333
}
332334
];
333-
chunkData = nil;
334-
free(buf);
335+
// chunkData = nil;
336+
// free(buf);
335337
}
336338
// end of stream
337339
else {
@@ -342,8 +344,8 @@ - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
342344
@"detail": @""
343345
}
344346
];
345-
chunkData = nil;
346-
free(buf);
347+
// chunkData = nil;
348+
// free(buf);
347349
}
348350
break;
349351
}

src/ios/RNFetchBlobNetwork.m

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ - (void) sendRequest:(NSDictionary * _Nullable )options bridge:(RCTBridge * _Nu
7777
if([options valueForKey:CONFIG_TRUSTY] != nil)
7878
{
7979
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
80-
session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:taskQueue];
80+
session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:[NSOperationQueue mainQueue]];
8181
}
8282
// the session validates SSL certification, self-signed certification will be aborted
8383
else
@@ -105,6 +105,7 @@ - (void) sendRequest:(NSDictionary * _Nullable )options bridge:(RCTBridge * _Nu
105105
callback(@[[NSNull null], path]);
106106
// prevent memory leaks
107107
self.respData = nil;
108+
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
108109
}];
109110
[task resume];
110111
}
@@ -127,6 +128,7 @@ - (void) sendRequest:(NSDictionary * _Nullable )options bridge:(RCTBridge * _Nu
127128
return;
128129
}
129130
callback(@[[NSNull null], tmpPath]);
131+
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
130132
// prevent memory leaks
131133
self.respData = nil;
132134
}];
@@ -143,9 +145,15 @@ - (void) sendRequest:(NSDictionary * _Nullable )options bridge:(RCTBridge * _Nu
143145
else {
144146
callback(@[[NSNull null], [resp base64EncodedStringWithOptions:0]]);
145147
}
148+
self.respData = nil;
149+
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
146150
}];
147151
[task resume];
148152
}
153+
154+
// network status indicator
155+
if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
156+
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
149157
}
150158

151159
////////////////////////////////////////
@@ -188,6 +196,7 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
188196
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
189197
NSLog([error localizedDescription]);
190198
self.error = error;
199+
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
191200
}
192201

193202
// upload progress handler
@@ -205,9 +214,9 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSen
205214
];
206215
}
207216

208-
- (void) application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler {
209-
210-
}
217+
//- (void) application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler {
218+
//
219+
//}
211220

212221
//- (void) URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
213222
//{
@@ -225,8 +234,10 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didRec
225234
{
226235
if([options valueForKey:CONFIG_TRUSTY] != nil)
227236
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
228-
else
237+
else {
229238
RCTLogWarn(@"counld not create connection with an unstrusted SSL certification, if you're going to create connection anyway, add `trusty:true` to RNFetchBlob.config");
239+
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
240+
}
230241
}
231242

232243
@end

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-fetch-blob",
3-
"version": "0.5.5",
3+
"version": "0.5.6",
44
"description": "A module provides upload, download, and files access API. Supports file stream read/write for process large files.",
55
"main": "index.js",
66
"scripts": {

src/types.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ type RNFetchBlobConfig = {
33
fileCache : bool,
44
path : string,
55
appendExt : string,
6-
session : string
6+
session : string,
7+
addAndroidDownloads : any,
8+
indicator : bool
79
};
810

911
type RNFetchBlobNative = {

0 commit comments

Comments
 (0)