2014-01-30 38 views
0

我正在通過電子郵件發送記錄的數據。我正在開發的這個項目是基於Apple的示例項目SpeakHere。我的程序中的功能是,當我點擊停止按鈕時,手機應顯示電子郵件視圖。這部分是基於蘋果的電子郵件示例代碼編寫象下面這樣:未能在SpeakHere項目中加載電子郵件界面

-(void)displayComposerSheet{ 
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
picker.mailComposeDelegate = self; 

[picker setSubject:@"Recorded Data!"]; 
// Set up the recipients. 
NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]",nil]; 

[picker setToRecipients:toRecipients]; 

NSString *path = recorder -> getFilePath(); 
NSLog(@"%@",path); 

NSData *myData = [NSData dataWithContentsOfFile:path]; 
[picker addAttachmentData:myData mimeType:@"text/plain" fileName:@"MyFile"]; 

// Fill out the email body text. 
NSString *emailBody = @"The attachement is the recorded integer data!"; 
[picker setMessageBody:emailBody isHTML:NO]; 

// Present the mail composition interface. 
// [self presentModalViewController:picker animated:YES]; 
[self presentViewController:picker animated:YES completion:NULL]; 

[picker release]; // Can safely release the controller now. 
} 

郵件撰寫視圖控制器的委託方法是這樣的:

- (void)mailComposeController:(MFMailComposeViewController *)controller 
     didFinishWithResult:(MFMailComposeResult)result 
        error:(NSError *)error 
{ 
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Mail Alert" message:nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil] ; 


switch (result) 
{ 
    case MFMailComposeResultCancelled: 
     alert.message = @"Email Cancelled"; 
     break; 
    case MFMailComposeResultSaved: 
     alert.message = @"Email Saved"; 
     break; 
    case MFMailComposeResultSent: 
     alert.message = @"Email Sent"; 
     break; 
    case MFMailComposeResultFailed: 
     alert.message = @"Email Failed"; 
     break; 
    default: 
     alert.message = @"Email Not Sent"; 
     break; 
} 


// [self dismissModalViewControllerAnimated:YES]; 
[self dismissViewControllerAnimated:YES completion:NULL]; 
[alert show]; 
}` 

但是,它墜毀時,我按下停止按鈕。我覺得原因是這段代碼是用SpeakHereController.mm編寫的。但是,真正的視圖控制器是SpeakHereViewController。在SpeakHereViewController.h的代碼是

@class SpeakHereController; 
@interface SpeakHereViewController : UIViewController { 

IBOutlet SpeakHereController *controller; 
} 
@end 

不過,我不知道如何編輯電子郵件部分的代碼來設置從自我(SpeakHereController)到SpeakHereViewController視圖控制器。誰能幫我 ?非常感謝!

追問:

我的iPhone可以顯示郵件視圖如果我改變了代碼如下:

self.window.rootViewController = viewController; 
在委託文件

,然後,改變

[self presentViewController:picker animated:YES completion:nil]; 

到:

[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:picker animated:YES completion:NULL]; 

然而,新的問題是,如果我取消郵件,郵件視圖不能被解僱。似乎發生,因爲下面的代碼的問題:

picker.mailComposeDelegate = self; 

的代表應該是根視圖控制器的代表,但我不知道如何設置委託的根視圖控制器的委託。誰能幫忙?

+0

把崩潰日誌。它會讓別人知道它在哪裏絆倒 –

+0

嗨,山姆,謝謝你的回覆,我已經添加了崩潰日誌。 – Magic

+0

SpeekHereViewController似乎有一個NavigationBar。你應該做'[[self navigationController] presentViewController:picker animated:YES completion:nil];'而不是。 – Larme

回答

0

我相信你需要爲你的MailController即代表

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

@interface SpeakHereViewController : UIViewController <MFMailComposeViewControllerDelegate> 

確保添加MessageUI框架

我有時會發現蘋果教程有點難以跟隨。試用谷歌搜索xcode電子郵件教程。 Here是其中之一。

+0

我已經將這些代碼添加到SpeakHereViewController。但它給了我同樣的問題。我想我需要一種方法來設置當前視圖控制器的功能[self presentViewController:picker animated:YES completion:NULL];而不是將其設置爲自我。 – Magic

+0

我只是試過,相同的結果..T_T .. – Magic

+0

愚蠢的問題 - 一旦記錄停止,你是否會提示電子郵件窗口?我真的不能看到這個代碼有什麼問題 –

相關問題