How to attach Image with message via iPhone application?
I want to send message with image data. So I used MFMessageComposeViewController
.
But that controller provide only SMS service. So I used UIPasteBoard
attached 开发者_StackOverflow中文版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?
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.
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];
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];
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.
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)
}
Why don't you share the Image and Text via the Share API (selecting Message, and if you want exluding Facebook, twitter etc..)
精彩评论