欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS开发中实现邮件和短信发送的简单示例

程序员文章站 2022-03-23 13:12:19
发送邮件 1.导入库文件:messageui.framework 2.引入头文件 3.实现代理

发送邮件
1.导入库文件:messageui.framework
2.引入头文件
3.实现代理<mfmailcomposeviewcontrollerdelegate> 和 <uinavigationcontrollerdelegate>
代码示例:

复制代码 代码如下:

- (void)didclicksendemailbuttonaction{ 
 
    if ([mfmailcomposeviewcontroller cansendmail] == yes) { 
         
        mfmailcomposeviewcontroller *mailvc = [[mfmailcomposeviewcontroller alloc] init]; 
        //  设置代理(与以往代理不同,不是"delegate",千万不能忘记呀,代理有3步) 
        mailvc.mailcomposedelegate = self; 
        //  收件人 
        nsarray *sendtoperson = @[@"humingtao2014@gmail.com"]; 
        [mailvc settorecipients:sendtoperson]; 
        //  抄送 
        nsarray *copytoperson = @[@"humingtao2013@126.com"]; 
        [mailvc setccrecipients:copytoperson]; 
        //  密送 
        nsarray *secrettoperson = @[@"563821250@qq.com"]; 
        [mailvc setbccrecipients:secrettoperson]; 
        //  主题  
        [mailvc setsubject:@"hello world"]; 
        [self presentviewcontroller:mailvc animated:yes completion:nil]; 
        [mailvc setmessagebody:@"魑魅魍魉,哈哈呵呵嘿嘿霍霍" ishtml:no]; 
    }else{ 
     
        nslog(@"此设备不支持邮件发送"); 
     
    } 
 

 
- (void)mailcomposecontroller:(mfmailcomposeviewcontroller *)controller didfinishwithresult:(mfmailcomposeresult)result error:(nserror *)error{ 
 
    switch (result) { 
        case mfmailcomposeresultcancelled: 
            nslog(@"取消发送"); 
            break; 
        case mfmailcomposeresultfailed: 
            nslog(@"发送失败"); 
            break; 
        case mfmailcomposeresultsaved: 
            nslog(@"保存草稿文件"); 
            break; 
        case mfmailcomposeresultsent: 
            nslog(@"发送成功"); 
            break; 
        default: 
            break; 
    } 
     
    [self dismissviewcontrolleranimated:yes completion:nil]; 
}  
 
//  系统发送,模拟器不支持,要用真机测试 
- (void)didclicksendsystememailbuttonaction{ 
 
    nsurl *url = [nsurl urlwithstring:@"humingtao2014@gmail.com"]; 
    if ([[uiapplication sharedapplication] canopenurl:url] == yes) { 
         
        [[uiapplication sharedapplication] openurl:url];  
      
    }else{ 
     
        nslog(@"此设备不支持"); 
    } 
 

发送短信
前面三步引入配置和邮件发送一样  

复制代码 代码如下:

//  调用系统api发送短信 
- (void)didclicksendmessagebuttonaction{ 
     
    if ([mfmessagecomposeviewcontroller cansendtext] == yes) { 
         
        mfmessagecomposeviewcontroller *messagevc = [[mfmessagecomposeviewcontroller alloc] init]; 
        //  设置代理<mfmessagecomposeviewcontrollerdelegate> 
        messagevc.messagecomposedelegate = self; 
        //  发送to who 
        messagevc.recipients = @[@"18757289870"]; 
        messagevc.body = @"hello world"; 
        [self presentviewcontroller:messagevc animated:yes completion:nil]; 
         
    }else{ 
     
        nslog(@"此设备不支持"); 
    } 

 
- (void)messagecomposeviewcontroller:(mfmessagecomposeviewcontroller *)controller didfinishwithresult:(messagecomposeresult)result{ 
     
    switch (result) { 
        case messagecomposeresultcancelled: 
            nslog(@"取消发送"); 
            break; 
        case messagecomposeresultfailed: 
            nslog(@"发送失败"); 
            break; 
        case messagecomposeresultsent: 
            nslog(@"发送成功"); 
            break; 
        default: 
            break; 
    } 
     
    [self dismissviewcontrolleranimated:yes completion:nil]; 
 

 
//  调用系统应用程序发送消息 
- (void)didclicksendmessage2buttonaction{ 
     
    nsurl *url = [nsurl urlwithstring:@"sms:18656348970"]; 
    if ([[uiapplication sharedapplication] canopenurl:url] == yes) { 
         
        [[uiapplication sharedapplication] openurl:url]; 
         
    }else{ 
     
        nslog(@"失败"); 
    }