3

Does the Mobile SDK provide any methods which allow uploading an attachment (image) to Salesforce? I am using 3.0 iOS SDK and building a native app

1 Answer 1

5

You can use the requestForUploadFile method. Try this

  • Use the UIImagePickerControllerDelegate delegate
  • Set your image source to UIImagePickerControllerSourceTypePhotoLibrary
  • Convert image data to NSData and then upload using SFRest API requestForUploadFile

Some sample code (may require some tweaks to run though)

@interface RootViewController : UITableViewController <SFRestDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>

- (void) uploadImage {
    UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
    imgPicker.delegate = self;
    imgPicker.allowsEditing = YES;
    imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:imgPicker animated:YES completion:NULL];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;
    NSData *imageData= UIImageJPEGRepresentation(chosenImage,0.0);

    SFRestRequest *request =
    [[SFRestAPI sharedInstance] requestForUploadFile:imageData
                                                name:@"myimage.png"
                                         description:@"Test Img"
                                            mimeType:@"image/png"];
    [[SFRestAPI sharedInstance] send:request delegate:self];
    [picker dismissViewControllerAnimated:YES completion:NULL];
}
3
  • Thanks, can I upload a jpg using this?
    – user5094
    Feb 11, 2015 at 7:01
  • Yes, change mimeType:@"image/png"]; to mimeType:@"image/jpg"]; That should be it Feb 11, 2015 at 7:03
  • How do I store the uploaded file against a Salesforce Record (Eg: Case)? @user5094 Sep 4, 2023 at 1:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .