7

私はソフトウェア開発者で、電子メール メッセージ用のアプリケーションを作成しています。次のコードがあります。

// Header file  

// importing the MessageUI framework  
#import <MessageUI/MessageUI.h>  

// adding the delegate functionality to the class (<MFMailComposeViewControllerDelegate>)  
@interface TutorialProjectViewController : UIViewController <MFMailComposeViewControllerDelegate> {  

}  

- (IBAction)pressTheMailButtonDudeFunction:(id)sender

// Implementation file  

- (IBAction)pressTheMailButtonDudeFunction:(id)sender {  

    // allocatind new message composer window  
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];  

    // setting a delegate method to "self"  
    mc.mailComposeDelegate = self;  

    // pre-populating the message subject  
    [mc setSubject:@"Send me a message"];  

    // adding content of the message as a plain text  
    [mc setMessageBody:@"Send me a message is you like this tutorial :)" isHTML:NO];  

    // adding content of the message as an HTML  
    [mc setMessageBody:@"<p>Send me a message is you like this tutorial :)<p>" isHTML:YES];  

    // adding recipients  
    [mc setToRecipients:[NSArray arrayWithObjects:@"Fuerte <info@fuerte.cz>", @"info@xprogress.com", nil]];  

    // adding recipients for a send copy to (arrayWithObject or arrayWithObjects)  
    [mc setCcRecipients:[NSArray arrayWithObject:@"test@example.com"]];  

    // adding hidden recipients  
    [mc setBccRecipients:[NSArray arrayWithObject:@"test@example.com"]];  

    // adding image attachment  
    // getting path for the image we have in the tutorial project  
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Extra_Xcode_100x100" ofType:@"png"];  

    // loading content of the image into NSData  
    NSData *imageData = [NSData dataWithContentsOfFile:path];  

    // adding the attachment to he message  
    [mc addAttachmentData:imageData mimeType:@"image/png" fileName:@"Collection"];  

    // setting different than the default transition for the modal view controller  
    [mc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];  

    /* 
     Modal view controllers transitions: 

     UIModalTransitionStyleCoverVertical => pops up from the bottom, default transition 
     UIModalTransitionStyleCrossDissolve => fade on the screen 
     UIModalTransitionStyleFlipHorizontal => page flip 
     */  

    // displaying our modal view controller on the screen (of course animated has to be set on YES if you want to see any transition)  
    [self presentModalViewController:mc animated:YES];  

    // releasing the controller  
    [mc release];  
}  

// delegate function callback  
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {  
    // switchng the result  
    switch (result) {  
        case MFMailComposeResultCancelled:  
            NSLog(@"Mail send canceled.");  
            /* 
             Execute your code for canceled event here ... 
             */  
            break;  
        case MFMailComposeResultSaved:  
            NSLog(@"Mail saved.");  
            /* 
             Execute your code for email saved event here ... 
             */  
            break;  
        case MFMailComposeResultSent:  
            NSLog(@"Mail sent.");  
            /* 
             Execute your code for email sent event here ... 
             */  
            break;  
        case MFMailComposeResultFailed:  
            NSLog(@"Mail send error: %@.", [error localizedDescription]);  
            /* 
             Execute your code for email send failed event here ... 
             */  
            break;  
        default:  
            break;  
    }  
    // hide the modal view controller  
    [self dismissModalViewControllerAnimated:YES];  
}  

そして、私は適切な答えを得ていません.それは正しいコードですか?

4

3 に答える 3

5
  1. iOS プロジェクトに MessageUI フレームワークが含まれていることを確認してください。Xcode 4 内では、左側の列でプロジェクトを選択して、フレームワークを含めることができます。次に、「Build Phases」タブを選択します。ここで、「Link Binary With Libraries」の左側の矢印をクリックすると、アプリに既に含まれているフレームワークのリストが表示されます。MessageUI.framework が見つからない場合は、そこに追加してください。
  2. あなたが投稿したコードは、完全なチュートリアル コードを切り取ったように見えます...必要なコードのみを使用し、段階的に機能を追加してください。このようにして、バグのあるコード行をどこに追加したかがわかります。アプリバンドルに「Extra_Xcode_100x100.png」という画像がない可能性があります。

したがって、「最小限の」MFMailComposeViewController は次のとおりです。


- (IBAction)showMinimalModalMailView:(id)sender {
    // get a new new MailComposeViewController object 
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];  

    // his class should be the delegate of the mc  
    mc.mailComposeDelegate = self;  

    // set a mail subject ... but you do not need to do this :)
    [mc setSubject:@"This is an optional mail subject!"];  

    // set some basic plain text as the message body ... but you do not need to do this :)
    [mc setMessageBody:@"This is an optional message body plain text!" isHTML:NO];  

    // set some recipients ... but you do not need to do this :) 
    [mc setToRecipients:[NSArray arrayWithObjects:@"first.address@test.com", @"second.address@test.com", nil]];  

    // displaying our modal view controller on the screen with standard transition  
    [self presentModalViewController:mc animated:YES];  

    // be a good memory manager and release mc, as you are responsible for it because your alloc/init
    [mc release];  

}
于 2011-05-02T20:47:40.143 に答える
2

私は同じ問題を抱えていました。メッセージを送信するたびにアプリがクラッシュしました。私が削除した場合、私はそれを見つけました

[mc setToRecipients:[NSArray arrayWithObjects:@"first.address@test.com", @"second.address@test.com", nil]];

正常に動作し、メールアドレスを尋ねるだけです。

于 2012-01-04T13:54:46.200 に答える