电子邮件中的数据附件?

时间:2012-05-07 10:52:14

标签: iphone objective-c nsmutablearray nsdata mfmailcomposeviewcontroller

我将我的对象保存在NSData formate中的NSMutablearray中。我没有尝试附加电子邮件Body.here是代码。

  - (IBAction)sendEmail
     {

   if ([MFMailComposeViewController canSendMail])
  {
       NSArray *recipients = [NSArray arrayWithObject:@"example@yahoo.com"];
       MFMailComposeViewController *controller = [[MFMailComposeViewController 
            alloc] init];
       controller.mailComposeDelegate = self;
       [controller setSubject:@"Iphone Game"];
       NSString *string = [viewArray componentsJoinedByString:@"\n"];
       NSString *emailBody = string; 
       NSLog(@"test=%@",emailBody);
       [controller setMessageBody:emailBody isHTML:YES];
       [controller setToRecipients:recipients];
       [self presentModalViewController:controller animated:YES];
       [controller release];
    }
else 
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" 
            message:@"Your device is not set up for email." delegate:self 
            cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];   
    [alert release];
}

 }

我没有收到任何错误,但没有看到E-mail.in NSLog中的任何数据我看到这个... 2012-05-07 15:33:22.984注释列表[273:207] test =>]请建议任何一个更好的解决方案,我如何在电子邮件正文中附加我的NSMutableArray数据..

3 个答案:

答案 0 :(得分:2)

我不清楚您的问题,请尝试这种方式设置您的数据。并在将它传递给composer之前检查你要设置的值,

看到这个

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

    NSString *mSubject;

    if(isInvite)
    {
        mSubject=@"TAPP Invitation";
    }
    else 
    {
        mSubject= @"TAPP email";
    }

    [picker setSubject:mSubject];

    NSString *mBody;

    if(isInvite)
    {
        NSString *pTappId=[[DataModel sharedDataModel] getValueForKey:USER_TAPP_ID];
        NSString *currentUserName=[[DataModel sharedDataModel] getValueForKey:CURRENT_USER_NAME];
         mBody=[NSString stringWithFormat:@"<HTML><BODY>Hi,<br><br>We already know one another, and I would like us to keep in touch.<br><br>Let's connect through TAPP (<a href=\"http://download.mytapp.com\">Download Here</a>) a smarter, private way to exchange and manage contact information.<br><br>Join TAPP and secure your preferred, unique ID before it is too late, and then connect with me. My TAPP ID is %@.<br><br>For more information, <a href=\"http://www.mytapp.com\">click here</a><br><br>Regards,<br><br>%@</BODY></HTML>",pTappId,currentUserName];
    }
    else 
    {
        mBody= [NSString stringWithFormat:@"<HTML><BODY><br /><br />Connected by <a href=http://www.mytapp.com>TAPP</a></BODY></HTML>"];
    }

    // Set up recipients
    NSArray *toRecipients = [NSArray arrayWithObject:selectedEmailId]; 
    //NSArray *toRecipients = [NSArray arrayWithObject:@""]; 

    [picker setToRecipients:toRecipients];

    // Attach an image to the email
    //NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];

    //NSData *myData = UIImagePNGRepresentation(photo.image);
    //[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"abc.png"];

    // Fill out the email body text
    NSString *emailBody = mBody;
    [picker setMessageBody:emailBody isHTML:YES];

    [self presentModalViewController:picker animated:YES];

    [picker release];

答案 1 :(得分:1)

如果您尝试将数据附加为标准电子邮件附件,请使用以下命令:

NSData *data = UIImageJPEGRepresentation(artworkImageView.image, 0.0);
[picker addAttachmentData:data mimeType:@"image/jpeg" fileName:@"Photo.jpeg"];

data可以是您想要的任何内容,只需提供适当的mime类型和文件名。

答案 2 :(得分:1)

也许你对viewArray的定义是错误的?

在你的.h文件中:

@property(nonatomic, retain) NSMutableArray *viewArray;

在您的.m文件中:

@synthesize viewArray;

请查看方法“componentsJoindedByString”的apple docs,因为我无法找到错误。

我使用api的viewArray初始化测试:(效果很好)

- (IBAction)sendEmail {

  self.viewArray = [NSArray arrayWithObjects:@"here", @"be", @"dragons", nil];

  if ([MFMailComposeViewController canSendMail])
  {
    NSArray *recipients = [NSArray arrayWithObject:@"example@yahoo.com"];
    MFMailComposeViewController *controller = [[MFMailComposeViewController 
                                            alloc] init];
    controller.mailComposeDelegate = self;
    [controller setSubject:@"Iphone Game"];

    //is anything in the array?
    NSLog(@"viewArray: %@", viewArray);

    NSString *string = [viewArray componentsJoinedByString:@"\n"];
    NSString *emailBody = string; 
    NSLog(@"test=%@",emailBody);
    [controller setMessageBody:emailBody isHTML:YES];
    [controller setToRecipients:recipients];
    [self presentModalViewController:controller animated:YES];
    [controller release];
  }
  else 
  { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" 
                                                message:@"Your device is not set up for     email." delegate:self 
                                      cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];   
    [alert release];
  } 
}