From f877a13740e2f557cdbc82e37e40e5bb97b12961 Mon Sep 17 00:00:00 2001 From: Dariusz Niemczyk Date: Sun, 7 Jun 2020 15:14:07 +0200 Subject: [PATCH 1/2] feat: add iOS group app id support --- README.md | 1 + index.d.ts | 5 +++++ ios/VydiaRNFileUploader.m | 12 ++++++++---- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7d539096..711de193 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,7 @@ Returns a promise with the string ID of the upload. Will reject if there is a c |`parameters`|object|Optional||Additional form fields to include in the HTTP request. Only used when `type: 'multipart`|| |`notification`|Notification object (see below)|Optional||Android only. |`{ enabled: true, onProgressTitle: "Uploading...", autoClear: true }`| |`useUtf8Charset`|boolean|Optional||Android only. Set to true to use `utf-8` as charset. || +|`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 ### Notification Object (Android Only) |Name|Type|Required|Description|Example| diff --git a/index.d.ts b/index.d.ts index 2c9d42e7..615b8369 100644 --- a/index.d.ts +++ b/index.d.ts @@ -88,6 +88,11 @@ declare module "react-native-background-upload" { }; // Android notification settings notification?: Partial + /** + * AppGroup defined in XCode for extensions. Necessary when trying to upload things via this library + * in the context of ShareExtension. + */ + appGroup?: string; } export interface MultipartUploadOptions extends UploadOptions { diff --git a/ios/VydiaRNFileUploader.m b/ios/VydiaRNFileUploader.m index c60c2d1a..7e11e5f5 100644 --- a/ios/VydiaRNFileUploader.m +++ b/ios/VydiaRNFileUploader.m @@ -150,6 +150,7 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri NSString *uploadType = options[@"type"] ?: @"raw"; NSString *fieldName = options[@"field"]; NSString *customUploadId = options[@"customUploadId"]; + NSString *appGroup = options[@"appGroup"]; NSDictionary *headers = options[@"headers"]; NSDictionary *parameters = options[@"parameters"]; @@ -158,7 +159,7 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri if (requestUrl == nil) { return reject(@"RN Uploader", @"URL not compliant with RFC 2396", nil); } - + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestUrl]; [request setHTTPMethod: method]; @@ -197,14 +198,14 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri NSData *httpBody = [self createBodyWithBoundary:uuidStr path:fileURI parameters: parameters fieldName:fieldName]; [request setHTTPBody: httpBody]; - uploadTask = [[self urlSession] uploadTaskWithStreamedRequest:request]; + uploadTask = [[self urlSession: appGroup] uploadTaskWithStreamedRequest:request]; } else { if (parameters.count > 0) { reject(@"RN Uploader", @"Parameters supported only in multipart type", nil); return; } - uploadTask = [[self urlSession] uploadTaskWithRequest:request fromFile:[NSURL URLWithString: fileURI]]; + uploadTask = [[self urlSession: appGroup] uploadTaskWithRequest:request fromFile:[NSURL URLWithString: fileURI]]; } uploadTask.taskDescription = customUploadId ? customUploadId : [NSString stringWithFormat:@"%i", thisUploadId]; @@ -266,9 +267,12 @@ - (NSData *)createBodyWithBoundary:(NSString *)boundary return httpBody; } -- (NSURLSession *)urlSession { +- (NSURLSession *)urlSession: (NSString *) groupId { if (_urlSession == nil) { NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:BACKGROUND_SESSION_ID]; + if (groupId != nil && ![groupId isEqualToString:@""]) { + sessionConfiguration.sharedContainerIdentifier = groupId; + } _urlSession = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil]; } From 9eb55a59a4195f45dd95de51ed15f22b78bf45a3 Mon Sep 17 00:00:00 2001 From: Dariusz Niemczyk Date: Mon, 8 Jun 2020 12:37:01 +0200 Subject: [PATCH 2/2] Fix d.ts for multipart options and callbacks for listeners --- index.d.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 615b8369..b7e3ba12 100644 --- a/index.d.ts +++ b/index.d.ts @@ -93,6 +93,8 @@ declare module "react-native-background-upload" { * in the context of ShareExtension. */ appGroup?: string; + // Necessary only for multipart type upload + field?: string } export interface MultipartUploadOptions extends UploadOptions { @@ -107,13 +109,13 @@ declare module "react-native-background-upload" { export type UploadListenerEvent = 'progress' | 'error' | 'completed' | 'cancelled' + export default class Upload { - static startUpload(options: UploadOptions): Promise - static addListener(event: UploadListenerEvent, uploadId: uploadId, data: object): void - static addListener(event: 'progress', uploadId: uploadId, data: ProgressData): void - static addListener(event: 'error', uploadId: uploadId, data: ErrorData): void - static addListener(event: 'completed', uploadId: uploadId, data: CompletedData): void - static addListener(event: 'cancelled', uploadId: uploadId, data: EventData): void + static startUpload(options: UploadOptions | MultipartUploadOptions): Promise + static addListener(event: 'progress', uploadId: uploadId, callback: (data: ProgressData ) => void): void + static addListener(event: 'error', uploadId: uploadId, callback: (data: ErrorData) => void): void + static addListener(event: 'completed', uploadId: uploadId, callback: (data: CompletedData) => void): void + static addListener(event: 'cancelled', uploadId: uploadId, callback: (data: EventData) => void): void static getFileInfo(path: string): Promise static cancelUpload(uploadId: uploadId): Promise }