1. Add messageui. frameworkCopy the code
Import the header file#import <MessageUI/MessageUI.h>
Copy the code
3, if you need the callback is adding agent < MFMessageComposeViewControllerDelegate >Copy the code
4. Use // to check whether the device can send short messagesif([MFMessageComposeViewController canSendText]){ MFMessageComposeViewController*picker = [[MFMessageComposeViewController  alloc] init]; / / set the entrusted picker. MessageComposeDelegate = self; Picker.body = @"ABCD"; // The default recipient (multiple recipients) picker.recipients = @[@"12345678900"];
        
        [self presentViewController:picker animated:YES completion:nil];
        
    }elseUIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Tip" message:@"This device does not support SMS." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Sure", nil];
        [alert show];
    }
Copy the code
5, agents,#pragma mark MFMessageComposeViewControllerDelegate- (void)messageComposeViewController:(MFMessageComposeViewController *)controller DidFinishWithResult: (MessageComposeResult) result {/ / regardless of any state to return to the interface of before [controller dismissViewControllerAnimated: YES completion:nil]; NSString *message; switch (result){case MessageComposeResultCancelled:
        {
            NSLog(@"Unsend");
            message = @"Unsend";
        }
            break;
        case MessageComposeResultFailed:
        {
            NSLog(@"Send failed");
            message = @"Send failed";
        }
            break;
        case MessageComposeResultSent:
        {
            NSLog(@"Sent successfully");
            message = @"Sent successfully";
        }
            break;
            
        default:
            break;
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Tip" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Sure", nil];
    [alert show];
}
Copy the code