调用系统的短信和邮件功能,实现发送指定内容等
2016-10-20 21:17
836 查看
1.首先导入 #import<MessageUI/MessageUI.h>,有的人说还要导入MessageUI.framework,但是我这里面没导入也好用,还是导入吧,也不费什么事!
2.<MessageUI/MessageUI.h>中包含MFMailComposeViewController和MFMessageComposeViewController,也就是邮件和短信,实现两个代理,
MFMailComposeViewControllerDelegate,MFMessageComposeViewControllerDelegate
3.在.m文件中实现方法:
(1)邮件,只需要在你点击邮件按钮的方法中调用sendMail即可!!
- (void)sendMail
{
MFMailComposeViewController *mailPicker = [[MFMailComposeViewControlleralloc]init];
if (!mailPicker) {
//在设备还没有添加邮件账户的时候mailPicker为空,下面的present
view controller会导致程序崩溃,这里要作出判断
NSLog(@"设备还没有添加邮件账户");
return;
}
mailPicker.mailComposeDelegate =self;
(一)//设置主题
[mailPicker setSubject:@"这里是主题"];
(二)//设置内容
[mailPicker setMessageBody:@"这里是邮件的内容"];
//还有很多方法,比如
(三)//添加收件人
NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
[mailPicker setToRecipients: toRecipients];
(四)//添加抄送
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
[mailPicker setCcRecipients:ccRecipients];
(五)//添加密送
NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];
[mailPicker setBccRecipients:bccRecipients];
(六)// 添加一张图片
UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"];
NSData *imageData = UIImagePNGRepresentation(addPic);
//关于mimeType:http://www.iana.org/assignments/media-types/index.html
[mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];
(七)//添加一个pdf附件
NSString *file = [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];
NSData *pdf = [NSData dataWithContentsOfFile:file];
[mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];
(八)//添加一个视频
NSString *path=[NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),@"20121219.avi"];
NSData *video = [NSData dataWithContentsOfFile:path];
[mailPicker addAttachmentData:video mimeType: @"" fileName:@"20121219.avi"];
[selfpresentViewController:mailPickeranimated:YEScompletion:nil];
}
//实现代理方法
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error
{
//关闭邮件发送窗口
[controller dismissViewControllerAnimated:YEScompletion:nil];
NSString *msg;
if (result ==MFMailComposeResultCancelled) {
msg = @"用户取消编辑邮件";
}
elseif (result ==MFMailComposeResultSaved){
msg = @"用户成功保存邮件";
}
elseif (result ==MFMailComposeResultSent){
msg = @"发送成功";
}
else
{
msg = @"用户试图保存或者发送邮件失败";
}
[selfalertWithMessage:msg];
}
(2)短信,只需要在你点击短信按钮的方法中调用sendMessage即可!!
-(void)sendMessage
{
//显示发短信的控制器
MFMessageComposeViewController *messageViewController =[[MFMessageComposeViewControlleralloc]init];
if (!messageViewController) {
//在设备还没有添加邮件账户的时候messageViewController为空,下面的present
view controller会导致程序崩溃,这里要作出判断
return;
}
//设置短信内容
messageViewController.body = [InvateURLstringByAppendingString:self.invitationURL];
//设置收件人列表
messageViewController.recipients=@[@"13888888888",@"13666666666"];
//设置代理
messageViewController.messageComposeDelegate =self;
//显示控制器
[selfpresentViewController:messageViewControlleranimated:YEScompletion:^{
}];
}
#pragma mark 调取手机短信
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[controller dismissViewControllerAnimated:YEScompletion:nil];
//关闭短信界面
NSString *msg;
if (result ==MessageComposeResultCancelled) {
msg = @"取消发送";
}
elseif (result ==MessageComposeResultSent){
msg = @"发送成功";
}
else
{
msg = @"发送失败";
}
[selfalertWithMessage:msg];
}
//弹出alert方法
-(void)alertWithMessage:(NSString *)msg
{
UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"提示"message:msgpreferredStyle:
UIAlertControllerStyleAlert];
[alert addAction:[UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction
*_Nonnull action) {
SZQLog(@"确定");
}]];
[selfpresentViewController:alertanimated:YEScompletion:^{
}];
}
2.<MessageUI/MessageUI.h>中包含MFMailComposeViewController和MFMessageComposeViewController,也就是邮件和短信,实现两个代理,
MFMailComposeViewControllerDelegate,MFMessageComposeViewControllerDelegate
3.在.m文件中实现方法:
(1)邮件,只需要在你点击邮件按钮的方法中调用sendMail即可!!
- (void)sendMail
{
MFMailComposeViewController *mailPicker = [[MFMailComposeViewControlleralloc]init];
if (!mailPicker) {
//在设备还没有添加邮件账户的时候mailPicker为空,下面的present
view controller会导致程序崩溃,这里要作出判断
NSLog(@"设备还没有添加邮件账户");
return;
}
mailPicker.mailComposeDelegate =self;
(一)//设置主题
[mailPicker setSubject:@"这里是主题"];
(二)//设置内容
[mailPicker setMessageBody:@"这里是邮件的内容"];
//还有很多方法,比如
(三)//添加收件人
NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
[mailPicker setToRecipients: toRecipients];
(四)//添加抄送
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
[mailPicker setCcRecipients:ccRecipients];
(五)//添加密送
NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];
[mailPicker setBccRecipients:bccRecipients];
(六)// 添加一张图片
UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"];
NSData *imageData = UIImagePNGRepresentation(addPic);
//关于mimeType:http://www.iana.org/assignments/media-types/index.html
[mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];
(七)//添加一个pdf附件
NSString *file = [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];
NSData *pdf = [NSData dataWithContentsOfFile:file];
[mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];
(八)//添加一个视频
NSString *path=[NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),@"20121219.avi"];
NSData *video = [NSData dataWithContentsOfFile:path];
[mailPicker addAttachmentData:video mimeType: @"" fileName:@"20121219.avi"];
[selfpresentViewController:mailPickeranimated:YEScompletion:nil];
}
//实现代理方法
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error
{
//关闭邮件发送窗口
[controller dismissViewControllerAnimated:YEScompletion:nil];
NSString *msg;
if (result ==MFMailComposeResultCancelled) {
msg = @"用户取消编辑邮件";
}
elseif (result ==MFMailComposeResultSaved){
msg = @"用户成功保存邮件";
}
elseif (result ==MFMailComposeResultSent){
msg = @"发送成功";
}
else
{
msg = @"用户试图保存或者发送邮件失败";
}
[selfalertWithMessage:msg];
}
(2)短信,只需要在你点击短信按钮的方法中调用sendMessage即可!!
-(void)sendMessage
{
//显示发短信的控制器
MFMessageComposeViewController *messageViewController =[[MFMessageComposeViewControlleralloc]init];
if (!messageViewController) {
//在设备还没有添加邮件账户的时候messageViewController为空,下面的present
view controller会导致程序崩溃,这里要作出判断
return;
}
//设置短信内容
messageViewController.body = [InvateURLstringByAppendingString:self.invitationURL];
//设置收件人列表
messageViewController.recipients=@[@"13888888888",@"13666666666"];
//设置代理
messageViewController.messageComposeDelegate =self;
//显示控制器
[selfpresentViewController:messageViewControlleranimated:YEScompletion:^{
}];
}
#pragma mark 调取手机短信
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[controller dismissViewControllerAnimated:YEScompletion:nil];
//关闭短信界面
NSString *msg;
if (result ==MessageComposeResultCancelled) {
msg = @"取消发送";
}
elseif (result ==MessageComposeResultSent){
msg = @"发送成功";
}
else
{
msg = @"发送失败";
}
[selfalertWithMessage:msg];
}
//弹出alert方法
-(void)alertWithMessage:(NSString *)msg
{
UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"提示"message:msgpreferredStyle:
UIAlertControllerStyleAlert];
[alert addAction:[UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction
*_Nonnull action) {
SZQLog(@"确定");
}]];
[selfpresentViewController:alertanimated:YEScompletion:^{
}];
}
相关文章推荐
- 如何实现程序调用系统邮件,发送app中抓取的xml图片和内容到指定的邮箱
- iOS-调用系统的短信和发送邮件功能,实现短信分享邮件分享
- iOS-调用系统的短信和发送邮件功能,实现短信分享邮件分享
- <ios>调用系统的短信和发送邮件功能,实现短信分享邮件分享
- 小胖说事8------iOS 调用系统的短信和发送邮件功能,实现短信分享邮件分享
- 调用系统的短信和发送邮件功能,实现短信分享邮件分享
- iOS-调用系统的短信和发送邮件功能,实现短信分享邮件分享
- iOS-调用系统的短信和发送邮件功能,实现短信分享邮件分享
- iOS-调用系统的短信和发送邮件功能,实现短信分享邮件分享
- iOS-调用系统的短信和发送邮件功能,实现短信分享邮件分享
- iOS-调用系统的短信和发送邮件功能,实现短信分享邮件分享
- android中调用系统的发送短信、发送邮件、打电话功能
- 调用系统界面,给指定的号码发送短信,附带短信内容
- android调用系统的分享功能实现分享到短信,邮件和其他
- 调用系统发送短信、邮件等信息功能
- 调用webpowerAPI实现邮件发送功能(系统邮件一对一发送)
- 调用阿里云短信服务平台API实现ERP系统短信发送功能
- Java调用WebService接口实现发送手机短信验证码功能
- Android 调用系统邮件,发送邮件到指定邮箱
- 商派BBC系统——邮件短信发送调用说明