2012-11-29 77 views
3

我在我的電子郵件應用程序中呈現MFMailComposeViewController。 但是這個視圖並沒有顯示出來,只有上層的SEND和CANCEL按鈕才顯示出來。MFMailComposeViewController模型不顯示

這是點擊「電子郵件訂閱」按鈕後顯示的視圖的屏幕截圖。

enter image description here

我已簽出所有相關的代碼。我已經加入「MessageUI」框架和進口

MessageUI/MessageUI.h , MessageUI/MFMailComposeViewController.h 

我用下面的代碼:

- (void)viewWillAppear:(BOOL)animated { 

    [self.view setFrame:CGRectMake(0, 62, 320, 418)]; 

    [APPDELEGATE.window addSubview:self.view]; 

    [self.navigationController.navigationBar setHidden:NO]; 
    [email protected]"Free Subscription"; 
    [super viewWillAppear:animated]; } 


-(void)viewWillDisappear:(BOOL)animated { 
    [self.view removeFromSuperview]; 
    [super viewWillDisappear:animated]; } 



    -(IBAction)btnEmailPressed { 
     MFMailComposeViewController* Apicker = [[MFMailComposeViewController alloc] init]; 
     if (Apicker != nil) 
     { 

     [Apicker setSubject:@"Free Subscription"]; 

     [Apicker setMessageBody:@" " isHTML:NO]; 

     NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]", nil]; 
     [Apicker setToRecipients:toRecipients]; 

     Apicker.mailComposeDelegate = self; 

     if([MFMailComposeViewController canSendMail]) 
     { 
      [self presentModalViewController:Apicker animated:YES]; 
     } 
     else 
     { 

     } 
     [Apicker release]; 
    } } 


    -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError 
    *)error{ 

    NSString *message; 

    switch (result) { 
     case MFMailComposeResultCancelled: 
      message [email protected]"You have canceled your email."; 
      break; 
     case MFMailComposeResultFailed: 
      [email protected]"Your email is failed"; 
      break; 
     case MFMailComposeResultSent: 
      [email protected]"Your email was successfully sent."; 
      break; 
     case MFMailComposeResultSaved: 
      [email protected]" Your email has been saved"; 
      break; 

     default: 
      [email protected]" Your email is not send"; 
      break; 
    } 
    UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"" message:message delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 

    [alt show]; 
    [alt release]; 

    [self dismissModalViewControllerAnimated:YES]; 
     } 

我無法得到這個問題的原因。

謝謝。

+0

你的MailComposeViewController添加委託? –

+0

@PuneethSB - 亞我已添加它 –

+0

我將粘貼正在爲我工​​作的代碼 –

回答

8

及其在IOS 6

-(void)email{ 
    MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init]; 
    [composer setMailComposeDelegate:self]; 
    if ([MFMailComposeViewController canSendMail]) { 
     [composer setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]]; 
     [composer setSubject:@""]; 

     // [composer.setSubject.placeholder = [NSLocalizedString(@"This is a placeholder",)]; 

     [composer setMessageBody:@"" isHTML:NO]; 
     [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; 
     [self presentViewController:composer animated:YES completion:nil]; 
    } 
    else { 
    } 
} 
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ 
    if (error) { 
     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"error" message:[NSString stringWithFormat:@"error %@",[error description]] delegate:nil cancelButtonTitle:@"dismiss" otherButtonTitles:nil, nil]; 
     [alert show]; 
     [self dismissViewControllerAnimated:YES completion:nil]; 
    } 
    else { 
     [self dismissViewControllerAnimated:YES completion:nil]; 
    } 
} 

添加這兩個代表剛剛嘗試

對於iOS 5

-(void)email:(id)sender{ 
    MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init]; 
    [composer setMailComposeDelegate:self]; 
    if ([MFMailComposeViewController canSendMail]) { 
     [composer setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]]; 
     [composer setSubject:@""]; 

     // [composer.setSubject.placeholder = [NSLocalizedString(@"This is a placeholder",)]; 

     [composer setMessageBody:@"" isHTML:NO]; 
     [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; 
     [self presentModalViewController:composer animated:YES]; 
    } 
    else { 
    } 
} 
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ 
    if (error) { 
     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"error" message:[NSString stringWithFormat:@"error %@",[error description]] delegate:nil cancelButtonTitle:@"dismiss" otherButtonTitles:nil, nil]; 
     [alert show]; 
     [self dismissModalViewControllerAnimated:YES]; 
    } 
    else { 
     [self dismissModalViewControllerAnimated:YES]; 
    } 
} 
+0

感謝您的回覆。但是,如果對於ios 5呢? –

+0

@rohanpanchal for iOS 5我更新了上面的代碼:) –

+0

再次感謝您的回覆。你能說明一下ios 6和ios 5代碼有什麼不同嗎? –

0

.h文件MFMessageComposeViewControllerDelegateMFMailComposeViewControllerDelegate

UPDATE

 if ([MFMailComposeViewController canSendMail]) { 
      appDelegate.imgCapture = [self captureView]; 
      [appDelegate.imgCapture retain]; 
      MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init]; 
      NSString *mailBody = @"Set Your Body Message"; 


      [mailComposeViewController setMessageBody:mailBody isHTML:NO]; 
      mailComposeViewController.mailComposeDelegate = self; 
      [self presentViewController:mailComposeViewController animated:YES completion:nil]; 
     } else { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"e-Mail Sending Alert" 
                  message:@"You can't send a mail" 
                  delegate:nil 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 
     } 
+0

感謝您的回覆。我已經添加了這兩個文件。 –

+0

@rohanpanchal試試這個代碼花花公子.. –

2

嘗試修改你的代碼是這樣的: -

-(IBAction)btnEmailPressed 
{ 
    MFMailComposeViewController* Apicker = [[MFMailComposeViewController alloc] init]; 
    if (Apicker != nil) 
    { 
     [Apicker setSubject:@"Free Subscription"]; 
     [Apicker setMessageBody:@" " isHTML:NO]; 
     NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]", nil]; 
     [Apicker setToRecipients:toRecipients]; 
     Apicker.mailComposeDelegate = self; 

     if([MFMailComposeViewController canSendMail]) 
     { 
      [self presentModalViewController:Apicker animated:YES]; 
     } 
     else 
     { 
      NSString *recipients = @"mailto:[email protected]?cc=&subject="; 
      NSString *body = @"&body="; 

       NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body]; 
      email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]]; 
     } 
     [Apicker release]; 
    } 
} 
+0

很好的解決方案,當canSendMail返回NO時處理。 –