2011-03-04 40 views
0

當我按下sendMail按鈕時,它會轉到郵件按鈕,但當我點擊發送或取消時,它不會將我帶回我的應用程序。有什麼建議麼?iPhone In應用程序電子郵件問題

-(IBAction)sendMail { 

    MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease] ; 

    if ([MFMailComposeViewController canSendMail]) { 
     [mailComposer setToRecipients:nil]; 
     [mailComposer setSubject:nil]; 
     [mailComposer setMessageBody:@"Default text" isHTML:NO]; 

     [self presentModalViewController:mailComposer animated:YES]; 
    } 
} 

回答

1

下面添加郵件撰寫初始化

mailComposer.mailComposeDelegate = self;//very important if you want feedbacks on what the user did with your email sheet. 

以下行然後實現委託方法肯尼建議。您可以使用此方法執行自定義操作。

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ 
// Notifies users about errors associated with the interface 
switch (result) 
{ 
case MFMailComposeResultCancelled: 
    { 
     //Do something, If you need to 
    } 
break; 

default: 
break; 
} 
[self dismissModalViewControllerAnimated:YES]; 
} 

記住加入

@interface YourViewController : UIViewController <MFMailComposeViewControllerDelegate> { } 

如果您仍然有問題,以符合委託,您可以訪問下面的教程,一切很好地解釋說: http://blog.mugunthkumar.com/coding/iphone-tutorial-in-app-email/

4

您需要設置一個委託(通常與呈現MFMailComposeViewController的視圖控制器相同)。然後,當用戶點擊Save或Cancel按鈕時,MFMailComposeViewController將在代理上調用-mailComposeController:didFinishWithResult:錯誤。所以自己設置爲代理並定義以下方法:

#pragma mark - 
#pragma mark MessageUI Delegate Methods 

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

    [controller dismissModalViewControllerAnimated:YES]; 
} 
+0

我插入了這段代碼,但同樣的問題,你是什麼意思把你自己設爲代表。 – Vikings 2011-03-04 19:19:20

+0

當你初始化組合控制器時,你需要設置composeController.delegate = self; – 2011-03-04 19:32:46

1

這個奇妙的線:

mailComposer.mailComposeDelegate = self; 

是wha不知道是不是出了什麼問題,這讓它發生了好幾天。

而且不要忘記他們:

# import <UIKit/UIKit.h> 
# import <MessageUI/MessageUI.h> 
# import <MessageUI/MFMailComposeViewController.h> 

除了在項目導入MessageUI.framework

在IOS5驗證