There are multiple threads on stackoverflow, like this one: How to programmatically send SMS on the iPhone? That discuss on how to send an SMS message to specified list of recipients. But I really need to somehow let the user select the recipients to share some info from my app (ideally I want also to show an image besides text body), is there a way to do this? I know I can do such a thing with Facebook SDK, but here I want to use SMS/default messenger. And I am using Objective C for my development.
Asked
Active
Viewed 544 times
0
-
Have you made any attempt with `MFMessageComposeViewController`? Update your question with relevant code. – rmaddy Nov 27 '15 at 22:23
-
I need to set up recipients that were selected by the user. This is the functionality I am looking for. Yes, I selected to use `MFMessageComposeViewController`, but in all of the examples I have seen on stackoverflow and elsewhere they do not mention `where` they get the phone numbers to send sms to, they just say how to set the numbers. – Nikita Vlasenko Nov 27 '15 at 23:22
-
In other words, my issue is that I do not want to set the numbers myself, but by the user. I am confused about whether it is required to set the recipients or not. – Nikita Vlasenko Nov 27 '15 at 23:24
-
2Simply present the `MFMessageComposeViewController`. Don't set any recipients. Now see what the user can do. Try it. – rmaddy Nov 27 '15 at 23:27
-
1`MFMessageComposeViewController ` draws contacts from a user's address book. It's the same interface as if you began drafting a new message in Messages. – Chris Droukas Nov 27 '15 at 23:31
-
@rmaddy, why do any work yourself? That's what we're for.
– Duncan C Nov 27 '15 at 23:41 -
I see, I do not know Messages. So, does it mean that I can just skip recipients part? Could you provide any links? – Nikita Vlasenko Nov 27 '15 at 23:46
-
@Duncan C I do not ask for code, just for some guidance. I searched extensively online, not only on stackoverflow, and was not able to figure it out. That's why I am asking. Apple docs did not provide me with these details either. – Nikita Vlasenko Nov 27 '15 at 23:53
-
@rmaddy OK, thank you! The method `[MFMessageComposeViewController canSendText]` returns `false` on the iOS simulator. That's the problem. Can I test it on the simulator (avoiding deprecated methods) ? – Nikita Vlasenko Nov 27 '15 at 23:58
-
OK, I guess, not. I just want to test the functionality and do not have iPhone. – Nikita Vlasenko Nov 28 '15 at 00:01
-
1Some things have to be tested on real devices. In fact, the whole app really must be tested on a real device before you send the app to Apple. – rmaddy Nov 28 '15 at 00:01
-
1This is not an app you can develop on the sim. You are going to need a device. You might borrow an older iPod touch, but you must have an iOS device. – Duncan C Nov 28 '15 at 00:16
1 Answers
1
As the comments said, MFMessageComposeViewController
includes a built-in contact picker that's linked to the user's address book. If you already know the addresses of the recipients you'd like to send to, you can set MFMessageComposeViewController
yourself. You could (for example) use the AddressBook
framework and pull numbers out yourself, but that's going to involve:
- Asking the user for permission
- Displaying an interface for the user to select numbers
- Feeding that information to
MFMessageComposeViewController
I would avoid doing this unless you have a compelling reason to do so.
Here's an example that sends a message to two recipients along with a message and an attachment.
- (void)presentMessageViewController {
if ([MFMessageComposeViewController canSendText] &&
[MFMessageComposeViewController canSendAttachments]) {
MFMessageComposeViewController *messageViewController = [MFMessageComposeViewController new];
messageViewController.messageComposeDelegate = self;
messageViewController.recipients = @[@"867-5309",
@"1-800-MIX-A-LOT"];
messageViewController.body = @"Hey, Jenny and Sir Mix-A-Lot!";
[messageViewController addAttachmentData:UIImagePNGRepresentation([UIImage imageNamed:@"Funny Cats"])
typeIdentifier:@"public.data"
filename:@"image.png"];
[self presentViewController:messageViewController
animated:YES
completion:^{
nil;
}];
}
}
Again, unless you already know the addresses of recipients, the recipients property is optional. The user will input that information in MFMessageComposeViewController
themselves.

Chris Droukas
- 3,196
- 1
- 23
- 26