How to get a pre-configured SMS for a user in the iOS SDK

I used the sample code on the Apple Dev website to find out how to set up pre-written emails, but is there a way to set pre-configured SMS messages in the same way?

+4
source share
4 answers

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

Now to send 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 a send 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]; } 
+7
source

The body property on the MFMessageComposeViewController allows you to set the message body in the same way as you can for email.

See documentation: http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMessageComposeViewController_class/Reference/Reference.html

+2
source

See this article about the Apple Dev Center:

SMS sending

+1
source

PresentModalViewController is now deprecated in iOS 6. So, I used

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

all code is as follows

 -(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]; } 
+1
source

Source: https://habr.com/ru/post/1391506/


All Articles