大家好,又见面了,我是你们的朋友全栈君。
//该方法在不退出应用程序的前提下调用短信和邮箱,以下内容请在真机测试
//导入框架MessageUI.framework
#import “ViewController.h”
//首先导入头文件
#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MFMessageComposeViewController.h>
//代理
@interface ViewController ()<MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>
@end
@implementation ViewController
//邮件按钮方法实现
– (void)mail:(id)sender {
//判断设备是否支持应用内发送邮件功能
if ([MFMailComposeViewController canSendMail]) {
//在应用内发送邮件
//创建邮件controller
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
//设置邮件代理
mailPicker.mailComposeDelegate = self;
//邮件主题
[mailPicker setSubject:@”Send WebView ScreenShot”];
//设置发送给谁,参数是NSarray,设置发送给两个邮箱
[mailPicker setToRecipients:[NSArray arrayWithObjects:@”aaaaa@163.com”, @”aaaaaa@qq.com”, nil]];
//可以添加抄送
[mailPicker setCcRecipients:[NSArray arrayWithObject:@”aaaaa@qq.com”]];
//可以添加暗抄送
[mailPicker setBccRecipients:[NSArray arrayWithObject:@”aaaaaa@qq.com”]];
//邮件正文
[mailPicker setMessageBody:@”WebShotScreen n in Attachment!” isHTML:NO];
//发送图片附件
//第一个图片名字是本地要选择发送的图片的名字, 第二个图片的名字是邮件里发送时显示的图片名字
NSString *pathImage = [[NSBundle mainBundle] pathForResource:@”a” ofType:@”jpg”];
NSData *dataImage = [NSData dataWithContentsOfFile:pathImage];
[mailPicker addAttachmentData:dataImage mimeType:@”image/jpg” fileName:@”1.jpg”];
//发送txt文本附件
NSString *pathText = [[NSBundle mainBundle] pathForResource:@”tv” ofType:@”txt”];
NSData *dataText = [NSData dataWithContentsOfFile:pathText];
[mailPicker addAttachmentData:dataText mimeType:@”text/txt” fileName:@”aa.txt”];
//发送doc文本附件
NSString *pathDoc = [[NSBundle mainBundle] pathForResource:@”MyText” ofType:@”doc”];
NSData *dataDoc = [NSData dataWithContentsOfFile:pathDoc];
[mailPicker addAttachmentData:dataDoc mimeType:@”text/doc” fileName:@”MyText.doc”];
//发送pdf文档附件
NSString *pathPdf = [[NSBundle mainBundle] pathForResource:@”CodeSigningGuide”ofType:@”pdf”];
NSData *dataPdf = [NSData dataWithContentsOfFile:pathPdf];
[mailPicker addAttachmentData:dataPdf mimeType:@”file/pdf”fileName:@”rainy.pdf”];
//把当前controller变为邮件controller
[self presentModalViewController:mailPicker animated:YES];
}else{
//如果该设备不支持在不退出程序的前提下调用邮件,则会推出应用程序并调用系统邮件,mailto://为固定写法后面加邮箱地址
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@”mailto://admin@hzlzh.com”]];
}
}
//实现 MFMailComposeViewControllerDelegate
//发送结果
– (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
NSString *mes = nil;
switch (result)
{
case MFMailComposeResultCancelled:
mes = @”取消编辑邮件“;
break;
case MFMailComposeResultSaved:
mes = @”成功保存邮件“;
break;
case MFMailComposeResultSent:
mes = @”点击发送,将邮件放到队列中,还没发送“;
break;
case MFMailComposeResultFailed:
mes = @”试图保存或者发送邮件失败“;
break;
default:
break;
}
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@”提醒“ message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:@”ok”, nil];
[alter show];
[self dismissModalViewControllerAnimated:YES];
}
//短信按钮方法实现
– (IBAction)message:(id)sender {
//判断设备是否支持应用内发送短信功能
if ([MFMessageComposeViewController canSendText]) {
//在应用内发送短信
{
//初始化
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
//代理
picker.messageComposeDelegate = self;
picker.navigationBar.tintColor = [UIColor blackColor];
//短信内容
picker.body = @”1111111111111111″;
//设置发送给谁
picker.recipients = [NSArray arrayWithObject:@”13300000000″];
//推到发送试图控制器
[self presentModalViewController:picker animated:YES];
}
}
else {
//如果该设备不支持在不退出程序的前提下调用短信,则会推出应用程序并调用系统短信,mailto://为固定写法后面加手机号码
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@”sms://800888″]];
}
}
//实现 MFMessageComposeViewControllerDelegate
– (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
NSString *mes = nil;
switch (result) {
case MessageComposeResultCancelled:
mes = @”取消编辑短信“;
break;
case MessageComposeResultSent:
mes = @”点击发送,将短信放到队列中,还没发送“;
break;
case MessageComposeResultFailed:
mes = @”发送短信失败“;
break;
default:
break;
}
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@”提醒“ message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:@”ok”, nil];
[alter show];
[self dismissModalViewControllerAnimated:YES];
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/182429.html原文链接:https://javaforall.cn