0

メール機能を数回使用していることに気付いたので、コードを数回コピーして貼り付けるのではなく、数回繰り返される関数用に独立したクラスを作成することにしました

クラスは正常に呼び出され、電子メール機能も正常に呼び出されますが、関数が呼び出された後、電話の電子メールクライアントは表示されません

ここに私のコードがあります

メインクラスでは、次の呼び出しを行います

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

    }

コードはコンパイルされ、エラーなしで実行されます。何が欠けていますか??

4

2 に答える 2

2

これを行う代わりに:

[self presentModalViewController:composer animated:YES];

これを行う:

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

またはこれ:

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

クラスEメールは現在アクティブなビューコントローラーではないため、モーダルビューコントローラーを表示できません。メインUIWindowのrootViewControllerなどのアクティブなビューコントローラーを使用する必要があります。

編集

emailClientが却下されるまでにARCを使用している場合、オブジェクト(デリゲート)はメモリから削除されているため、解決策はCommonlyUsedクラスをシングルトンにすることです。

+(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];
于 2012-08-09T19:11:32.807 に答える
2

classpresentModalViewController:のインスタンスに送信しますが、このインスタンスはビュー階層のビュー コントローラーではありません。EmailCommonlyUsed

presentModalViewController:現在アクティブなView Controllerに送信する必要があります。

于 2012-08-09T18:53:22.797 に答える