Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

iphone - NSMutableArray Data Attachement With E-mail Body?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...