NSMutableArray Data Attachement With E-mail Body?
Asked Answered
L

1

0

My NSMutableArray data are in NSData formate.I am trying to attached NSMutableArray data to E-mail body.Here is my NSMutableArray code:

   NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
   NSString *msg1 = [defaults1 objectForKey:@"key5"];
   NSData *colorData = [defaults1 objectForKey:@"key6"];
   UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
   NSData *colorData1 = [defaults1 objectForKey:@"key7"];
   UIColor *color1 = [NSKeyedUnarchiver unarchiveObjectWithData:colorData1];
   NSData *colorData2 = [defaults1 objectForKey:@"key8"];
   UIFont *color2 = [NSKeyedUnarchiver unarchiveObjectWithData:colorData2];
   CGFloat x =(arc4random()%100)+100;
   CGFloat y =(arc4random()%100)+250;  
   lbl = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 100, 70)];
   lbl.userInteractionEnabled=YES;
   lbl.text=msg1;
   lbl.backgroundColor=color;
   lbl.textColor=color1;
   lbl.font =color2;
   lbl.lineBreakMode = UILineBreakModeWordWrap;
   lbl.numberOfLines = 50;
   [self.view addSubview:lbl];
   [viewArray addObject:lbl ];

viewArray is my NSMutableArray .All the data store in viewArray are in NSData formate .Then how Can attached this viewArray data With E-mail body.here is My E-mail Code.

 - (IBAction)sendEmail
{

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

   NSData *data = [NSKeyedArchiver archivedDataWithRootObject:viewArray];
   NSLog(@"testing: %@", data);
  [controller addAttachmentData:data mimeType:@"application/octet-stream";  
  fileName:nil]; 

  NSString *emailBody = @"Happy Valentine Day!";
  [controller setMessageBody:emailBody isHTML:NO
  [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];
}

 }

I get NO Error .viewArray show here the objects which is store in it and also when i convert viewArray to NSData it show bytes in console.but not Show any data in E-mail body..which is in viewArray.please Any One guide me how its possible to attached my viewArray data with Email.

Loch answered 7/5, 2012 at 8:37 Comment(0)
P
3

From the MFMailComposeViewController reference for addAttachmentData:mimeType:fileName::

filename

The preferred filename to associate with the data. This is the default name applied to the file when it is transferred to its destination. Any path separator (/) characters in the filename are converted to underscore (_) characters prior to transmission. This parameter must not be nil.

So it seems you have to specify a proper filename to be displayed in the mail body. Just any string will do.

EDIT:

I am afraid I cannot understand your comment... as I said, I have successfully sent an email with your code: what I get is a plist file, so everything is working as expected. This is the code I am using:

NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
NSString *msg1 = [defaults1 objectForKey:@"key5"];
UIColor *color = [UIColor grayColor];
UIColor *color1 = [UIColor grayColor];
UIFont *color2 = [UIFont systemFontOfSize:12];
CGFloat x =(arc4random()%100)+100;
CGFloat y =(arc4random()%100)+250;  
UILabel* lbl = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 100, 70)];
lbl.userInteractionEnabled=YES;
lbl.text=msg1;
lbl.backgroundColor=color;
lbl.textColor=color1;
lbl.font =color2;
lbl.lineBreakMode = UILineBreakModeWordWrap;
lbl.numberOfLines = 50;
[self.view addSubview:lbl];
NSMutableArray* viewArray = [NSMutableArray arrayWithCapacity:1];
[viewArray addObject:lbl ];


if ([MFMailComposeViewController canSendMail]) {
    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
    mailer.mailComposeDelegate = self;
    [mailer setSubject:@"Hello"];
    [mailer setToRecipients:[NSArray arrayWithObjects:@"mailAddress@mailAddress", nil]];
    NSString *emailBody = @"";
    [mailer setMessageBody:emailBody isHTML:NO];

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:viewArray];
    [mailer addAttachmentData:data mimeType:@"application/octet-stream" fileName:@"prova.plist"]; 

    [self presentModalViewController:mailer animated:YES];
    [mailer release];
}

The way I would go, in your case is:

  1. forget about the attachment for a moment, and try to send you a simple text email;

  2. if that works, add the 2 lines that send the attachment too:

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:viewArray];
    [mailer addAttachmentData:data mimeType:@"application/octet-stream" fileName:@"prova.plist"]; 
    

In both cases, set a breakpoint in your delegate method - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error and see which branch is executed:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
switch (result) {
        case MFMailComposeResultCancelled:
        case MFMailComposeResultSaved:
        case MFMailComposeResultSent:
        case MFMailComposeResultFailed:
        default:
        break;
    }
[self dismissModalViewControllerAnimated:YES];
}
Pump answered 7/5, 2012 at 19:28 Comment(2)
Thanx @sergio.but yet i not seen my array data in E-mail.Loch
Strange... I tried the code out with a proper filename, and apart from having to initialize color/color1/color2 myself (because I don't have user defaults set), it works and I can see the attachment file in the mail body (not array data, attachment file)... what are you expecting to see?Pump

© 2022 - 2024 — McMap. All rights reserved.