如何通过 iPhone 应用程序在消息中附加图像?

发布于 2024-09-16 10:45:52 字数 240 浏览 5 评论 0原文

我想发送带有图像数据的消息。所以我使用了 MFMessageComposeViewController 。 但该控制器仅提供短信服务。所以我使用 UIPasteBoard 附加了图像数据。 但它也不起作用。键入消息时不会创建“粘贴”按钮。在 UIPasteBoard 上附加图像显然是成功的。 我认为使用 MFMessageComposeViewController 并不能解决我的问题。 我怎样才能实现我的目标?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

将军与妓 2024-09-23 10:45:52

这在当前的 MessageUI API 中是不可能的:MSMessageComposeViewController 不像 MFMailComposeViewController 那样接受附件。

当前执行此操作的唯一方法是使用外部服务,该服务允许您通过 REST 调用发送彩信。

GSMA 正是为此目的定义了 REST 规范:
http://www.gsmworld.com/oneapi/reference_documentation-version_1.html(此内容有多个 pdf 文件)页)

尝试找到实现此规范的本地服务提供商,然后就可以了。

只需将直接 wiki 链接添加到 OneAPI MMS 规范: http://gsma .securespsite.com/access/Access%20API%20Wiki/MMS%20RESTful%20API.aspx 以及 PHP/Java 沙箱的链接 https://github.com/OneAPI/GSMA-OneAPI 可以在本地测试彩信。干杯。

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.

阳光的暖冬 2024-09-23 10:45:52

这是正确的工作代码,它在我的设备上完美运行。

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

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];
紫﹏色ふ单纯 2024-09-23 10:45:52

我有同样的问题,我在此处发布。 中有一个错误MFMessageComposeViewController 如果您只使用下面的代码,它将启动一条消息,您可以将图像插入其中

    NSString *phoneToCall = @"sms: 123-456-7890";
    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];
无可置疑 2024-09-23 10:45:52

本方法经过测试和验证。我在我的代码中使用了它。

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

您可以使用任何 jpeg、jpg 和 png 格式。

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.

国产ˉ祖宗 2024-09-23 10:45:52

快捷的方式。适用于 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)
}

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)
}
驱逐舰岛风号 2024-09-23 10:45:52

为什么不通过 Share API 共享图像和文本(选择“消息”,如果您想排除 Facebook、Twitter 等......)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文