在iPhone的MessageUI中使用MFMailComposeViewController从应用程序发送电子邮件
我是一名软件开发人员,我正在为电子邮件制作一个应用程序,我有以下代码:在iPhone的MessageUI中使用MFMailComposeViewController从应用程序发送电子邮件,iphone,objective-c,ipad,mfmailcomposeviewcontroller,messageui,Iphone,Objective C,Ipad,Mfmailcomposeviewcontroller,Messageui,我是一名软件开发人员,我正在为电子邮件制作一个应用程序,我有以下代码: // Header file // importing the MessageUI framework #import <MessageUI/MessageUI.h> // adding the delegate functionality to the class (<MFMailComposeViewControllerDelegate>) @interface Tutoria
// Header file
// importing the MessageUI framework
#import <MessageUI/MessageUI.h>
// adding the delegate functionality to the class (<MFMailComposeViewControllerDelegate>)
@interface TutorialProjectViewController : UIViewController <MFMailComposeViewControllerDelegate> {
}
- (IBAction)pressTheMailButtonDudeFunction:(id)sender
// Implementation file
- (IBAction)pressTheMailButtonDudeFunction:(id)sender {
// allocatind new message composer window
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
// setting a delegate method to "self"
mc.mailComposeDelegate = self;
// pre-populating the message subject
[mc setSubject:@"Send me a message"];
// adding content of the message as a plain text
[mc setMessageBody:@"Send me a message is you like this tutorial :)" isHTML:NO];
// adding content of the message as an HTML
[mc setMessageBody:@"<p>Send me a message is you like this tutorial :)<p>" isHTML:YES];
// adding recipients
[mc setToRecipients:[NSArray arrayWithObjects:@"Fuerte <info@fuerte.cz>", @"info@xprogress.com", nil]];
// adding recipients for a send copy to (arrayWithObject or arrayWithObjects)
[mc setCcRecipients:[NSArray arrayWithObject:@"test@example.com"]];
// adding hidden recipients
[mc setBccRecipients:[NSArray arrayWithObject:@"test@example.com"]];
// adding image attachment
// getting path for the image we have in the tutorial project
NSString *path = [[NSBundle mainBundle] pathForResource:@"Extra_Xcode_100x100" ofType:@"png"];
// loading content of the image into NSData
NSData *imageData = [NSData dataWithContentsOfFile:path];
// adding the attachment to he message
[mc addAttachmentData:imageData mimeType:@"image/png" fileName:@"Collection"];
// setting different than the default transition for the modal view controller
[mc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
/*
Modal view controllers transitions:
UIModalTransitionStyleCoverVertical => pops up from the bottom, default transition
UIModalTransitionStyleCrossDissolve => fade on the screen
UIModalTransitionStyleFlipHorizontal => page flip
*/
// displaying our modal view controller on the screen (of course animated has to be set on YES if you want to see any transition)
[self presentModalViewController:mc animated:YES];
// releasing the controller
[mc release];
}
// delegate function callback
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
// switchng the result
switch (result) {
case MFMailComposeResultCancelled:
NSLog(@"Mail send canceled.");
/*
Execute your code for canceled event here ...
*/
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved.");
/*
Execute your code for email saved event here ...
*/
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent.");
/*
Execute your code for email sent event here ...
*/
break;
case MFMailComposeResultFailed:
NSLog(@"Mail send error: %@.", [error localizedDescription]);
/*
Execute your code for email send failed event here ...
*/
break;
default:
break;
}
// hide the modal view controller
[self dismissModalViewControllerAnimated:YES];
}
//头文件
//导入MessageUI框架
#进口
//将委托功能添加到类()
@接口教程ProjectViewController:UIViewController{
}
-(iAction)按主按钮解除功能:(id)发送者
//实现文件
-(iAction)按MailButtonDudeFunction:(id)发送方{
//分配新消息生成器窗口
MFMailComposeViewController*mc=[[MFMailComposeViewController alloc]init];
//将委托方法设置为“self”
mc.mailComposeDelegate=self;
//预填充消息主题
[mc setSubject:@“给我发个消息”];
//以纯文本形式添加邮件内容
[mc setMessageBody:@“如果您喜欢本教程,请向我发送消息:)”isHTML:否];
//将邮件内容添加为HTML
[mc setMessageBody:@“如果您喜欢本教程,请向我发送消息:)“isHTML:是的];
//添加收件人
[mc setToRecipients:[NSArray arrayWithObjects:@“Fuerte”,@”info@xprogress.com“,无]];
//将发送副本的收件人添加到(arrayWithObject或arrayWithObjects)
[mc setCcRecipients:[NSArray arrayWithObject:@”test@example.com"]];
//添加隐藏收件人
[mc SetBCRecipients:[NSArray arrayWithObject:@”test@example.com"]];
//添加图像附件
//获取我们在教程项目中拥有的图像的路径
NSString*path=[[NSBundle mainBundle]pathForResource:@“png”类型的“Extra_Xcode_100x100”;
//将图像内容加载到NSData中
NSData*imageData=[NSData dataWithContentsOfFile:path];
//将附件添加到邮件中
[mc addAttachmentData:imageData mimeType:@“image/png”文件名:@“Collection”];
//设置不同于模态视图控制器的默认转换
[mc SetModAltTransitionStyle:UIModAltTransitionStyleCrossDissolve];
/*
模态视图控制器转换:
UIModalTransitionStyleCoverVertical=>从底部弹出,默认转换
UIModalTransitionStyleCrossBluse=>在屏幕上淡入淡出
UIModalTransitionStyleFlipHorizontal=>页面翻转
*/
//在屏幕上显示我们的模态视图控制器(当然,如果您想看到任何转换,必须将动画设置为“是”)
[自我呈现模式控制器:mc动画:是];
//释放控制器
[mc发布];
}
//委托函数回调
-(void)mailComposeController:(MFMailComposeViewController*)控制器未完成结果:(MFMailComposeResult)结果错误:(NSError*)错误{
//切换结果
开关(结果){
案例MFMailComposer结果已取消:
NSLog(@“邮件发送已取消”);
/*
在此处执行已取消事件的代码。。。
*/
打破
案例MFMailComposer结果已保存:
NSLog(@“邮件已保存”);
/*
在此处执行电子邮件保存事件的代码。。。
*/
打破
案例MFMailComposer结果已发送:
NSLog(@“已发送邮件”);
/*
在此处执行电子邮件发送事件的代码。。。
*/
打破
案例MFMailComposer结果失败:
NSLog(@“邮件发送错误:%@.”,[error localizedDescription]);
/*
在此处执行电子邮件发送失败事件的代码。。。
*/
打破
违约:
打破
}
//隐藏模态视图控制器
[自我解散Modalviewcontrolleranimated:是];
}
我没有得到正确的答案。。。这是正确的密码吗
- (IBAction)showMinimalModalMailView:(id)sender {
// get a new new MailComposeViewController object
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
// his class should be the delegate of the mc
mc.mailComposeDelegate = self;
// set a mail subject ... but you do not need to do this :)
[mc setSubject:@"This is an optional mail subject!"];
// set some basic plain text as the message body ... but you do not need to do this :)
[mc setMessageBody:@"This is an optional message body plain text!" isHTML:NO];
// set some recipients ... but you do not need to do this :)
[mc setToRecipients:[NSArray arrayWithObjects:@"first.address@test.com", @"second.address@test.com", nil]];
// displaying our modal view controller on the screen with standard transition
[self presentModalViewController:mc animated:YES];
// be a good memory manager and release mc, as you are responsible for it because your alloc/init
[mc release];
}
我也有同样的问题,每次在发送消息时运行应用程序,它都会崩溃;我发现如果我移除
[mc setToRecipients:[NSArray arrayWithObjects:@]首先。address@test.com“,@”秒。address@test.com“,无]]代码>
它工作正常,只要求我提供电子邮件地址。要从应用发送电子邮件,您的设备应配置电子邮件服务
// importing the MessageUI framework
#import <MessageUI/MessageUI.h>
// adding the delegate functionality to the class (<MFMailComposeViewControllerDelegate>)
@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate> {
}
- (IBAction)sendEMailClick:(id)sender {
//check mail service is configure to your device or not.
if ([MFMailComposeViewController canSendMail]) {
// get a new new MailComposeViewController object
MFMailComposeViewController * composeVC = [MFMailComposeViewController new];
// his class should be the delegate of the composeVC
[composeVC setDelegate:self];
// set a mail subject ... but you do not need to do this :)
[composeVC setSubject:@"This is an optional mail subject!"];
// set some basic plain text as the message body ... but you do not need to do this :)
[composeVC setMessageBody:@"This is an optional message body plain text!" isHTML:NO];
// set some recipients ... but you do not need to do this :)
[composeVC setToRecipients:[NSArray arrayWithObjects:@"first.address@test.com", @"second.address@test.com", nil]];
// Present the view controller modally.
[self presentViewController:composeVC animated:true completion:nil];
} else {
NSLog(@"Mail services are not available or configure to your device");
}
}
它显示了太多我无法理解的错误。它显示了“邮件发送已取消”,“邮件已保存”,“邮件已发送”。同时,请详细说明您的问题。@Nick Weaver:它显示了太多我无法理解的错误。它还显示了“邮件发送已取消”,“邮件已保存”,“邮件已发送”同时…我还将“导入MessageUI框架”导入到我的项目中,但我仍然无法得到答案。请详细说明您面临的确切问题。请尝试将数组结尾设置为[NSNull null],因为当需要对象时,nil不能作为结尾。这是弧前的事
#pragma mark - MFMailComposeViewControllerDelegate Methode.
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@",error.description);
break;
}
// Dismiss the mail compose view controller.
[controller dismissViewControllerAnimated:true completion:nil];
}