Search code examples
iphoneiosnsmutablearraynsdatamfmailcomposeviewcontroller

Data Attached in E-mail?


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.I try this code to attached viewArray data in E-mail.

   - (IBAction)sendEmail {
          if ([MFMailComposeViewController canSendMail])
      {
     NSArray *recipients = [NSArray arrayWithObject:@"[email protected]"];
    MFMailComposeViewController *controller = [[MFMailComposeViewController 
                                        alloc] init];
     controller.mailComposeDelegate = self;
     [controller setSubject:@"Iphone Game"];
     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];
 } 
 }

Here i see objects store in viewArray.in console its look like this..[2012-05-07 18:48:00.065 Note List[279:207] test=UILabel: 0x8250850; frame = (140 341; 56 19); text = 'Xxxxxxx'; clipsToBounds = YES; layer = > but in E-mail i see only this..>> please suggest any one how can i attached my viewArray data in E-mail. ]


Solution

  • in email attachement, you can only send NSData or string to email, now if you want to send it by string, then get all values you want to send email like, lable.text, lable.color, lable.alpha etc, with proper keys and place it in the body and parse there, else find some way to convert your object into NSData and attach it using mfmailcomposer attach data method

    read this to convert NSArray into NSData

    How to convert NSArray to NSData?

    and this to convert back the NSData to NSArray

    How can i convert a NSData to NSArray?

    and then write this data to file as,

    -(void)writeDataToFile:(NSString*)filename
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
    
        if(filename==nil)
        {
            DLog(@"FILE NAME IS NIL");
            return;
        }
        // the path to write file
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat: @"%@",filename]];
        /*NSData *writeData;
         writeData=[NSKeyedArchiver archivedDataWithRootObject:pArray]; */
        NSFileManager *fm=[NSFileManager defaultManager];
        if(!filePath)
        {
            //DLog(@"File %@ doesn't exist, so we create it", filePath);
            [fm createFileAtPath:filePath contents:self.mRespData attributes:nil];
        }
        else 
        {
            //DLog(@"file exists");
            [self.mRespData writeToFile:filePath atomically:YES];
        }
    
        NSMutableData *resData = [[NSMutableData alloc] init];
        self.mRespData=resData;
        [resData release];
    }
    

    and finally attach it to the email using

    - (void)addAttachmentData:(NSData *)attachment mimeType:(NSString *)mimeType fileName:(NSString *)filename