2011-03-21 86 views
1

我想在我的应用程序中实现电子邮件的功能。我已经添加了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]; 
} 

但是当我点击邮件按钮然后applictaion终止并开始加载。它说不能存储私人价值!

回答

0

为什么要在viewDidLoad中显示MFMailcomposeviewcontroller的两个实例?你为什么要创建两个对象,即emailDialogmailController并呈现它们?

+0

使用setSubject-method设置标题和消息正文。 MFMailComposeViewController * emailDialog = [[MFMailComposeViewController alloc] init]; emailDialog.mailComposeDelegate = self; [emailDialog setSubject:@“My Inline Image Document”]; [emailDialog setMessageBody:emailBody isHTML:YES]; \t [self presentModalViewController:emailDialog animated:YES]; [emailDialog发布]; [emailBody release]; – Ketan 2011-03-21 10:31:36

+0

MFMailComposeViewController * mailController = [[[MFMailComposeViewController alloc] init] autorelease]; \t [mailController setMessageBody:@“can send my mail”isHTML:NO]; \t mailController.mailComposeDelegate = self; \t [self presentModalViewController:mailController animated:YES] – Ketan 2011-03-21 10:34:29

+0

这需要什么?你为什么要提交两个mailcomposer实例? – visakh7 2011-03-21 10:44:27