Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iOS group app id support & fix .d.ts #198

Merged
merged 2 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
19 changes: 13 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ declare module "react-native-background-upload" {
};
// Android notification settings
notification?: Partial<NotificationOptions>
/**
* AppGroup defined in XCode for extensions. Necessary when trying to upload things via this library
* in the context of ShareExtension.
*/
appGroup?: string;
// Necessary only for multipart type upload
field?: string
}

export interface MultipartUploadOptions extends UploadOptions {
Expand All @@ -102,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<uploadId>
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<uploadId>
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<FileInfo>
static cancelUpload(uploadId: uploadId): Promise<boolean>
}
Expand Down
12 changes: 8 additions & 4 deletions ios/VydiaRNFileUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand All @@ -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];

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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];
}

Expand Down