In fact, there are two ways to send SMS
Method 1:
The method of openURL in UIApplication
As follows:
UIApplication *app = [UIApplication sharedApplication]; [app openURL:[NSURL URLWithString:@"sms://13007310000"]];
This method is not very advanced, because only the contact can be written. Here is a method to edit the content and the receiver directly!
Method two:
First step
First, import the framework MessageUI.framework
The second step
Import header file ා import < messageui / messageui. H >
The third step
Signing agreement < mfmessagecomposeviewcontrollerdelegate >
And implement the protocol method
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { NSLog(@"%s",__func__); switch (result) { case MessageComposeResultSent: NSLog(@"MessageComposeResultSent"); break; case MessageComposeResultFailed: NSLog(@"MessageComposeResultFailed"); break; case MessageComposeResultCancelled: NSLog(@"MessageComposeResultCancelled"); break; default: break; } [controller dismissViewControllerAnimated:YES completion:nil]; }
The fourth step
Judge whether the device supports sending SMS. If it supports sending SMS, it will jump into the SMS interface and edit the content and receiver of the SMS. If it does not support sending SMS, there will be a pop-up prompt!
UIViewController * VC; if ([MFMessageComposeViewController canSendText]) { MFMessageComposeViewController* composeVC = [[SMSViewController alloc] init]; composeVC.messageComposeDelegate = self; composeVC.recipients = @[@"13007310000"]; composeVC.body = @"Hello from California!"; VC = composeVC; }else{ UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"Reminder" message:@"Your device does not support SMS" preferredStyle:UIAlertControllerStyleAlert]; VC = alertVC; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [alertVC dismissViewControllerAnimated:YES completion:nil]; }); } [self presentViewController:VC animated:YES completion:nil];
But I ask a method, can send the short message directly, for this method can only be edited, and finally the user has to click the send button, is there any way to send it directly! Hope you can leave a message!!