iOS發(fā)送短信功能的實現(xiàn)代碼
發(fā)短信的功能對于一個需要渠道擴展的APP來說,必不可少。但是,當?shù)谝淮慰吹竭@個需求時,我卻一臉懵逼,因為之前并沒有接觸過,出于對未知事物的恐懼,被分配做這個任務的時候其實我是拒絕的,但是,沒辦法誰讓我是小兵一個呢,只能硬著頭皮強上了。在查閱了一番資料之后,發(fā)現(xiàn)這個功能做起來其實非常簡單,不到十分鐘就可以解決。下面我們就來聊一下如何實現(xiàn)這個功能。
首先,我們來介紹一個最為簡單的方法,只需要一行代碼就可以搞定,代碼如下:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://800888"]];
但是,這段代碼雖然簡單,缺陷卻也明顯,這段代碼屬于程序外部調(diào)用,也就是跳出app程序本身,利用手機短信功能發(fā)送短信,在發(fā)送短信頁面點擊取消按鈕時,回到的是手機短信功能頁面,而不是app程序。而且,此方法也不能傳入默認的短信文本,需要自己手動輸入,很可能無法滿足需求。所以:
我們需要知道的一點是,蘋果官方在發(fā)送短信功能方面有一個框架,叫做MessageUI,此框架可以解決上述一切問題,而且輕松方便容易實現(xiàn),下面我們就來介紹一下這個框架的使用:
在吃大餐之前,讓我們先來幾道前菜:
導入框架:MessageUI.framework
2.導入頭文件:#import <MessageUI/MessageUI.h>
3.添加協(xié)議:<MFMessageComposeViewControllerDelegate>
添加完協(xié)議之后,我們首先想到的就是實現(xiàn)協(xié)議方法,可是還不能急,我們還得檢測一下設備是否可以發(fā)送短信。如下:
- (void)showSMSPicker:(id)sender { /** 您必須檢查當前設備是否可以在嘗試創(chuàng)建一個MFMessageComposeViewController的實例之前發(fā)送短信 。 如果設備無法發(fā)送短信, [[MFMessageComposeViewController alloc] init]將返回nil。 您的應用程式用一個空視圖控制器調(diào)用 -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; //您可以指定一個或多個預配置的收件人。 用戶有從消息編輯器視圖中刪除或添加收件人的選項控制器 //您可以指定將出現(xiàn)在消息編輯器視圖控制器中的初始消息文本。 picker.recipients = @[@"Phone number here"];//發(fā)短信的手機號碼的數(shù)組,數(shù)組中是一個即單發(fā),多個即群發(fā)。 picker.body = @"Hello from California!"; //短信主體內(nèi)容 [self presentViewController:picker animated:YES completion:NULL]; }
檢測是否可以發(fā)送短信,還是需要有一個觸發(fā)事件的,代碼如下:
- (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:@"發(fā)送短信" 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; }
嗯,到了實現(xiàn)協(xié)議方法的時候了:
// ------------------------------------------------------------------------------- // 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. // 當用戶點擊取消或發(fā)送時,關閉消息組合界面。 // 收到更新反饋消息字段的結果操作。 // ------------------------------------------------------------------------------- - (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: //發(fā)送 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運行結果如下:
至此,我們的發(fā)送短信功能就實現(xiàn)了,是不是很簡單!但是,當年也是慫過啊,所以,特寫此文,以紀念當年慫過的日子。
參考文章
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用Reachability類判斷iOS設備的當前網(wǎng)絡連接類型
這篇文章主要介紹了使用Reachability類判斷iOS設備的當前網(wǎng)絡連接類型,這里開發(fā)語言為傳統(tǒng)的Objectice-C,需要的朋友可以參考下2016-02-02IOS 開發(fā)自定義條形ProgressView的實例
這篇文章主要介紹了IOS 開發(fā)自定義條形ProgressView的實例的相關資料,希望開發(fā)自己的條形進度條的朋友可以參考下2016-10-10iOS實現(xiàn)不規(guī)則Button點擊效果實例代碼
這篇文章主要給大家介紹了關于iOS實現(xiàn)不規(guī)則Button點擊的相關資料,文中通過示例代碼介紹的非常詳細,對各位iOS開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-04-04