0

Apple の「Tabster」サンプル コードに基づいてアプリを作成しました。アプリは問題なく動作しますが、メールを追加したいと考えています。メールアプリを作りました。考えられる、チュートリアルから学ぶ、または読むことができるすべてのアプローチを試しましたが、継続的にクラッシュします。私はApple Devに質問を投げかけました。フォーラムといくつかの回答は、単純に「ファイルを既存のアプリにコピーしてください。そうすれば問題ないはずです」というものでした。明らかに、これは単純ではありません。MessageUI フレームワークを追加し、さまざまな方法でファイルをコピーしようとしましたが、まだ行き詰まっています。金曜日で、月曜日からこの 1 つの問題に悩まされていました。最初の部分は、2 つの main.m ファイルがあり、それらを結合しようとしたという事実だと思います。メールの main.m ファイルの名前を emailmain.m に変更しようとしました。他に何を試すべきかわかりません。

iOS アプリ内での電子メールの作成に関するすべてのドキュメントとそこにあるすべてのチュートリアルは、すべて新しいアプリケーションの作成から始まるというのは、私にとって驚くべきことです。私は何が欠けていますか?完全に機能するアプリにメールを追加するにはどうすればよいですか。このテーマに関するガイダンス、文献へのリンク、またはチュートリアルをいただければ幸いです。

これについて私が得ることができるどんな助けも非常に高く評価されます. 追加したいものは他にもいくつかありますが、電子メールを実装することさえできません!

あなたが提供できる助けをありがとう。ジョン

4

3 に答える 3

3

これは私がいつも使っている方法です。シンプルかつクリーンに任意の UIViewController に追加できる必要があります。

フレームワークをインポートします。

#import <MessageUI/MessageUI.h>

インターフェイスにデリゲートが含まれていることを確認してください。

@interface MyViewController : UIViewController <MFMailComposeViewControllerDelegate>

次に、実装で、IBAction などにする必要がある場合は、このメソッドまたはバリエーションを追加します。

- (void)sendEmail
{    
    if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *email = [[MFMailComposeViewController alloc] init];
        email.mailComposeDelegate = self;

        [email setSubject:@"My Email Subject"];

        [email setMessageBody:@"My message body." isHTML:NO];

        [self presentModalViewController:email animated:YES];

    } else {
        UIAlertView *emailAlert = [[UIAlertView alloc] initWithTitle:@"Email Failure" message:@"Your device is not configured to send email" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [emailAlert show];
    }
}

このメソッドは、ボタンのクリック時または必要に応じて呼び出すことができます。ユーザーが [送信] をクリックできるメール コンポーザ ビューが表示されます。

于 2012-05-11T20:03:38.543 に答える
0

これは、添付ファイルとして画像を含む電子メールを作成するサンプル コードです。必要に応じて変更できます。

-(void)createEmail 
    {
    NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:@"<html><body>"] retain];
       [emailBody appendString:@"<p>Some email body text can go here</p>"];
       UIImage *emailImage = [UIImage imageNamed:@"myImageName.png"];
       NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)];

       NSString *base64String = [imageData base64EncodedString];
      [emailBody appendString:[NSString stringWithFormat:@"<p><b><img src='data:image/png;base64,%@'></b></p>",base64String]];
      [emailBody appendString:@"</body></html>"];
        NSLog(@"%@",emailBody);

     //mail composer window
        MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
        emailDialog.mailComposeDelegate = self;
        [emailDialog setSubject:@"My Inline Image Document"];
        [emailDialog setMessageBody:emailBody isHTML:YES];

        [self presentModalViewController:emailDialog animated:YES];
        [emailDialog release];
        [emailBody release];
    }
于 2012-05-11T20:18:29.267 に答える
0

MessageUI フレームワークをインポートする必要があります。使用したい場所で、対応する .h ファイルにインポートし、MFMailComposeViewControllerDelegate を設定します。

#import <MessageUI/MessageUI.h>
@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate>

次に、メッセージを送信する場合は、.m ファイルで次のコードを使用します。

MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;

// Optional Configuration Parameters to make life easier for the user
[mailViewController setSubject:subjectString];
[mailViewController setMessageBody:messageString isHTML:YES];
[mailViewController setToRecipients:recipientsArray];

// Present the VIew Controller and clean up after ourselves
[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];

適切なデリゲート メソッドを追加します。これを使用して、メールが送信されたらコントローラーを閉じることができます。

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    [self dismissModalViewControllerAnimated:YES];
}
于 2012-05-11T20:07:10.567 に答える