創建App-傳發短訊
為了本App的傳送短訊功能,因此來實作!
首先需要在專案的框架中加入MessageUI Framework,便引入MessageUI/MessageUI.h
/**
Theme: Send Message
IDE: Xcode 5
Language: Objective C
Date: 103/05/31
Author: HappyMan
Blog: https://cg2010studio.wordpress.com/
*/
-(IBAction)messageButtonClicked:(UIButton *)button
{
MFMessageComposeViewController *mcvc = [[MFMessageComposeViewController alloc] init];
// 判斷裝置是否在可傳送訊息的狀態
if([MFMessageComposeViewController canSendText]) {
// 設定SMS訊息內容
mcvc.body = @"嗨~猜猜我是誰?我是快樂測試人!";
// 設定接傳送對象的號碼
mcvc.recipients = [NSArray arrayWithObjects:@"0912345678",nil];
// 設定代理
mcvc.messageComposeDelegate = self;
// 顯示controller的畫面
[self presentViewController:mcvc animated:YES completion:nil];
}
else {
NSLog(@"此設備無法發送簡訊⋯⋯");
}
}
#pragma mark - MFMessageComposeViewControllerDelegate
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
NSLog(@"%@", controller.recipients);
switch (result) {
case MessageComposeResultSent:
// 訊息傳送成功
NSLog(@"訊息傳送成功⋯⋯");
break;
case MessageComposeResultFailed:
// 訊息傳送失敗
NSLog(@"訊息傳送失敗⋯⋯");
break;
case MessageComposeResultCancelled:
// 訊息被使用者取消傳送
NSLog(@"訊息被使用者取消傳送⋯⋯");
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}