Skip to content

Commit 3ac2b0d

Browse files
authored
Merge pull request #24 from chrisharrison/master
Support for `responseBody` on iOS
2 parents 3ff437a + d9227d9 commit 3ac2b0d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

ios/VydiaRNFileUploader.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ @implementation VydiaRNFileUploader
2323
static RCTEventEmitter* staticEventEmitter = nil;
2424
static NSString *BACKGROUND_SESSION_ID = @"VydiaRNFileUploader";
2525
NSURLSession *_urlSession = nil;
26+
NSMutableDictionary *_responsesData = nil;
27+
28+
+(void)initialize {
29+
if(!_responsesData) {
30+
_responsesData = [NSMutableDictionary dictionary];
31+
}
32+
}
2633

2734
-(id) init {
2835
self = [super init];
@@ -158,6 +165,17 @@ - (void)URLSession:(NSURLSession *)session
158165
if (response != nil)
159166
{
160167
[data setObject:[NSNumber numberWithInteger:response.statusCode] forKey:@"responseCode"];
168+
169+
//Add data that was collected earlier by the didReceiveData method
170+
NSMutableData *responseData = _responsesData[@(task.taskIdentifier)];
171+
if (responseData) {
172+
NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
173+
[data setObject:response forKey:@"responseBody"];
174+
} else {
175+
[data setObject:[NSNull null] forKey:@"responseBody"];
176+
}
177+
[_responsesData removeObjectForKey:@(task.taskIdentifier)];
178+
161179
}
162180

163181
if (error == nil)
@@ -185,4 +203,15 @@ - (void)URLSession:(NSURLSession *)session
185203
[self _sendEventWithName:@"RNFileUploader-progress" body:@{ @"id": task.taskDescription, @"progress": [NSNumber numberWithFloat:progress] }];
186204
}
187205

206+
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
207+
//Hold returned data so it can be picked up by the didCompleteWithError method later
208+
NSMutableData *responseData = _responsesData[@(dataTask.taskIdentifier)];
209+
if (!responseData) {
210+
responseData = [NSMutableData dataWithData:data];
211+
_responsesData[@(dataTask.taskIdentifier)] = responseData;
212+
} else {
213+
[responseData appendData:data];
214+
}
215+
}
216+
188217
@end

0 commit comments

Comments
 (0)