【问题标题】:unable to load the mail client in iOS无法在 iOS 中加载邮件客户端
【发布时间】:2012-08-09 18:31:24
【问题描述】:

我注意到我多次使用邮件功能,所以我决定为重复多次的函数创建一个独立的类,而不是多次复制粘贴代码

类调用成功,邮件函数调用成功,但是调用函数后手机邮件客户端没有出现

这是我的代码

在主类中我执行以下调用

-(void)emailFunction{
...

CommonlyUsed Email = [[CommonlyUsed alloc] init];
[Email sendMail:receipient :CC :BCC :subject :body];

...
}

在我的 CommonlyUsed.h 中,我有以下 IDE:

#import <UIKit/UIKit.h>
#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MessageUI.h>


@interface CommonlyUsed : UIViewController <MFMailComposeViewControllerDelegate>{

在 CommonUsed.m 我有以下内容:

-(void)sendMail:(NSString*)receipient:(NSString*)cc:(NSString*)bcc:(NSString*)subject:(NSString*)body{

 MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
    [ composer setMailComposeDelegate:self];

 if ( [MFMailComposeViewController canSendMail]){
if (receipient) {
            [composer setToRecipients:[NSArray arrayWithObjects:receipient, nil]];
        }

        if (cc) {
            [composer setCcRecipients:[NSArray arrayWithObjects:receipient, nil]];
        }

        if (bcc) {
            [composer setBccRecipients:[NSArray arrayWithObjects:receipient, nil]];
        }

        if (subject) {
             [composer setSubject:subject];
        }

 [composer setMessageBody:body isHTML:HTMLBody];

        [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

        [self presentModalViewController:composer animated:YES];
        }
}

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

    }

代码编译并运行没有错误我错过了什么??

【问题讨论】:

  • 你检查过[MFMailComposeViewController canSendMail]的值吗?
  • 是的,它返回 YES 并进入代码内部

标签: objective-c xcode mfmailcomposeviewcontroller


【解决方案1】:

不要这样做:

[self presentModalViewController:composer animated:YES];

这样做:

[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentModalViewController:composer animated:YES];

或者这个:

[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentModalViewController:composer animated:YES];

您的电子邮件类当前不是活动视图控制器,因此它不能呈现模态视图控制器,您需要使用活动的,例如主 UIWindow 的 rootViewController。

编辑

如果您在 emailClient 被解除时使用 ARC,则您的对象(委托)将从内存中删除,因此解决方案是将 CommonlyUsed 类设为 Singleton:

+(CommonlyUsed *)sharedInstance {

    static CommonlyUsed * cu = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        cu = [[CommonlyUsed alloc] init];

    });

    return cu;
}

所以你会这样使用它:

CommonlyUsed * email = [CommonlyUsed sharedInstance];
[email sendMail:receipient :CC :BCC :subject :body];

【讨论】:

  • 它有效!现在应用程序启动了邮件客户端,但是当我发送电子邮件或取消电子邮件发送时没有调用“didFinishWithResult”,有什么办法解决这个问题吗?
  • 当用户关闭 mailClient 时,您确定电子邮件对象仍然存在吗?从您的代码中(如果您使用 arc),当方法 @ 时,电子邮件对象似乎已从内存中删除987654326@ 返回。一个简单的解决方法是让您的 CommonUsed 类成为单例。
  • 一切正常!!!!我唯一需要编辑的是 [self dismissModalViewControllerAnimated:YES];[[[[[UIApplication sharedApplication] delegate] window] rootViewController] dismissModalViewControllerAnimated:YES]; 非常感谢!
【解决方案2】:

您将presentModalViewController: 发送到您的实例Email,属于CommonlyUsed,但此实例不是视图层次结构中的视图控制器。

您必须将presentModalViewController: 发送到您当前活动的视图控制器。

【讨论】:

    猜你喜欢
    • 2010-09-25
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多