2011-03-21 84 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