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

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];
}
  • Thanks, can I upload a jpg using this? – user5094 Feb 11 '15 at 7:01
  • Yes, change mimeType:@"image/png"]; to mimeType:@"image/jpg"]; That should be it – Gaurav Kheterpal Feb 11 '15 at 7:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.