gpt4 book ai didi

iphone - 在 iPhone 的 MessageUI 中使用 MFMailComposeViewController 从应用程序发送电子邮件

转载 作者:太空狗 更新时间:2023-10-30 03:37:25 26 4
gpt4 key购买 nike

我是一名软件开发人员,我正在为电子邮件消息制作一个应用程序,并且我有以下代码:

// 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];
}

我没有得到正确的答案...这是正确的代码吗?

最佳答案

  1. 确保您将 MessageUI 框架包含在您的 iOS 项目中。在 Xcode 4 中,您可以通过在左栏中选择您的项目来包含框架。然后选择选项卡“构建阶段”。在这里,您可以单击“Link Binary With Libraries”左侧的箭头,您会看到已包含在您的应用程序中的框架列表。如果缺少 MessageUI.framework - 只需将其添加到那里。
  2. 您发布的代码看起来像是一个完整的教程代码被剪断了……所以只使用您需要的代码……并逐步向其中添加更多功能。这样你就会看到你在哪里添加了错误的代码行。也许您的应用程序包中没有图像“Extra_Xcode_100x100.png”。

所以,这是一个“最小的”MFMailComposeViewController:


- (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];

}

关于iphone - 在 iPhone 的 MessageUI 中使用 MFMailComposeViewController 从应用程序发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5776854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com