Expand Minimize Picture-in-picture Power Device Status Voice Recognition Skip Back Skip Forward Minus Plus Play Search
Internet Explorer alert
This browser is not recommended for use with smartdevicelink.com, and may not function properly. Upgrade to a different browser to guarantee support of all features.
close alert
To Top Created with Sketch. To Top
To Bottom Created with Sketch. To Bottom
iOS Guides
Uploading Files

Uploading Files

In almost all cases, you will not need to handle uploading images because the screen manager API will do that for you. There are some situations, such as VR help-lists and turn-by-turn directions, that are not currently covered by the screen manager so you will have manually upload the image yourself in those cases. For more information about uploading images, see the Uploading Images guide.

Uploading an MP3 Using the File Manager

The SDLFileManager uploads files and keeps track of all the uploaded files names during a session. To send data with the file manager you need to create either a SDLFile or SDLArtwork object. SDLFile objects are created with a local NSURL or NSData; SDLArtwork uses a UIImage.

NSData *mp3Data = <#Get the File Data#>;
SDLFile *audioFile = [SDLFile fileWithData:mp3Data name:<#File name#> fileExtension:<#File Extension#>];

[self.sdlManager.fileManager uploadFile:audioFile completionHandler:^(BOOL success, NSUInteger bytesAvailable, NSError * _Nullable error) {
    if (error != nil) { return; }
    <#File upload successful#>
}];
let mp3Data = <#Get MP3 Data#>
let audioFile = SDLFile(data: mp3Data, name: <#File name#>, fileExtension: <#File Extension#>)

sdlManager.fileManager.upload(file: audioFile) { (success, bytesAvailable, error) in
    guard error == nil else { return }
    <#File upload successful#>
}

Batching File Uploads

If you want to upload a group of files, you can use the SDLFileManager's batch upload methods. Once all of the uploads have completed you will be notified if any of the uploads failed. If desired, you can also track the progress of each file in the group.

SDLFile *file1 = [SDLFile fileWithData:<#Data#> name:<#File name to be referenced later#> fileExtension:<#File Extension#>];
SDLFile *file2 = [SDLFile fileWithData:<#Data#> name:<#File name#> fileExtension:<#File Extension#>];

[self.sdlManager.fileManager uploadFiles:@[file1, file2] progressHandler:^BOOL(NSString * _Nonnull fileName, float uploadPercentage, NSError * _Nullable error) {
    <#Called as each upload completes#>
    // Return true to continue sending files. Return false to cancel any files that have not yet been sent.
    return YES;
} completionHandler:^(NSError * _Nullable error) {
    <#Called when all uploads complete#>
}];
let file1 = SDLFile(data: <#File Data#>, name: <#File name#> fileExtension: <#File Extension#>)
let file2 = SDLFile(data: <#File Data#>, name: <#File name#> fileExtension: <#File Extension#>)

sdlManager.fileManager.upload(files: [file1, file2], progressHandler: { (fileName, uploadPercentage, error) -> Bool in
    <#Called as each upload completes#>
    // Return true to continue sending files. Return false to cancel any files that have not yet been sent.
    return true
}) { (fileNames, error) in
    <#Called when all uploads complete#>
}

File Persistence

SDLFile and its subclass SDLArtwork support uploading persistent files, i.e. files that are not deleted when the car turns off. Persistence should be used for files that will be used every time the user opens the app. If the file is only displayed for short time the file should not be persistent because it will take up unnecessary space on the head unit. You can check the persistence via:

BOOL isPersistent = file.isPersistent;
let isPersistent = file.isPersistent
Note

Be aware that persistence will not work if space on the head unit is limited. The SDLFileManager will always handle uploading images if they are non-existent.

Overwriting Stored Files

If a file being uploaded has the same name as an already uploaded file, the new file will be ignored. To override this setting, set the SDLFile's overwrite property to true.

file.overwrite = YES;
file.overwrite = true

Checking the Amount of File Storage Left

To find the amount of file storage left for your app on the head unit, use the SDLFileManager’s bytesAvailable property.

NSUInteger bytesAvailable = self.sdlManager.fileManager.bytesAvailable;
let bytesAvailable = sdlManager.fileManager.bytesAvailable

Checking if a File Has Already Been Uploaded

You can check out if an image has already been uploaded to the head unit via the SDLFileManager's remoteFileNames property.

BOOL isFileOnHeadUnit = [self.sdlManager.fileManager.remoteFileNames containsObject:<#Name Uploaded As#>];
let isFileOnHeadUnit = sdlManager.fileManager.remoteFileNames.contains(<#Name Uploaded As#>)

Deleting Stored Files

Use the file manager’s delete request to delete a file associated with a file name.

[self.sdlManager.fileManager deleteRemoteFileWithName:@"<#Name Uploaded As#>" completionHandler:^(BOOL success, NSUInteger bytesAvailable, NSError *error) {
    if (success) {
        <#File was deleted successfully#>
    }
}];
sdlManager.fileManager.delete(fileName: "<#Name Uploaded As#>") { (success, bytesAvailable, error) in
    if success {
        <#File was deleted successfully#>
    }
}

Batch Deleting Files

[self.sdlManager.fileManager deleteRemoteFilesWithNames:@[@"<#Name Uploaded As#>", @"<#Name Uploaded As 2#>"] completionHandler:^(NSError * _Nullable error) {
    if (error == nil) {
        <#Images were deleted successfully#>
    }
}];
sdlManager.fileManager.delete(fileNames: ["<#Name Uploaded As#>", "<#Name Uploaded As 2#>"]) { (error) in
    if (error == nil) {
        <#Files were deleted successfully#>
    }
}
View on GitHub.com
Previous Section Next Section