兩種iOS調用系統發簡訊的方法_IOS

來源:互聯網
上載者:User

一、程式外調用系統發簡訊

這個方法其實很簡單,直接調用openURL即可:

NSURL *url = [NSURL URLWithString:@"sms://15888888888"];[[UIApplication sharedApplication]openURL:url];

二、程式內調用系統發簡訊

這種方法有一個好處就是使用者發簡訊之後還可以回到App.

首先要匯入MessageUI.framework,並引入標頭檔:

#import <MessageUI/MessageUI.h>

然後要遵循代理MFMessageComposeViewControllerDelegate,並實現代理方法。

#pragma mark - 代理方法-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{  [self dismissViewControllerAnimated:YES completion:nil];  switch (result) {    case MessageComposeResultSent:      //資訊傳送成功             break;    case MessageComposeResultFailed:      //資訊傳送失敗             break;    case MessageComposeResultCancelled:      //資訊被使用者取消傳送             break;    default:      break;  }}

傳送簡訊方法實現

#pragma mark - 傳送簡訊方法-(void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body{  if( [MFMessageComposeViewController canSendText] )  {    MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];    controller.recipients = phones;    controller.navigationBar.tintColor = [UIColor redColor];    controller.body = body;    controller.messageComposeDelegate = self;    [self presentViewController:controller animated:YES completion:nil];    [[[[controller viewControllers] lastObject] navigationItem] setTitle:title];//修改簡訊介面標題  }  else  {    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示資訊"                            message:@"該裝置不支援簡訊功能"                            delegate:nil                       cancelButtonTitle:@"確定"                       otherButtonTitles:nil, nil];    [alert show];  }}

最後,調用傳送簡訊的方法

複製代碼 代碼如下:

[self showMessageView:[NSArray arrayWithObjects:@"15888888888",@"12399999999", nil] title:@"test" body:@"這是測試用簡訊,勿回複!"];

以上就是小編給大家介紹的iOS調用系統發簡訊的兩種方法,希望對大家有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.