我正致力于通过电子邮件发送录制的数据。我正在研究的项目是基于Apple的示例项目SpeakHere。我的程序中的功能是,当我点击停止按钮时,手机应显示电子邮件视图。这部分是基于苹果的电子邮件示例代码编写的,如下所示:
-(void)displayComposerSheet{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Recorded Data!"];
// Set up the recipients.
NSArray *toRecipients = [NSArray arrayWithObjects:@"yyyy@gmail.com",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
但是,我不知道如何编辑电子邮件部分的代码以将视图控制器从self(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)
我相信你需要MailController的代表,即
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface SpeakHereViewController : UIViewController <MFMailComposeViewControllerDelegate>
确保添加MessageUI框架
我有时会发现苹果教程有点难以理解。尝试谷歌搜索xcode电子邮件教程。 Here就是一个。