导入头文件
头文件:#import <MessageUI/MessageUI.h>
遵循代理:<MFMessageComposeViewControllerDelegate>
跳转代码实现
//是否可以发送文本消息(模拟器不可以)
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc]init];
vc.recipients = @[phoneNum];//短信接收者---是个数组
vc.body = @"";//短信内容
vc.messageComposeDelegate = self;
[self presentViewController:vc animated:YES completion:nil];
}else {
NSLog(@"设备不支持发送短信");
}
代理方法实现
#pragma mark- ====== MFMessageComposeViewControllerDelegate =======
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
//发送完信息就回到程序
[self dismissViewControllerAnimated:YES completion:nil];
switch (result) {
case MessageComposeResultSent:
[HUD showHUDMessage:@"发送成功"];
break;
case MessageComposeResultFailed:
[HUD showHUDMessage:@"发送失败"];
break;
case MessageComposeResultCancelled:
[HUD showHUDMessage:@"取消发送"];
break;
default:
break;
}
}
在 demo 中这样写,是可以,但是放到有些项目中短信界面的导航栏一片空白,没有取消按钮,无法返回自己的应用,出现这种情况看看自己的项目中是不是用了UINavigationController+FDFullscreenPopGesture这个第三方全屏滑动返回,如果用了的话在下面这个方法中添加一段代码:
- (void)fd_pushViewController:(UIViewController *)viewController animated:(BOOL)animated
就像这样子,到这里已经大功告成
- (void)fd_pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([self isKindOfClass:NSClassFromString(@"MFMessageComposeViewController")]) {
[self fd_pushViewController:viewController animated:animated];
return;
}
}