3

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!

Tapy
  • 1,044
  • 6
  • 17
  • 30

3 Answers3

8

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> 
{
    // ...
Community
  • 1
  • 1
fabian789
  • 8,348
  • 4
  • 45
  • 91
7

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]];
Mohd Asim
  • 2,171
  • 1
  • 18
  • 15
0

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")
        }
    }
Rohit Pathak
  • 359
  • 4
  • 16