Skip to content

Commit 6729599

Browse files
authoredJun 9, 2020
feat: Add iOS group app id support & fix .d.ts (Vydia#198)
* feat: add iOS group app id support * Fix d.ts for multipart options and callbacks for listeners
1 parent 9f6deb4 commit 6729599

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ Returns a promise with the string ID of the upload. Will reject if there is a c
168168
|`parameters`|object|Optional||Additional form fields to include in the HTTP request. Only used when `type: 'multipart`||
169169
|`notification`|Notification object (see below)|Optional||Android only. |`{ enabled: true, onProgressTitle: "Uploading...", autoClear: true }`|
170170
|`useUtf8Charset`|boolean|Optional||Android only. Set to true to use `utf-8` as charset. ||
171+
|`appGroup`|string|Optional|iOS only. App group ID needed for share extensions to be able to properly call the library. See: https://developer.apple.com/documentation/foundation/nsfilemanager/1412643-containerurlforsecurityapplicati
171172

172173
### Notification Object (Android Only)
173174
|Name|Type|Required|Description|Example|

‎index.d.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ declare module "react-native-background-upload" {
8888
};
8989
// Android notification settings
9090
notification?: Partial<NotificationOptions>
91+
/**
92+
* AppGroup defined in XCode for extensions. Necessary when trying to upload things via this library
93+
* in the context of ShareExtension.
94+
*/
95+
appGroup?: string;
96+
// Necessary only for multipart type upload
97+
field?: string
9198
}
9299

93100
export interface MultipartUploadOptions extends UploadOptions {
@@ -102,13 +109,13 @@ declare module "react-native-background-upload" {
102109

103110
export type UploadListenerEvent = 'progress' | 'error' | 'completed' | 'cancelled'
104111

112+
105113
export default class Upload {
106-
static startUpload(options: UploadOptions): Promise<uploadId>
107-
static addListener(event: UploadListenerEvent, uploadId: uploadId, data: object): void
108-
static addListener(event: 'progress', uploadId: uploadId, data: ProgressData): void
109-
static addListener(event: 'error', uploadId: uploadId, data: ErrorData): void
110-
static addListener(event: 'completed', uploadId: uploadId, data: CompletedData): void
111-
static addListener(event: 'cancelled', uploadId: uploadId, data: EventData): void
114+
static startUpload(options: UploadOptions | MultipartUploadOptions): Promise<uploadId>
115+
static addListener(event: 'progress', uploadId: uploadId, callback: (data: ProgressData ) => void): void
116+
static addListener(event: 'error', uploadId: uploadId, callback: (data: ErrorData) => void): void
117+
static addListener(event: 'completed', uploadId: uploadId, callback: (data: CompletedData) => void): void
118+
static addListener(event: 'cancelled', uploadId: uploadId, callback: (data: EventData) => void): void
112119
static getFileInfo(path: string): Promise<FileInfo>
113120
static cancelUpload(uploadId: uploadId): Promise<boolean>
114121
}

‎ios/VydiaRNFileUploader.m

+8-4
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri
150150
NSString *uploadType = options[@"type"] ?: @"raw";
151151
NSString *fieldName = options[@"field"];
152152
NSString *customUploadId = options[@"customUploadId"];
153+
NSString *appGroup = options[@"appGroup"];
153154
NSDictionary *headers = options[@"headers"];
154155
NSDictionary *parameters = options[@"parameters"];
155156

@@ -158,7 +159,7 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri
158159
if (requestUrl == nil) {
159160
return reject(@"RN Uploader", @"URL not compliant with RFC 2396", nil);
160161
}
161-
162+
162163
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestUrl];
163164
[request setHTTPMethod: method];
164165

@@ -197,14 +198,14 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri
197198
NSData *httpBody = [self createBodyWithBoundary:uuidStr path:fileURI parameters: parameters fieldName:fieldName];
198199
[request setHTTPBody: httpBody];
199200

200-
uploadTask = [[self urlSession] uploadTaskWithStreamedRequest:request];
201+
uploadTask = [[self urlSession: appGroup] uploadTaskWithStreamedRequest:request];
201202
} else {
202203
if (parameters.count > 0) {
203204
reject(@"RN Uploader", @"Parameters supported only in multipart type", nil);
204205
return;
205206
}
206207

207-
uploadTask = [[self urlSession] uploadTaskWithRequest:request fromFile:[NSURL URLWithString: fileURI]];
208+
uploadTask = [[self urlSession: appGroup] uploadTaskWithRequest:request fromFile:[NSURL URLWithString: fileURI]];
208209
}
209210

210211
uploadTask.taskDescription = customUploadId ? customUploadId : [NSString stringWithFormat:@"%i", thisUploadId];
@@ -266,9 +267,12 @@ - (NSData *)createBodyWithBoundary:(NSString *)boundary
266267
return httpBody;
267268
}
268269

269-
- (NSURLSession *)urlSession {
270+
- (NSURLSession *)urlSession: (NSString *) groupId {
270271
if (_urlSession == nil) {
271272
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:BACKGROUND_SESSION_ID];
273+
if (groupId != nil && ![groupId isEqualToString:@""]) {
274+
sessionConfiguration.sharedContainerIdentifier = groupId;
275+
}
272276
_urlSession = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
273277
}
274278

0 commit comments

Comments
 (0)
Please sign in to comment.