objective c 中 MFMailComposeViewController 的电子邮件共享在 IOS 9.0 中无法正常工作。 ToRecipients 和发送选项不起作用

EMail sharing by MFMailComposeViewController in objective c not work properly for IOS 9.0. ToRecipients and send option is not worked

我正在尝试在 iOS 9 中打开电子邮件,但发送选项不起作用。我已经下载了一些项目,他们也有与 ios 9.

相同的问题

我已经在 app delegate .h 中声明了 globalMailComposer 并且此代码位于 app delegate .m 文件中的警报按钮中

if ([MFMailComposeViewController canSendMail])
{

    globalMailComposer = [[MFMailComposeViewController alloc] init];
    self.globalMailComposer.mailComposeDelegate = self;
    NSString *emailTitle = @"Test Email";
    NSString *messageBody = @"iOS programming is so fun!";
    NSArray *toRecipents:    [NSArrayarrayWithObjects:@"support@appcoda.com",nil];
    [globalMailComposer setSubject:emailTitle];
    [globalMailComposer setMessageBody:messageBody isHTML:NO];
    [globalMailComposer setToRecipients:toRecipents];
    [self.window.rootViewControllerpresentViewController:globalMailComposer animated:YES completion:nil];
}


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

也许它不能在模拟器上运行,但在任何设备上运行良好。 你在设备上检查过吗?

  1. 您不能从模拟器中发送邮件。始终在真实设备上进行测试。
  2. 在你的设备上试试这个代码,它对我来说工作得很好:

AppDelegate.h

#import <UIKit/UIKit.h>
@import MessageUI;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) MFMailComposeViewController *globalMailComposer;

@end

AppDelegate.m

#import "AppDelegate.h"

@interface AppDelegate () <MFMailComposeViewControllerDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if ([MFMailComposeViewController canSendMail])
    {
        self.globalMailComposer = [[MFMailComposeViewController alloc] init];
        self.globalMailComposer.mailComposeDelegate = self;
        NSString *emailTitle = @"Test Email";
        NSString *messageBody = @"iOS programming is so fun!";
        [self.globalMailComposer setSubject:emailTitle];
        [self.globalMailComposer setMessageBody:messageBody isHTML:NO];
        [self.globalMailComposer setToRecipients:@[@"support@appcoda.com"]];
        [self.window.rootViewController presentViewController:self.globalMailComposer animated:YES completion:nil];
    }

    return YES;
}

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

@end
  1. 如果仍然无法正常工作,请确保您已在设备上设置邮件帐户 correctly.It 应该会出现在邮件帐户下的设置中。