从ViewController中的TableView发送电子邮件

时间:2013-12-14 21:39:05

标签: ios objective-c xcode tableview

我有一个TableView设置,当用户选择列表中的项目时触发不同的ViewController,但我希望“联系我们”触发消息框架(在应用程序电子邮件中),以便用户可以发送电子邮件而不是被推送到ViewController。

有什么建议吗?下面是我用于“联系我们”的代码块。

很抱歉,如果这是一个明显的答案,那就去学习,羡慕那里的所有巫师!

 NSMutableDictionary *sectionContactUs = [NSMutableDictionary dictionary];
    [sectionContactUs setObject:kSlideViewControllerSectionTitleNoTitle forKey:kSlideViewControllerSectionTitleKey];
    [sectionContactUs setObject:@"Contact Us" forKey:kSlideViewControllerSectionTitleKey];
    NSMutableDictionary *contactUsViewControllerDictionary = [NSMutableDictionary dictionary];
    [contactUsViewControllerDictionary setObject:@"Contact Us" forKey:kSlideViewControllerViewControllerTitleKey];
    [contactUsViewControllerDictionary setObject:@"ContactUsViewController" forKey:kSlideViewControllerViewControllerNibNameKey];
    [contactUsViewControllerDictionary setObject:[ContactUsViewController class] forKey:kSlideViewControllerViewControllerClassKey];
    [sectionTest setObject:[NSArray arrayWithObject:contactUsViewControllerDictionary] forKey:kSlideViewControllerSectionViewControllersKey];
    [datasource addObject:sectionContactUs];

2 个答案:

答案 0 :(得分:0)

你可以打开MFMailComposeViewController发送邮件..

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


    [picker setMessageBody:@"Body message" isHTML:NO];
    [self presentModalViewController:picker animated:YES];
    }
}


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissModalViewControllerAnimated:YES];
//////nslog (@"mail finished");
}

答案 1 :(得分:0)

使用MFMailComposeComposeView:  首先在项目中导入MessageUI框架 在视图控制器中编写#import <MessageUI/MessageUI.h>也可在界面

添加MFMailComposeViewControllerDelegate
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObjects:@"test1@gmail.com", @"test2@gmail.com", nil]; 
[controller setToRecipients:toRecipients];
[controller setTitle:@"Contact Us"];
[controller setSubject:@"Your Mail Subject"];
[controller setMessageBody:@"Mail body here \n Comments" isHTML:NO];

if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
    controller.modalPresentationStyle = UIModalPresentationFormSheet;
}
else
{
    controller.modalPresentationStyle = UIModalPresentationFullScreen;
}
[self presentModalViewController:controller animated:YES];

同时添加此方法

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    [self becomeFirstResponder];
    NSString *strMailResult;
    switch (result)
    {
        case MFMailComposeResultCancelled:
            strMailResult = NSLocalizedString(@"E-Mail Cancelled",@"");
            break;
        case MFMailComposeResultSaved:
            strMailResult = NSLocalizedString(@"E-Mail Saved",@"");
            break;
        case MFMailComposeResultSent:
            strMailResult = NSLocalizedString(@"E-Mail Sent",@"");
            break;
        case MFMailComposeResultFailed:
            strMailResult = NSLocalizedString(@"E-Mail Failed",@"");
            break;
        default:
            strMailResult = NSLocalizedString(@"E-Mail Not Sent",@"");
            break;
    }

    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Message",@"") message:strMailResult delegate:self  cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil];
    [alertView show];
    [self dismissModalViewControllerAnimated:YES];
}