1. 程式人生 > >iOS呼叫系統發簡訊功能詳解

iOS呼叫系統發簡訊功能詳解

iOS呼叫系統的發簡訊功能可以分為兩種:1,程式外呼叫系統發簡訊。2,程式內呼叫系統發簡訊。第二種的好處是使用者發簡訊之後還可以回到app。這對app來說非常重要。

程式外呼叫系統發簡訊

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

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

程式內呼叫系統發簡訊

1)匯入MessageUI.framework,並引入標頭檔案:
#import <MessageUI/MessageUI.h>
2)實現代理方法MFMessageComposeViewControllerDelegate
    -(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;
    }
}
3)傳送簡訊
    -(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];
    }
}

引數phones:發簡訊的手機號碼的陣列,陣列中是一個即單發,多個即群發。

4)呼叫發簡訊的方法
    [self showMessageView:[NSArray arrayWithObjects:@"13888888888",@"13999999999", nil] title:@"test" body:@"你是土豪麼,麼麼噠"];

我的微信iOS開發:iOSDevTip