發短信的功能對於一個須要渠道擴展的APP來講,必不可少。可是,當第一次看到這個需求時,我卻一臉懵逼,由於以前並無接觸過,出於對未知事物的恐懼,被分配作這個任務的時候其實我是拒絕的,可是,沒辦法誰讓我是小兵一個呢,只能硬着頭皮強上了。在查閱了一番資料以後,發現這個功能作起來其實很是簡單,不到十分鐘就能夠解決。下面咱們就來聊一下如何實現這個功能。html
首先,咱們來介紹一個最爲簡單的方法,只須要一行代碼就能夠搞定,代碼以下:數組
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://800888"]];
複製代碼
可是,這段代碼雖然簡單,缺陷卻也明顯,這段代碼屬於程序外部調用,也就是跳出app程序自己,利用手機短信功能發送短信,在發送短信頁面點擊取消按鈕時,回到的是手機短信功能頁面,而不是app程序。並且,此方法也不能傳入默認的短信文本,須要本身手動輸入,極可能沒法知足需求。因此:bash
咱們須要知道的一點是,蘋果官方在發送短信功能方面有一個框架,叫作MessageUI,此框架能夠解決上述一切問題,並且輕鬆方便容易實現,下面咱們就來介紹一下這個框架的使用:app
在吃大餐以前,讓咱們先來幾道前菜:框架
2.導入頭文件:#import <MessageUI/MessageUI.h>編輯器
3.添加協議: ide
添加完協議以後,咱們首先想到的就是實現協議方法,但是還不能急,咱們還得檢測一下設備是否能夠發送短信。以下:ui
- (void)showSMSPicker:(id)sender
{
/**
您必須檢查當前設備是否能夠在嘗試建立一個MFMessageComposeViewController的實例以前發送短信
。 若是設備沒法發送短信,
[[MFMessageComposeViewController alloc] init]將返回nil。 您的應用程式用一個空視圖控制器調用
-presentViewController時會致使崩潰。
**/
if ([MFMessageComposeViewController canSendText])
// The device can send email.
{
[self displaySMSComposerSheet];
}
else
// The device can not send email.
{
self.feedbackMsg.hidden = NO;
self.feedbackMsg.text = @"Device not configured to send SMS.";
}
}
- (void)displaySMSComposerSheet
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
//您能夠指定一個或多個預配置的收件人。 用戶有從消息編輯器視圖中刪除或添加收件人的選項控制器
//您能夠指定將出如今消息編輯器視圖控制器中的初始消息文本。
picker.recipients = @[@"Phone number here"];//發短信的手機號碼的數組,數組中是一個即單發,多個即羣發。
picker.body = @"Hello from California!"; //短信主體內容
[self presentViewController:picker animated:YES completion:NULL];
}
複製代碼
檢測是否能夠發送短信,仍是須要有一個觸發事件的,代碼以下:spa
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(140, 100, 100, 100)];
button.backgroundColor = [UIColor blackColor];
[button setTitle:@"發送短信" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(showSMSPicker:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UILabel * feedbackMsg = [[UILabel alloc]initWithFrame:CGRectMake(10, 300, self.view.frame.size.width -20, 30)];
feedbackMsg.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:feedbackMsg];
self.feedbackMsg = feedbackMsg;
}
複製代碼
嗯,到了實現協議方法的時候了:code
// -------------------------------------------------------------------------------
// messageComposeViewController:didFinishWithResult:
// Dismisses the message composition interface when users tap Cancel or Send.
// Proceeds to update the feedback message field with the result of the
// operation.
// 當用戶點擊取消或發送時,關閉消息組合界面。
// 收到更新反饋消息字段的結果操做。
// -------------------------------------------------------------------------------
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result
{
self.feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
// 通知用戶與界面相關的錯誤
switch (result)
{
case MessageComposeResultCancelled: //取消
self.feedbackMsg.text = @"Result: SMS sending canceled";
break;
case MessageComposeResultSent: //發送
self.feedbackMsg.text = @"Result: SMS sent";
break;
case MessageComposeResultFailed: //失敗
self.feedbackMsg.text = @"Result: SMS sending failed";
break;
default: //默認
self.feedbackMsg.text = @"Result: SMS not sent";
break;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
複製代碼
Demo運行結果以下:
至此,咱們的發送短信功能就實現了,是否是很簡單!可是,當年也是慫過啊,因此,特寫此文,以記念當年慫過的日子。
參考文章