Reputation: 1054
Would this be possible?
The app will have a button and a Text Field or a Text View.
The user types in the phone number in the Text Field or Text View. when the user is done the user presses the button which will open the Message or SMS app in the iPhone.
How would I do this? If possible please provide some code! :)
Thank you in advance!
Upvotes: 3
Views: 14780
Reputation: 359
In Swift 3.0
static func sendMessageToNumber(number:String,message:String){
let sms = "sms:\(number)&body=\(message)"
let url = URL(string:sms)!
let shared = UIApplication.shared
if(shared.canOpenURL(url)){
shared.openURL(url)
}else{
print("unable to send message")
}
}
Upvotes: 0
Reputation: 2211
Another short way, may help anyone much better :)
NSString *sms = @"sms:+1234567890&body=This is sms body.";
NSString *url = [sms stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
Upvotes: 7
Reputation: 8412
Take a look at the MessageComposer Sample App and the MFMessageComposeViewController Class.
You then do something like this, though you should first check whether the MFMessageComposeViewController
is actually available on your device (See the MessageComposer Sample):
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
picker.recipients = [NSArray arrayWithObjects:@"1234", @"2345", nil];
picker.body = yourTextField.text
[self presentModalViewController:picker animated:YES];
[picker release];
You need to first import the MessageUI.framework
(see this answer).
Import it into your classes via #import <MessageUI/MessageUI.h>
and add <MFMessageComposeViewControllerDelegate>
in the .h file, e.g. like so:
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
@interface YourClass : UIViewController <MFMessageComposeViewControllerDelegate>
{
// ...
Upvotes: 8