ios - 在 UIActionSheet 中单击索引按钮时如何撰写邮件?

标签 ios iphone email uiactionsheet mfmailcomposeviewcontroller

当我在 UIActionSheet 中单击电子邮件选项时,我对如何显示邮件编辑器感到有点困惑。 这是我的示例代码:

-(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

这正是您要寻找的。我经常用这个。顺便说一句,UIActionSheets 在 iOS 8 中已被弃用。尽管如此:

.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];
}

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

关于ios - 在 UIActionSheet 中单击索引按钮时如何撰写邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27293356/

相关文章:

ios - 添加搜索栏以过滤 Collection View

ios - 为什么我需要在 Swift 的类中指定变量的类型?

iphone - scrollToRowAtIndexPath 不滚动到非事件/卸载的单元格

iphone - MKMapView setRegion :Animated: work on simulator but not on device iOS6

PHPMailer 在我使用 nginx 的 Digital Ocean 服务器上导致 504 超时错误

ios - CNContactViewController 与 CallKit 一起处理 CNContactPhoneNumbersKey

ios - 页面浏览量在 segue 后大小不正确

iphone - 如何让我的 UIPickerView 显示数组的所有元素

magento - 在系统配置magento中获取自定义电子邮件模板?

php - 如何检测域是否已捕获所有接受电子邮件的策略?