首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS发送短信的代码

iOS发送短信的代码

原创
作者头像
好派笔记
修改2021-10-29 11:07:23
4.2K0
修改2021-10-29 11:07:23
举报
文章被收录于专栏:好派笔记好派笔记
#pragma mark -发送短信

    @interface ViewController ()

    {

        UIAlertView *mfAlertview;//定义一个弹出框

    }

-(void)showMessageViewController  
{  
    if( [MFMessageComposeViewController canSendText] )//判断是否能发短息  
{  

        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init];  
        controller.recipients = [NSArray arrayWithObject:@"10010"];//接收人,可以有很多,放入数组  
        controller.body = self.txYaoqingma.text;//短信内容,自定义即可  
        controller.messageComposeDelegate = self;//注意不是delegate  

        [self presentViewController:controller animated:YES completion:nil];  

        [[[[controller viewControllers] lastObject] navigationItem] setTitle:@"发送短信"];//修改短信界面标题  
    }  
    else  
    {  

        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"抱歉" message:@"短信功能不可用!" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil nil];  
        [alert show];  
    }  
}  

//短信发送成功后的回调  
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result  
{  
    [controller dismissViewControllerAnimated:YES completion:nil];  

    switch (result)  
    {  
        case MessageComposeResultCancelled:  
        {  
            //用户取消发送  
        }  
            break;  
            case MessageComposeResultFailed://发送短信失败  
        {  
            mfAlertview=[[UIAlertView alloc]initWithTitle:@"抱歉" message:@"短信发送失败" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil, nil nil];  

            [mfAlertview show];  

        }  
            break;  
        case MessageComposeResultSent:  
        {  
            mfAlertview=[[UIAlertView alloc]initWithTitle:@"恭喜" message:@"短信发送成功!" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil, nil nil];  

            [mfAlertview show];  

        }  
            break;  
        default:  
            break;  
    }  
}  </pre> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
短信
腾讯云短信(Short Message Service,SMS)可为广大企业级用户提供稳定可靠,安全合规的短信触达服务。用户可快速接入,调用 API / SDK 或者通过控制台即可发送,支持发送验证码、通知类短信和营销短信。国内验证短信秒级触达,99%到达率;国际/港澳台短信覆盖全球200+国家/地区,全球多服务站点,稳定可靠。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档