我在我的电子邮件应用程序中展示了MFMailComposeViewController。 但是视图没有显示,只显示上部SEND和CANCEL按钮。
这是点击“Email to Subscribe”按钮后显示的视图的屏幕截图。
我已查看所有相关代码。我添加了“MessageUI”框架并导入了
MessageUI/MessageUI.h , MessageUI/MFMailComposeViewController.h
我使用了以下代码:
- (void)viewWillAppear:(BOOL)animated {
[self.view setFrame:CGRectMake(0, 62, 320, 418)];
[APPDELEGATE.window addSubview:self.view];
[self.navigationController.navigationBar setHidden:NO];
self.navigationItem.title=@"Free Subscription";
[super viewWillAppear:animated]; }
-(void)viewWillDisappear:(BOOL)animated {
[self.view removeFromSuperview];
[super viewWillDisappear:animated]; }
-(IBAction)btnEmailPressed {
MFMailComposeViewController* Apicker = [[MFMailComposeViewController alloc] init];
if (Apicker != nil)
{
[Apicker setSubject:@"Free Subscription"];
[Apicker setMessageBody:@" " isHTML:NO];
NSArray *toRecipients = [NSArray arrayWithObjects:@"info@xyz.com", nil];
[Apicker setToRecipients:toRecipients];
Apicker.mailComposeDelegate = self;
if([MFMailComposeViewController canSendMail])
{
[self presentModalViewController:Apicker animated:YES];
}
else
{
}
[Apicker release];
} }
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError
*)error{
NSString *message;
switch (result) {
case MFMailComposeResultCancelled:
message =@"You have canceled your email.";
break;
case MFMailComposeResultFailed:
message=@"Your email is failed";
break;
case MFMailComposeResultSent:
message=@"Your email was successfully sent.";
break;
case MFMailComposeResultSaved:
message=@" Your email has been saved";
break;
default:
message=@" Your email is not send";
break;
}
UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"" message:message delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alt show];
[alt release];
[self dismissModalViewControllerAnimated:YES];
}
我无法理解这个问题。
感谢。
答案 0 :(得分:8)
适用于IOS 6
-(void)email{
MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:@"xyz@gmail.com", nil]];
[composer setSubject:@""];
// [composer.setSubject.placeholder = [NSLocalizedString(@"This is a placeholder",)];
[composer setMessageBody:@"" isHTML:NO];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:composer animated:YES completion:nil];
}
else {
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (error) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"error" message:[NSString stringWithFormat:@"error %@",[error description]] delegate:nil cancelButtonTitle:@"dismiss" otherButtonTitles:nil, nil];
[alert show];
[self dismissViewControllerAnimated:YES completion:nil];
}
else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
试试
适用于iOS 5
-(void)email:(id)sender{
MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:@"xyz@gmail.com", nil]];
[composer setSubject:@""];
// [composer.setSubject.placeholder = [NSLocalizedString(@"This is a placeholder",)];
[composer setMessageBody:@"" isHTML:NO];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:composer animated:YES];
}
else {
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (error) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"error" message:[NSString stringWithFormat:@"error %@",[error description]] delegate:nil cancelButtonTitle:@"dismiss" otherButtonTitles:nil, nil];
[alert show];
[self dismissModalViewControllerAnimated:YES];
}
else {
[self dismissModalViewControllerAnimated:YES];
}
}
答案 1 :(得分:2)
尝试修改你的代码: -
-(IBAction)btnEmailPressed
{
MFMailComposeViewController* Apicker = [[MFMailComposeViewController alloc] init];
if (Apicker != nil)
{
[Apicker setSubject:@"Free Subscription"];
[Apicker setMessageBody:@" " isHTML:NO];
NSArray *toRecipients = [NSArray arrayWithObjects:@"info@xyz.com", nil];
[Apicker setToRecipients:toRecipients];
Apicker.mailComposeDelegate = self;
if([MFMailComposeViewController canSendMail])
{
[self presentModalViewController:Apicker animated:YES];
}
else
{
NSString *recipients = @"mailto:info@xyz.com?cc=&subject=";
NSString *body = @"&body=";
NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
[Apicker release];
}
}
答案 2 :(得分:0)
在.h
档案MFMessageComposeViewControllerDelegate
和MFMailComposeViewControllerDelegate
<强>更新强>
if ([MFMailComposeViewController canSendMail]) {
appDelegate.imgCapture = [self captureView];
[appDelegate.imgCapture retain];
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
NSString *mailBody = @"Set Your Body Message";
[mailComposeViewController setMessageBody:mailBody isHTML:NO];
mailComposeViewController.mailComposeDelegate = self;
[self presentViewController:mailComposeViewController animated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"e-Mail Sending Alert"
message:@"You can't send a mail"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}