如何在UIActionSheet中的索引点击按钮时撰写邮件? [英] how to compose mail when button at index clicked in UIActionSheet?

查看:146
本文介绍了如何在UIActionSheet中的索引点击按钮时撰写邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击 UIActionSheet 中的电子邮件选项时,我对如何显示邮件作曲者有些困惑。
这是我的示例代码:

I am a bit confused on how to display mail composer when I click the email option in UIActionSheet. Here's my sample code:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(buttonIndex == 0)
    {
        NSString *emailTitle = @"Test Email";
        NSString *messageBody = @"iOS programming is so fun!";
        NSArray *toRecipents = [NSArray arrayWithObject:@"support@email.com"];
        MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
        mc.mailComposeDelegate = self;
        [mc setSubject:emailTitle];
        [mc setMessageBody:messageBody isHTML:NO];
        [mc setToRecipients:toRecipents];
        [self presentViewController:mc animated:YES completion:NULL];
    }
}


推荐答案

确保添加到.h文件并导入MessageUI.framework

Make sure you add to your .h file and import the MessageUI.framework

这是您正在寻找的。我经常使用这个。顺便说一下UIActionSheets在iOS 8中被弃用。你可以通过以下方式:

Here is exactly what your looking for. I use this often. By the way UIActionSheets are deprecated in iOS 8. Here you go though:

.h

ViewController : UIViewController <MFMailComposeViewControllerDelegate>

.m

- (IBAction)showActionSheet:(id)sender {

UIActionSheet *moreActions = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Add To Favorites", @"Search",@"Email", nil];
[moreActions showInView:self.view];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

switch (buttonIndex) {
    case 0: [self addToFav:self];
        break;
    case 1: ; [self popSearchBar:self];
        break;
    case 2: { [self sendEmail];
        break;
    }
        break;
}
}
- (void)sendEmail {
NSString *emailTitle = @"This is email title";
// Email Content as for HTML
NSString *messageBody = [NSString stringWithFormat:@"I may have found a missing document in your catalog. I tried opening :<p><strong><font color=\"red\"> %@ </font><br>'%@'</strong></p> with no results. Can I have a dollar for reporting this?", self.title, self.titleString];
// To address
NSArray *toRecipents = [NSArray arrayWithObject:@"youremail@google.com"];

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:YES];
[mc setToRecipients:toRecipents];

// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];

}
 - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult: (MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
    case MFMailComposeResultCancelled:
        NSLog(@"Mail cancelled");
        [self dismissViewControllerAnimated:YES completion:NULL];
        break;
    case MFMailComposeResultSaved:
        NSLog(@"Mail saved");
        [self dismissViewControllerAnimated:YES completion:NULL];
        break;
    case MFMailComposeResultSent:
        NSLog(@"Mail sent");
        [self dismissViewControllerAnimated:YES completion:NULL];
        break;
    case MFMailComposeResultFailed:
        NSLog(@"Mail sent failure: %@", [error localizedDescription]);
        break;
    default:
        break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}

另请注意,有些人遇到模拟器电子邮件问题。在放弃之前尝试使用设备。

Also note that some people are having issues with simulator emails. try it on a device before you give up.

这篇关于如何在UIActionSheet中的索引点击按钮时撰写邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