服务器之家

服务器之家 > 正文

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

时间:2020-12-23 15:23     来源/作者:TommyYaphetS

发送邮件
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(@"失败"); 
    } 
 
标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
返回顶部