我如何显示邮件撰写当我点击电子邮件中的选项上有点混乱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];
}
还要注意的是有些人有与模拟器电子邮件的问题。 尝试在设备上,你放弃了。
下面的代码:
首先添加和导入信息框架:
#import <MessageUI/MessageUI.h>
然后标记你的自我作为这样的委托
@interface MYViewController () <MFMailComposeViewControllerDelegate>
然后拉起作曲:
- (IBAction)emailButtonPressed:(id)sender
{
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil];
[composeViewController setMailComposeDelegate:self];
[composeViewController setToRecipients:@[@"example@email.com"]];
[composeViewController setSubject:@"example subject"];
[self presentViewController:composeViewController animated:YES completion:nil];
}
}
然后办理委托回调并关闭作曲:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
//Add an alert in case of failure
[self dismissViewControllerAnimated:YES completion:nil];
}
在actionsheet使用actionSheet: clickedButtonAtIndex:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//Get the name of the current pressed button
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"Your button title"]) {
[self btnContact];
}
}
调用此方法
- (void)btnContact{
// Email Subject
NSString *emailTitle = @"";
// Email Content
NSString *messageBody = @"";
// To address
NSString *toRecp = @"";
NSArray *toRecipents = [NSArray arrayWithObject:toRecp];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}
这是appcoda的代码。 相反,指标的使用按钮的名称解雇你的邮件选项。