Skip to main content

Downloading Media Message Files

IMLib SDK provides functionality to download multimedia files from media messages.

Media Files in Media Messages

If the content property of a [RCMessage] object is of type RCMediaMessageContent, it may contain the remote storage address of a media file. The built-in media message types in IMLib SDK include:

  • [RCFileMessage]: File message content

  • [RCImageMessage]: Image message content

  • [RCGIFMessage]: GIF message content

  • [RCHQVoiceMessage]: High-quality voice message

  • [RCSightMessage]: Short video message content

When receiving such messages, you can use downloadMediaMessage to download the media files they contain.

Downloading Media Files from Media Messages

tip
  • Starting from SDK version 5.6.6, methods that take [RCMessage] as input for downloading media files are provided, while methods that take messageId as input are deprecated.

  • After downloading media files locally, the local path localPath of the media file in the message content body RCMediaMessageContent will be updated.

Interface Parameters

/// - Since: 5.6.4


- (void)downloadMediaMessage:(RCMessage *)message
progressBlock:(nullable void (^)(int progress))progressBlock
successBlock:(nullable void (^)(NSString *mediaPath))successBlock
errorBlock:(nullable void (^)(RCErrorCode errorCode))errorBlock
cancelBlock:(nullable void (^)(void))cancelBlock;

Parameter Description

ParameterTypeDescription
message[RCMessage]Message object.
progressBlockBlockDownload progress callback, returns int type progress value in range [0, 100].
successBlockBlockDownload success callback, returns NSString type local path, and updates the localPath in message content body with the returned mediaPath.
errorBlockBlockDownload failure callback, returns error code [RCErrorCode].
cancelBlockBlockDownload cancellation callback.

Example Code

(SDK version ≥ 5.6.6) To download media files from messages, pass a [RCMessage] object:

[[RCCoreClient sharedCoreClient] downloadMediaMessage:message
progressBlock:^(int progress) {
// Update UI with download progress
}
successBlock:^(NSString *mediaPath) {
// Handle successful download
}
errorBlock:^(RCErrorCode errorCode) {
// Handle download error
}
cancelBlock:^{
// Handle download cancellation
}];

(SDK version < 5.6.6) To download media files from messages, pass the value of the messageId property from the [RCMessage] object:

[[RCCoreClient sharedCoreClient] downloadMediaMessage:messageId
progress:^(int progress) { }
success:^(NSString *mediaPath) { }
error:^(RCErrorCode errorCode) { }
cancel:^{ }];

Pausing Download of Media Files from Media Messages

tip

Pause download functionality is only supported in version 5.6.6 and later.

You can pause the download of media files during the download process. To resume downloading, you need to call the download method again, as the download supports resumption from breakpoints.

Example Code

[[RCCoreClient sharedCoreClient] pauseDownloadMediaMessage:message
successBlock:^{
// Handle pause success
}
errorBlock:^(RCErrorCode errorCode) {
// Handle pause error
}];

Canceling Download of Media Files from Media Messages

tip

Starting from IMLib SDK version 5.6.6, asynchronous methods are provided, while the original synchronous methods are deprecated.

(SDK version ≥ 5.6.6) Asynchronously cancel the download of media files from media messages. If [RCErrorCode] is -3/OPERATION_MEDIA_NOT_FOUND, it means the download task has already ended.

Example Code

[[RCCoreClient sharedCoreClient] cancelDownloadMediaMessage:message
successBlock:^{
// Handle cancellation success
}
errorBlock:^(RCErrorCode errorCode) {
// Handle cancellation error
}];

(SDK version < 5.6.6) Synchronously cancel the download of media files from media messages. Returns YES if cancellation is successful. Returns NO if cancellation fails, meaning the download has already completed or the message does not exist.

BOOL cancelResult = [[RCCoreClient sharedCoreClient] cancelDownloadMediaMessage:messageId];

Downloading Network Media Files

IMLib SDK provides a media file downloader that can download files via network URL addresses without modifying the message body.

Downloading Network Media Files via URL

You can use the downloadMediaFile:mediaUrl:progress:success:error:cancel: method to download files via the remote URL of media files. This method does not modify the message body. The returned mediaPath will include the media filename specified in the parameters.

