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:@"你是土豪么,么么噠"];
簡版:
#pragma mark - MFMessageComposeViewControllerDelegate
///發送后或者cancel退回本頁面
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
[selfdismissViewControllerAnimated:YEScompletion:nil];
}
///編輯好內容跳轉到系統發送短信頁面
- (void)smsWihtMsg:(NSString*)msg phoneAry:(NSArray*)list{
if([MFMessageComposeViewController canSendText]) {
[AppConfig recordUserBehaviorCode:EVEN_ID_SEND_SMS codeDesc:@""nodeCode:nilassistantId:nil];
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
controller.body = msg;
controller.recipients = list;
controller.messageComposeDelegate =self;
[selfpresentViewController:controller animated:YEScompletion:nil];
}
else{
[selfshowTips:@"您的手機無法送短信"];
}
}