How to attach Image with message via iPhone application?

故事扮演 提交于 2019-12-17 10:35:12

问题


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?


回答1:


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.




回答2:


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];



回答3:


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];



回答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.




回答5:


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)
}



回答6:


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



来源:https://stackoverflow.com/questions/3577565/how-to-attach-image-with-message-via-iphone-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!