How to attach Image with message via iPhone application?
Asked Answered
F

6

10

I want to send message with image data. So I used MFMessageComposeViewController. But that controller provide only SMS service. So I used UIPasteBoard attached an image data. But It doesn't work, either. There are no "Paste" button created when typing messages. Attaching image at UIPasteBoard was clearly success. I think using MFMessageComposeViewController doesn't solve my problem. How can I accomplish my goal?

Fourteenth answered 26/8, 2010 at 17:8 Comment(1)
Make Sms url and then use [[UIApplication sharedApplication] openURL:url]; For sending image you need to manually paste the image using UIPasteBoard.Frighten
A
6

This is not possible with the current MessageUI API: the MSMessageComposeViewController doesn't accept attachments like the MFMailComposeViewController does.

The only way to do this currently is to use an external service that allows you to send mms via a REST call for example.

GSMA defines a REST specification for exactly this purpose: http://www.gsmworld.com/oneapi/reference_documentation-version_1.html (multiple pdf's on this page)

Try to find a local service provider that implements this specification and you're good to go.

Just to add the direct wiki link to the OneAPI MMS spec: http://gsma.securespsite.com/access/Access%20API%20Wiki/MMS%20RESTful%20API.aspx and a link to the PHP/Java sandbox https://github.com/OneAPI/GSMA-OneAPI where MMS can be tested locally . Cheers.

Amblygonite answered 9/2, 2011 at 9:33 Comment(0)
H
4

I had the same question that I posted here. There is a bug in MFMessageComposeViewController and if you just use the code below it will launch a message that you can insert images into

    NSString *phoneToCall = @"sms: 123-456-7890";
    NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];

    [[UIApplication sharedApplication] openURL:url];
Heyman answered 20/3, 2012 at 19:45 Comment(1)
even simpler, at least on an iPhone 4S, you can remove the fake phone number and the encoding - all you need is this: NSURL *url = [[NSURL alloc] initWithString:@"sms:"]; then [[UIApplication sharedApplication] openURL:url];Poriferous
D
4

Here is the correct working code and it is working perfectly on my device.

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.persistent = NO;

NSMutableDictionary *text = [NSMutableDictionary dictionaryWithCapacity:1];
[text setValue:label.text forKey:(NSString *)kUTTypeUTF8PlainText];

NSMutableDictionary *image = [NSMutableDictionary dictionaryWithCapacity:1];
[image setValue:imageView.image forKey:(NSString *)kUTTypePNG];

pasteboard.items = [NSArray arrayWithObjects:image,text, nil];

NSString *phoneToCall = @"sms:";
NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
[[UIApplication sharedApplication] openURL:url];
Dilapidated answered 30/12, 2013 at 10:43 Comment(2)
what is kUTTypeUTF8PlainText and kUTTypePNG?Malaysia
@Malaysia See this linkDilapidated
R
4

This Method is tested and verified. I Used it in my code.

if (![MFMessageComposeViewController canSendText]) {
    UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device not support SMS \nOr you hadn't login your iMessage" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alertV show];
    return;
}

MFMessageComposeViewController *mVC = [[MFMessageComposeViewController alloc] init];
mVC.body = @"jjjj";
mVC.recipients = @[@"00XXXXXXXXXX"];
mVC.messageComposeDelegate = self;
if ([MFMessageComposeViewController canSendAttachments]) {
    NSLog(@"ok");
}
[mVC addAttachmentData: UIImageJPEGRepresentation([UIImage imageNamed:@"test.jpg"], 1.0) typeIdentifier:@"public.data" filename:@"image.jpeg"];

[self presentViewController:mVC animated:YES completion:nil];

You can use any jpeg jpg and png formats.

Ruff answered 19/12, 2014 at 20:32 Comment(1)
in my code it never goes into if ([MFMessageComposeViewController canSendAttachments]) { NSLog(@"ok"); } Do i need to set anyother settings?Helminth
B
4

Swift way. Works in iOS11

func shareViaMessage() {
    if !MFMessageComposeViewController.canSendText() {
        showAlert("Text services are not available")
        return
    }

    let textComposer = MFMessageComposeViewController()
    textComposer.messageComposeDelegate = self
    textComposer.body = "Try my #app"

    if MFMessageComposeViewController.canSendSubject() {
        textComposer.subject = "AppName"
    }

    if MFMessageComposeViewController.canSendAttachments() {
        let imageData = UIImageJPEGRepresentation(imageView.image!, 1.0)
        textComposer.addAttachmentData(imageData!, typeIdentifier: "image/jpg", filename: "photo.jpg")
    }

    present(textComposer, animated: true)
}
Brewington answered 14/12, 2017 at 9:43 Comment(0)
O
0

Why don't you share the Image and Text via the Share API (selecting Message, and if you want exluding Facebook, twitter etc..)

Obligatory answered 17/12, 2015 at 16:33 Comment(1)
To have a different lookAlbright

© 2022 - 2024 — McMap. All rights reserved.