Interface Prototype



- (void)downloadMediaFile:(NSString *)fileName
mediaUrl:(NSString *)mediaUrl
progress:(nullable void (^)(int progress))progressBlock
success:(nullable void (^)(NSString *mediaPath))successBlock
error:(nullable void (^)(RCErrorCode errorCode))errorBlock
cancel:(nullable void (^)(void))cancelBlock;
ParameterTypeDescription
fileNamemediaUrlSpecified filename, must include file extension (e.g. rongCloud.mov).
mediaUrlNSStringRemote URL of the media file to download.
progressBlockBlockDownload progress callback, returns int type progress (range [0, 100]).
successBlockBlockDownload success callback, returns local path as NSString.
errorBlockBlockDownload failure callback, returns error code [RCErrorCode].
cancelBlockBlockDownload cancellation callback.

Sample Code

[[RCCoreClient sharedCoreClient] downloadMediaFile:@"filename.png"
mediaUrl:@"http://remote.url"
progress:^(int progress) { }
success:^(NSString *mediaPath) { }
error:^(RCErrorCode errorCode) { }
cancel:^{ } ];

You can use the downloadMediaFile:targetId:mediaType:mediaUrl:progress:success:error:cancel: method to download images via their network URLs. The IMLib SDK automatically generates filenames by combining the input parameters (conversation type, targetId, and media type), and returns them in mediaPath.

tip

This method only supports image downloads.

Method Prototype



- (void)downloadMediaFile:(RCConversationType)conversationType
targetId:(NSString *)targetId
mediaType:(RCMediaType)mediaType
mediaUrl:(NSString *)mediaUrl
progress:(nullable void (^)(int progress))progressBlock
success:(nullable void (^)(NSString *mediaPath))successBlock
error:(nullable void (^)(RCErrorCode errorCode))errorBlock
cancel:(nullable void (^)(void))cancelBlock;

Parameter Description

ParameterTypeDescription
conversationType[RCConversationType]Conversation type
targetIdNSStringConversation targetId
mediaType[RCMediaType]Media type (only MediaType_IMAGE is supported for this method).
mediaUrlNSStringRemote URL of the image.
progressBlockBlockDownload progress callback, returns int type progress (range [0, 100]).
successBlockBlockDownload success callback, returns local path as NSString.
errorBlockBlockDownload failure callback, returns error code [RCErrorCode].
cancelBlockBlockDownload cancellation callback.

Sample Code

[[RCCoreClient sharedCoreClient] downloadMediaFile:ConversationType_GROUP
targetId:@"targetId"
mediaType:MediaType_IMAGE
mediaUrl:@"remoteurl"
progress:^(int progress) { }
success:^(NSString *mediaPath) { }
error:^(RCErrorCode errorCode) { }
cancel:^{ }];

Pausing Media File Downloads

tip

Pause functionality is only supported in v5.6.6 and later.

You can pause media file downloads in progress. To resume, call the download method again - the SDK supports resumable downloads.

Sample Code

[[RCCoreClient sharedCoreClient] pauseDownloadMediaUrl:mediaUrl
successBlock:^{
// Handle pause success
}
errorBlock:^(RCErrorCode errorCode) {
// Handle pause error
}];

Canceling Media File Downloads

tip

Starting from IMLib SDK v5.6.6, asynchronous cancellation is supported - the synchronous method is deprecated.

Sample Code

(SDK version ≥ 5.6.6) Asynchronously cancel a media file download. If [RCErrorCode] returns -3/OPERATION_MEDIA_NOT_FOUND, the download task has already completed.

[[RCCoreClient sharedCoreClient] cancelDownloadMediaUrl:mediaUrl
successBlock:^{
// Handle cancellation success
}
errorBlock:^(RCErrorCode errorCode) {
// Handle cancellation error
}];

(SDK version < 5.6.6) Synchronously cancel a media file download. Returns YES if successful. Returns NO if failed (download already completed or message doesn't exist).

BOOL cancelResult = [[RCCoreClient sharedCoreClient] cancelDownloadMediaUrl:@"remoteurl"];