How to have a precomposed SMS message for the user

2019-06-27 11:31发布

I used the sample code on the Apple Dev site to learn how to set pre-composed emails, but is there a way to set precomposed SMS messages, similarly?

4条回答
beautiful°
2楼-- · 2019-06-27 11:45

PresentModalViewController is now deprecated in IOS 6. So i used

[self presentViewController:controller animated:YES completion:nil];

whole code is as following

-(IBAction)sendSMSButtonTouchupInside:(id)sender
{
MFMessageComposeViewController *controller =
        [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
  {
    controller.body = @"Whatever you want";
    controller.recipients = [NSArray arrayWithObjects:@"03136602888", nil];
    controller.messageComposeDelegate = self;
    [self presentViewController:controller animated:YES completion:nil];
  }
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller   didFinishWithResult:(MessageComposeResult)result
{
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Unknown Error"
                                               delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
  switch (result)
  {
    case MessageComposeResultCancelled:
         NSLog(@"Cancelled");
         [alert show];
         break;
    case MessageComposeResultFailed:
         [alert show];
         break;
    case MessageComposeResultSent:
         break;
    default:
         break;
  }
  [self dismissViewControllerAnimated:YES completion:nil];
}
查看更多
3楼-- · 2019-06-27 11:59

First you have to add the framework MessageUI to your project and import the library "MessageUI/MessageUI.h". Then conform to the protocol <MFMessageComposeViewControllerDelegate>.

Now to send an SMS:

- (IBAction) sendSMS:(id)sender
{
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
    if([MFMessageComposeViewController canSendText])
    {
        controller.body = @"The body of the SMS you want";
        controller.messageComposeDelegate = self;
        [self presentModalViewController:controller animated:YES];
    }
    [controller release];
}

To catch the result of the sending operation:

- (void) messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    switch(result)
    {
        case MessageComposeResultCancelled: break; //handle cancelled event
        case MessageComposeResultFailed: break; //handle failed event
        case MessageComposeResultSent: break; //handle sent event
    }
    [self dismissModalViewControllerAnimated:YES];
}
查看更多
疯言疯语
4楼-- · 2019-06-27 11:59

The body property on MFMessageComposeViewController allows you to set the message body just like you can for an email.

Check out the documentation: http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMessageComposeViewController_class/Reference/Reference.html

查看更多
冷血范
5楼-- · 2019-06-27 12:09

See this article on Apple's Dev Center:

Sending an SMS Message

查看更多
登录 后发表回答