当前位置: 移动技术网 > 移动技术>移动开发>IOS > 两种iOS调用系统发短信的方法

两种iOS调用系统发短信的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

一、程序外调用系统发短信

这个方法其实很简单,直接调用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调用系统发短信的两种方法,希望对大家有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网