我正在尝试在我的应用程序中实现电子邮件的功能。我添加了MessageUI框架,以及标头和MFMailComposeViewControllerDelegate协议,但我遇到了问题。这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
if(isViewPushed == NO) {
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
target:self action:@selector(email)] autorelease];
}
}
-(void) email
{
NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:@"<html><body>"] retain];
[emailBody appendString:@"<p>type text here</p>"];
UIImage *emailImage = [UIImage imageNamed:@"20-gear2.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)];
[emailBody appendString:[NSString stringWithFormat:@"<p><b><img src='data:image/png;base64.....,.......%@'></b></p>",imageData]];
[emailBody appendString:@"</body></html>"];
MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
emailDialog.mailComposeDelegate = self;
[emailDialog setSubject:@"My Inline Image Document"];
[self presentModalViewController:emailDialog animated:YES];
[emailDialog release];
if(! [MFMailComposeViewController canSendMail])
{
UIAlertView *cantMailAlert = [[UIAlertView alloc]
initWithTitle:@"cant email"
message:@"nt able to send email"
delegate:NULL
cancelButtonTitle:@"ok"
otherButtonTitles:NULL];
[cantMailAlert show];
[cantMailAlert release];
return;
}
MFMailComposeViewController *mailController = [[[MFMailComposeViewController alloc] init] autorelease];
[mailController setMessageBody:@"can send my mail" isHTML:NO];
mailController.mailComposeDelegate = self;
[self presentModalViewController:mailController animated:YES];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
if (error)
{
UIAlertView *cantMailAlert = [[UIAlertView alloc]
initWithTitle:@"mail error"
message: [error localizedDescription]
delegate:NULL
cancelButtonTitle:@"ok"
otherButtonTitles:NULL];
[cantMailAlert show];
[cantMailAlert release];
return;
}
NSString *resultString;
switch (result)
{
case MFMailComposeResultSent:
resultString = @"sent mail";
break;
case MFMailComposeResultSaved:
resultString = @"saved";
break;
case MFMailComposeResultCancelled:
resultString = @"cancel";
break;
case MFMailComposeResultFailed:
resultString = @"failed";
break;
}
if (resultString = @"saved")
{
NSString *msg = [NSString stringWithFormat:@"%@ at %@\n", resultString, [NSDate date]];
UIAlertView *MailAlert = [[UIAlertView alloc]
initWithTitle:@"status"
message: msg
delegate:NULL
cancelButtonTitle:@"ok"
otherButtonTitles:NULL];
[MailAlert show];
[MailAlert release];
return;
}
[controller dismissModalViewControllerAnimated:YES];
[controller release];
//[self email];
}
但是当我点击邮件按钮时,应用程序终止并开始加载。它说无法存储私密价值!!
答案 0 :(得分:0)
为什么在MFMailcomposeviewcontroller
中展示viewDidLoad
的两个实例?为什么要创建两个对象,即emailDialog
和mailController
并展示它们?