1. 程式人生 > >iOS獲取系統簡訊,傳送指定簡訊內容

iOS獲取系統簡訊,傳送指定簡訊內容

現在在開發中應該會經常遇到點選邀請 傳送簡訊給指定人員
這裡寫圖片描述
點選邀請 彈出簡訊介面 併發送指定內容,手動點擊發送即可

如果只要開啟簡訊介面,並顯示號碼很簡單,一句程式碼

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@sms://13666666666]];

但要指定簡訊內容 需要如下方法
1.先引入標頭檔案

#import <MessageUI/MFMessageComposeViewController.h>

2.設定代理

MFMessageComposeViewControllerDelegate

3.實現代理方法

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self.mobileContactsVC dismissViewControllerAnimated:YES completion:nil];

    if (result == MessageComposeResultCancelled)
    {
        NSLog(@"Message cancelled"
); } else if (result == MessageComposeResultSent) { NSLog(@"Message sent"); } else { NSLog(@"Message failed"); } }
  1. 呼叫方法
/**
 *  指定內容 開啟SMS
 *
 *  @param bodyOfMessage 要傳送的內容
 *  @param recipients    要傳送的號碼
 */
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];

    if
([MFMessageComposeViewController canSendText]) { controller.body = bodyOfMessage; controller.recipients = recipients; controller.messageComposeDelegate = self; [self.mobileContactsVC presentViewController:controller animated:YES completion:nil]; } }``` 只要在需要呼叫的地方 呼叫方法即可
[self sendSMS:@"加入工付寶 領取紅包金額" recipientList:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%@", self.mobileContactsModel.mobile], nil]];