I want to upload an image to Salesforce database from my native iOS app built using Salesforce mobile sdk. I heard we should be making use of ContentDocument, ContentVersion & ContentDocumentLink objects of Salesforce to attach/upload any files.

I'm following this below link for reference which shows implementation in apex.

Doubt in this block of code

String yourFiles = 'Lets assume this is your binary string of the files';  //what will be yourFiles in my case
ContentVersion conVer = new ContentVersion();
conVer.ContentLocation = 'S'; 
conVer.PathOnClient = 'ionicLogo.png';
conVer.Title = 'Proposal '; 
conVer.VersionData = EncodingUtil.base64Decode(yourFiles);  // What should be assigned to VersionData in Swift?
insert conVer;

The implementation that I tried in Swift

let imageName = UIImage(named: "TapToRead.png")        // Sample Image
guard let imageData = UIImagePNGRepresentation(imageName!) else { return }

print("imageData: ", imageData)

let base64String = imageData.base64EncodedString()
print("base64String: ", base64String)

let conVer = ContentVersionSObjectData()
conVer.location = "E"                 // S specify this document is in SF, use E for external files
conVer.pathOnClient = "TapToRead.png" // The files name
conVer.title = "Tap To Read Image"    // Display name of the files
conVer.verisonData = base64String     // for now I'm assigning the base64 string of my image to VersionData 
Managers.shared.CVDataManager?.updateRemoteData({ (status: SFSyncState?) in
if status!.isDone() {
    Logger.log(.debug, "ConVer update successfull")
} else {
    Logger.log(.error, "ConVer update failed")
    Utils.showAlert("Error", message: "Could not create ConVer")

I have tried something like above but still I'm not sure whether it's right or not. I'm getting ConVer update successful and it's not giving me any error but I don't see any record getting created on the ContentVersion object on Salesforce end.

Note: VersionData field is of base64 dataType on the ContentVersion Salesforce object. I have defined VersionData as String in my project ContentVersionSObjectData.

I should be sending the base64 string of my image in VersionData field or blob? If blob how to get the blob?What is the mistake I'm doing?Any help would be appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.