2012-07-17 51 views
0

我使用以下IBAction將包含一些簡單收據數據的CSV文件附加到電子郵件中,但是,當電子郵件編輯器視圖打開時,灰色out文件出現在正文部分,當我實際發送郵件時,郵件根本沒有附件發送。將CSV文件附加到電子郵件中的問題目標 - C

繼承人我的代碼:

- (IBAction)actionEmailComposer { 
    NSLog(@"Receipts Count: %d", [receipts count]); 
    //Create CSV File 
    NSMutableString *csvString = [[NSMutableString alloc] init]; 
    [csvString appendString:@"Date,Business,Category,Paid By,Note, Number,Currency, Amount"]; 

    for (int i = 0; i < [receipts count]; i++){ 

     [csvString appendString: [[receipts objectAtIndex:i] receiptDate]]; 
     [csvString appendString: [[receipts objectAtIndex:i] business]]; 
     [csvString appendString: [[receipts objectAtIndex:i] category]]; 
     [csvString appendString: [[receipts objectAtIndex:i] paidBy]]; 
     [csvString appendString: [[receipts objectAtIndex:i] note]]; 
     [csvString appendString: [[receipts objectAtIndex:i] number]]; 
     [csvString appendString: [[receipts objectAtIndex:i] currency]]; 
     [csvString appendString: [[receipts objectAtIndex:i] amount]]; 

    } 

    [csvString writeToFile:@"test.csv" atomically:YES encoding:NSUTF8StringEncoding error:NULL]; 

    if ([MFMailComposeViewController canSendMail]) { 

     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
//  NSString *recipient = [defaults objectForKey:@"reportsEmail"]; 
     NSString *recipient = @"[email protected]"; 
     NSArray *recipients = [NSArray arrayWithObjects:recipient, nil]; 

     MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init]; 
     mailViewController.mailComposeDelegate = self; 
     [mailViewController setSubject:@"CSV Export"]; 
     [mailViewController setToRecipients:recipients]; 
     [mailViewController setMessageBody:@"" isHTML:NO]; 
     mailViewController.navigationBar.tintColor = [UIColor blackColor]; 
     NSData *myData = [NSData dataWithContentsOfFile:@"test.csv"]; 

     [mailViewController addAttachmentData:myData 
            mimeType:@"text/csv" 
            fileName:@"test"]; 

     [self presentModalViewController:mailViewController animated:YES]; 

    } 
    //Display alert to the user 
    else { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Can't Send Mail" 
                  message:@"Device is unable to send email in its current state." 
                  delegate:nil 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil]; 

     [alertView show]; 
    } 

} 

csvString輸出:

日期,業務,類別,支付的,請注意,編號,幣種,金額 「2012年7月17日」, 「壽」,」類別1「,」支票「,」午餐「,」726269「,」GBP「,」100.00「

因此,現有的數據,它似乎沒有正確附加,任何人都可以解釋這裏發生了什麼?

感謝,

傑克

+0

這是一個可怕的* *的方式來建立的'stringToWrite'。爲了我的安寧,請至少使用可變字符串並追加。 – 2012-07-17 14:47:17

+0

好的,我已經編輯了我的代碼,讓您放心,謝謝。 – 2012-07-17 15:39:31

+0

謝謝,我放心了;)我假設你留下了引號,爲了簡潔起見, – 2012-07-17 16:27:09

回答

5

試試這個代碼

  arr_email=[[NSMutableArray alloc] initWithArray:[objDatabase lookupAllForSQL:@"select * from password"]]; 


    NSString *[email protected]""; 
    NSString *str_type; 
    NSString *str_name; 
    NSString *str_user; 
    NSString *str_pwd; 
    NSString *str_url; 
    NSString *str_descr; 
    NSString *csvstr; 

    csvstr=[NSString stringWithFormat:@"Account Type,Account Name,UserName/Card No,Password/Pin No, URL/Other Info,Description"]; 

    for (int i=0; i<[arr_email count]; i++) { 
     NSMutableDictionary *dict = [arr_email objectAtIndex:i]; 

     str_type=[NSString stringWithFormat:@"%@",[dict objectForKey:@"AccountType"]]; 
     str_name=[NSString stringWithFormat:@"%@",[dict objectForKey:@"AccountName"]]; 
     str_user=[NSString stringWithFormat:@"%@",[dict objectForKey:@"UserName"]]; 
     str_pwd=[NSString stringWithFormat:@"%@",[dict objectForKey:@"Password"]]; 
     str_url=[NSString stringWithFormat:@"%@",[dict objectForKey:@"Url"]]; 
     str_descr=[NSString stringWithFormat:@"%@",[dict objectForKey:@"Description"]]; 

     mystr=[NSString stringWithFormat:@"%@ \n %@,%@,%@,%@,%@,%@",mystr,str_type,str_name,str_user,str_pwd,str_url,str_descr]; 

    } 
    csvstr=[NSString stringWithFormat:@"%@ \n %@",csvstr,mystr]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docDir = [paths objectAtIndex:0]; 

    NSString *filename = [docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"My Private Closet.csv"]]; 
    NSError *error = NULL; 
    BOOL written = [csvstr writeToFile:filename atomically:YES encoding:NSUTF8StringEncoding error:&error]; 
    if (!written) 
    NSLog(@"write failed, error=%@", error); 
+0

謝謝我親愛的朋友 – 2013-02-18 12:50:30

0

這是違反直覺的,但是當你附加的文件的文件名不包括擴展名。因此,所謂的 「test.csv」 文件名:

[mailViewController addAttachmentData:myData 
          mimeType:@"text/csv" 
          fileName:@"test"]; 
+0

嗨,謝謝你的回答,但它沒有改變任何東西..我已經編輯了我的代碼來合併更改。 – 2012-07-17 15:38:45

+0

你可以驗證文件是否寫入幷包含正確的數據?另外,確保'myData'包含一些東西。 (我用'dataWithContentsOfURL',它工作。) – Mundi 2012-07-17 16:59:22