21

I want to attach an image to a MMS, on iOS7. I wrote following code:

    MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
    messageController.messageComposeDelegate = self;

    NSData *imgData = [NSData dataWithContentsOfFile:@"blablabla"];
    BOOL didAttachImage = [messageController addAttachmentData:imgData typeIdentifier:@"public.data" filename:@"image"];

    if (didAttachImage)
    {
        // Present message view controller on screen
        [self presentViewController:messageController animated:YES completion:nil];
    }
    else
    {
        UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                               message:@"Failed to attach image"
                                                              delegate:nil
                                                     cancelButtonTitle:@"OK"
                                                     otherButtonTitles:nil];
        [warningAlert show];
        return;
    }

The problem is that when the SMS screen is presented, it doesn't recognize the image, and cannot send it. I see something like this:

Message Screen

I believe this has something to do either with imgData I am sending, or with typeIdentifier.

Note: I tried almost all possible typeIdentifiers: @"public.data", @"public.image", @"public.item", ... etc. None worked.

Can anybody please help me? What is the typeIdentifier you are using? I am testing on iPhone 5, iOS 7.0.2.

Thanks.


SOLUTION:

As Greg instructed, this solved my problem: set filename as @"image.png", and typeIdentifier to kUTTypePNG.

[messageController addAttachmentData:imgData typeIdentifier:(NSString *)kUTTypePNG filename:@"image.png"];

Thanks Greg.

Beny Boariu
  • 736
  • 2
  • 11
  • 26
  • I am getting didAttachImage = YES but i can't see any attached image in my presented SMS screen . I am testing on Iphone 6plus, iOS 8.4.1 – Niharika Jan 11 '17 at 07:22

2 Answers2

38

The MFMessageComposeViewController wants the attachment to have the correct extension for the type of image you're uploading. I verified by testing with a PNG file, and the following variations of adding the attachment data:

[messageController addAttachmentData:imgData typeIdentifier:@"public.data" filename:@"image"];
[messageController addAttachmentData:imgData typeIdentifier:@"public.data" filename:@"image.abc"];
[messageController addAttachmentData:imgData typeIdentifier:@"public.data" filename:@"image.png"];

Only the last option worked. I didn't need to change the typeIdentifier, although it probably would make sense to choose a UTI that matches the type of data.

The full list of UTIs is available here: System-Declared Uniform Type Identifiers (Thanks @iWasRobbed!)

Greg
  • 33,450
  • 15
  • 93
  • 100
  • The entire list can be found here: https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1 – iwasrobbed Oct 03 '13 at 23:05
  • Tried both of them. Still not working. UIImage *img = [UIImage imageWithContentsOfFile:image.i_filePath]; NSData *imgData = UIImagePNGRepresentation(img); BOOL didAttachImage = [messageController addAttachmentData:imgData typeIdentifier:(NSString *)kUTTypePNG filename:@"image"]; – Beny Boariu Oct 03 '13 at 23:06
  • 1
    Come on, that's too stupid. Changed filename to "image.png", and now it's working. Lol. Can't believe. Thanks Greg. – Beny Boariu Oct 03 '13 at 23:10
  • Nice. :-) Glad I could help! – Greg Oct 03 '13 at 23:12
  • 2
    Thank you! Just a sidenote, this works for videos too, just name the file `video.mp4` or `video.mov` and so on. – Linuxios Oct 24 '13 at 15:21
2

For Swift You can try this

if (MFMessageComposeViewController.canSendText()) {
     
     let controller = MFMessageComposeViewController()
     controller.body = "Solution for broken image in composer"
     controller.messageComposeDelegate = self
        
      if image.imageAsset != nil {
            
            let imageData = UIImageJPEGRepresentation(self.fixOrientation(img: image), 1)  //! as NSData
            controller.addAttachmentData(imageData! , typeIdentifier: "image/.jpeg", filename: "image.jpeg")
            
        }
        
        viewController.present(controller, animated: true,completion: {
           
        })
}
    
Arnab
  • 4,216
  • 2
  • 28
  • 50
GDeep
  • 39
  • 7