0

テキスト フィールドと画像のデータを含むメールを送信しようとしていますが、うまくいきません。アドバイスをお願いします。これが私のコードです:

     - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==1)
{

    Cocktails*c=[[Cocktails alloc]init];

    _arrTextField=[NSArray  arrayWithObjects:_txtName,_txtIngredients,_txtPreper,_txtServe,_txtFrom ,nil];

    NSLog(@"send email");

    if ([MFMailComposeViewController canSendMail])
    {
    NSMutableArray*recipients=[[NSMutableArray alloc]init];

    [recipients addObject:@"maya1580@gmail.com"];
    MFMailComposeViewController *controller= [[MFMailComposeViewController alloc]init];
    controller.mailComposeDelegate= self;
    [controller addAttachmentData:_imgDrink mimeType:@"image/png" fileName:@"Myimage"];
    [controller setSubject:@"my cocktail"];
    [controller setMessageBody: _arrTextField isHTML:NO];
    [controller setToRecipients:recipients];
    }
    else
    {
        UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Your devise is not set up for Email" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil];

        [alert show];
       //  [alert release];
    }
4

3 に答える 3

0

アップルはユーザーの知らないうちにこの種のことをすることを許可しないので、自動的に電子メールを送信することはできません。presentModalViewを提示する必要があります!

于 2012-10-27T21:37:55.467 に答える
0

ここで役立つはずです:

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved: you saved the email message in the drafts folder.");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
            break;
        default:
            NSLog(@"Mail not sent.");
            break;
    }

    // Remove the mail view
    [self dismissModalViewControllerAnimated:YES];
}


-(IBAction)sendCode:(id)sender {
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

        mailer.mailComposeDelegate = self;

        [mailer setSubject:@"Your subject"];

        NSArray *toRecipients = [NSArray arrayWithObjects:@"maya1580@gmail.com", nil];
        [mailer setToRecipients:toRecipients];

UIImage *myImage = [UIImage imageNamed:@"MyImage.png"];
            NSData *imageData = UIImagePNGRepresentation(myImage);
            [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"myimag

e"];


        NSString *emailBody =
        @"your body";


        [mailer setMessageBody:emailBody isHTML:NO];


        [mailer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [self presentModalViewController:mailer animated:YES];

    }
    else {
        {
            UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Error"
                                                             message:@"Your device doesnt support that action."
                                                            delegate:nil
                                                   cancelButtonTitle:@"Okay"
                                                   otherButtonTitles:nil];

            [alert2 show];



        }
    }
}

.h ファイルに次を追加します。

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

@interface Controller : UIViewController <MFMailComposeViewControllerDelegate> {


}

-(IBAction)send:(id)sender;

@end
于 2012-10-27T21:07:39.033 に答える
0

メールコントローラーを提示することはありません。他のモーダル ビュー コントローラーと同じように表示する必要があります。

于 2012-10-27T21:04:59.627 に答える