无法在iOS中加载邮件客户端

时间:2012-08-09 18:31:24

标签: objective-c xcode mfmailcomposeviewcontroller

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

成功调用类并成功调用电子邮件函数,但调用函数后电话的电子邮件客户端不会出现

这是我的代码

在主课程中我做以下调用

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

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

    }

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

而不是这样做:

[self presentModalViewController:composer animated:YES];

这样做:

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

或者这个:

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

您的类Email当前不是活动视图控制器,因此它无法呈现模态视图控制器,您需要使用活动视图控制器,如主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];