1、程序外发短信
NSString *phoneStr = @"10086";
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"sms://%@", phoneStr]];
[[UIApplication sharedApplication] openURL:url];
2、程序内发短信,发完短信自动返回应用
首先导入框架MessageUI.framework
引入头文件 #import <MessageUI/MessageUI.h>
实现代理方法 <MFMessageComposeViewControllerDelegate, UINavigationControllerDelegate>
3、直接上代码
+ (void)sendShortMessageWithPhoneNumber:(NSString *)phoneNumber text:(NSString *)text viewController:(BaseViewController *)viewController {
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
// 发送短信的号码,数组形式入参
controller.recipients = @[phoneNumber];
controller.navigationBar.tintColor = [UIColor redColor];
// 此处的body就是短信将要发生的内容
controller.body = text;
controller.messageComposeDelegate = viewController;
[viewController presentViewController:controller animated:YES completion:nil];
// 修改短信界面标题
[[[[controller viewControllers] lastObject] navigationItem] setTitle:@"title"];
} else {
[BaseViewController hudWithTitle:@"该设备不支持短信功能"];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
[controller dismissViewControllerAnimated:YES completion:nil];
switch (result) {
case MessageComposeResultSent:
[BaseViewController hudWithTitle:@"信息传送成功"];
break;
case MessageComposeResultFailed:
[BaseViewController hudWithTitle:@"信息传送失败"];
break;
case MessageComposeResultCancelled:
[BaseViewController hudWithTitle:@"信息被用户取消发送"];
break;
default:
break;
}
}
Demo地址:https://github.com/jayZhangh/PhotosFrameworkBasicUsage.git
网友评论