0

我NSMutableArray的數據在NSData的formate.I正在試圖連接的NSMutableArray數據到電子郵件body.Here是我的NSMutableArray代碼:電子郵件中附加的數據?

 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是我的NSMutableArray。所有在viewArray數據存儲在NSData的formate.I嘗試此代碼以附加在電子郵件viewArray數據。

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

在這裏,我看到的對象存儲在viewArray.in安慰其看起來像這樣.. [2012-05-07 18:48:00.065註釋列表[279:207]測試=的UILabel:0x8250850; frame =(140 341; 56 19); text ='Xxxxxxx'; clipsToBounds = YES;層=>但在電子郵件我只看到這個.. >>請建議任何一個我怎麼能附加我的viewArray數據在電子郵件。 ]

回答

1

在電子郵件附件中,您只能發送NSData或字符串到電子郵件,現在如果您想通過字符串發送,則可以獲取所有想要發送電子郵件的值,如lable.text,lable.color,lable阿爾法等,適當的鑰匙,並將其放置在身體和分析那裏,否則找到某種方式來你的對象轉換爲NSData的使用mfmailcomposer附加數據的方法

讀這的NSArray轉換爲NSData的

將其固定How to convert NSArray to NSData?

並將此NSData轉換回NSArray

How can i convert a NSData to NSArray?

,然後將該數據寫入文件作爲,

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

,並最終將其連接到使用

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

不,他們不是NSData的格式,如果他們的電子郵件,他們不會顯示爲[2012-05-07 18:48:00.065注意列表[279:207] test = UILabel:0x8250850; frame =(140 341; 56 19); text ='Xxxxxxx'; clipsToBounds = YES;層=>在烏拉圭回合的NSLog – Saad

+0

uilable顯示陣列類數據的 – Saad

+0

看到雁 – Saad