一、程序外調用系統發短信
這個方法其實很簡單,直接調用openURL即可:
NSURL *url = [NSURL URLWithString:@"sms://15888888888"]; [[UIApplication sharedApplication]openURL:url];
二、程序內調用系統發短信
這種方法有一個好處就是用戶發短信之後還可以回到App.
首先要導入MessageUI.framework,並引入頭文件:
#import <MessageUI/MessageUI.h>
然後要遵循代理MFMessageComposeViewControllerDelegate
,並實現代理方法。
#pragma mark - 代理方法 -(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; } }
發送短信方法實現
#pragma mark - 發送短信方法 -(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]; } }
最後,調用發送短信的方法
復制代碼 代碼如下:
[self showMessageView:[NSArray arrayWithObjects:@"15888888888",@"12399999999", nil] title:@"test" body:@"這是測試用短信,勿回復!"];
以上就是小編給大家介紹的iOS調用系統發短信的兩種方法,希望對大家有所幫助。