Browse SDKs · iOS
SDKsiOS

Upload a file

Upload a standalone file with the OpenIM iOS SDK.

Copy

Standalone file uploads can be used for user avatars, group avatars, profile attachments, or application files. They are independent of messages.

Parameters

ParameterDescription
fullPathAn absolute file path that the app can read.
nameThe uploaded file name. Pass nil to use the final path component.
causeThe upload reason defined by your application.
onProgressReports saved, currently processed, and total bytes.
onCompletionReturns total bytes, the remote URL, and putType.
[[OIMManager manager] uploadFile:fileURL.path
                            name:fileURL.lastPathComponent
                           cause:@"user-avatar"
                      onProgress:^(NSInteger saved, NSInteger current, NSInteger total) {
    [uploadStore updateCurrentBytes:current totalBytes:total];
}
                    onCompletion:^(NSInteger total, NSString *url, NSInteger putType) {
    [profileService saveAvatarURL:url];
}
                       onSuccess:nil
                       onFailure:^(NSInteger code, NSString *message) {
    NSLog(@"uploadFile failed: %ld %@", (long)code, message);
}];

onCompletion means that the resource result is available. The final outcome of the overall call is still reported by onSuccess or onFailure. This method does not update a user profile automatically, nor does it create or send a message. If the resource will be used in chat, use the returned URL to create a URL-based message and then send it explicitly.