Download media files
The Chat SDK supports downloading media files from media messages.
Media message types
Built-in media message types include NCFileMessage, NCImageMessage, NCGIFMessage, NCHDVoiceMessage, and NCShortVideoMessage.
Download from a media message
After downloading, the localPath property of the media content is updated automatically.
- Swift
- Objective-C
swift
import NexconnChatSDK
message.downloadMedia(
progressHandler: { progress in
// 0-100
},
successHandler: { mediaPath in
// Downloaded to mediaPath
},
errorHandler: { error in
// Failed
},
cancelHandler: {
// Cancelled
}
)
Objective C
[message downloadMediaWithProgressHandler:^(int progress) {
// 0-100
}
successHandler:^(NSString *mediaPath) {
// Downloaded to mediaPath
}
errorHandler:^(NCError *error) {
// Failed
}
cancelHandler:^{
// Cancelled
}];
Pause download
- Swift
- Objective-C
swift
import NexconnChatSDK
message.pauseDownloadingMedia { error in
// Paused
}
Objective C
[message pauseDownloadingMediaWithCompletion:^(NCError *error) {
// Paused
}];
Cancel download
- Swift
- Objective-C
swift
import NexconnChatSDK
message.cancelDownloadingMedia { error in
// Cancelled
}
Objective C
[message cancelDownloadingMediaWithCompletion:^(NCError *error) {
// Cancelled
}];
Download by URL
- Swift
- Objective-C
swift
import NexconnChatSDK
let info = BaseChannel.getMediaDownloadInfo(mediaUrl: "https://example.com/path/to/media")
if let info {
print("Progress: \(info.progress)")
print("Local path: \(info.localPath ?? "")")
}
Objective C
NCMediaDownloadInfo *info =
[NCBaseChannel getMediaDownloadInfoWithMediaUrl:@"https://example.com/path/to/media"];
if (info != nil) {
NSLog(@"Progress: %d", info.progress);
NSLog(@"Local path: %@", info.localPath);
}
Use +[NCBaseChannel getMediaDownloadInfoWithMediaUrl:] to inspect an existing media download task by remote URL. To start, pause, or cancel a download, use the instance methods on NCMessage.