2

I'm new to iPhone development. I want to pass some string with additional SMS detail. I'm using the below code to send an SMS to a particular number but if I want to send some data with the SMS string then what should I do? Could anyone tell me the format for that?

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://9016098909891"]];
richsage
  • 26,912
  • 8
  • 58
  • 65
nids
  • 73
  • 1
  • 3
  • 10

2 Answers2

1

The SMS URL scheme does not provide any means of filling the SMS body. Thus your only option to pre-fill the sms is to use the MFMessageComposeViewController


You can set the text message body using the Messaging framework, you will need to added this to framework. Add the #import <MessageUI/MessageUI.h> to the import list.

make sure that the view controller implements MFMessageComposeViewControllerDelegate, this is needed to dismiss the MFMessageComposeViewController.

The implement for MFMessageComposeViewControllerDelegate :

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    [controller dismissModalViewControllerAnimated:YES];
}

Then in the method that will display the message composer place this code:

MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
messageController.body = @"Your message here.";
messageController.recipients = [NSArray arrayWithObject:@"9016098909891"];
[self presentModalViewController:messageController animated:YES];

[messageController release], messageController = nil;
rckoenes
  • 69,092
  • 8
  • 134
  • 166
1

You can use in app sms feature where you can set your SMS text body..

Step 1: Import the MessageUI Framework into your project and import the header file #import <MessageUI/MessageUI.h>

Step 2: send sms as follows

    MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
    if([MFMessageComposeViewController canSendText])
    {
        controller.body = @"Hello from Mugunth";
        controller.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil];
        controller.messageComposeDelegate = self;
            controller.delegate = self;
        [self presentModalViewController:controller animated:YES];
    }

Step 3: handle the delegates as

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    switch (result) {
        case MessageComposeResultCancelled:
            NSLog(@"Cancelled");
            break;
        case MessageComposeResultFailed:
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Unknown Error"
                                                           delegate:self cancelButtonTitle:@”OK” otherButtonTitles: nil];
            [alert show];
            [alert release];
            break;
        case MessageComposeResultSent:

            break;
        default:
            break;
    }

    [self dismissModalViewControllerAnimated:YES];
}

see the article here http://blog.mugunthkumar.com/coding/iphone-tutorial-how-to-send-in-app-sms/

Saurabh
  • 22,743
  • 12
  • 84
  • 133
  • why downvote?? Please always mention the reason in the comments if you are downvoting an answer.. – Saurabh Apr 23 '12 at 07:52
  • but i want to open that link in safari so i can not use MFMessageComposeViewController... I need to use @"http://sms://%@",phoneNumber but with some string attach to it – nids Apr 23 '12 at 09:25
  • 2
    @nids Why do you want to do it that way ?, see my anser since you can't set the text using the SMS url Scheme. – rckoenes Apr 23 '12 at 09:45