开发者

Attachment doesn't displayed in mail which is sent through iPhone

开发者 https://www.devze.com 2022-12-13 10:55 出处:网络
I have send an attachment through iPhone application. But when I see the mail, I can see the upin that shows that something is attached.

I have send an attachment through iPhone application.

But when I see the mail, I can see the upin that shows that something is attached.

But when I open the mail I couldn't found any attachment?

What is the problem behind this?

-(IBAction)btnPressedExport:(id)sender{

    NSArray *x=[[NSArray alloc] initWithArray:[DatabaseAccess getAllTransactionsForUserID:[BudgetTrackerAppDelegate getUserID] profileID:[BudgetTrackerAppDelegate getProfileID]]];
    int i=-1,j=[x count];
    NSDictionary *tmp;
    NSMutableString *stringToWrite=[[NSMutableString alloc] init];
    for(;i<j;i++){
        if(i==-1){
            [stringToWrite appendString:@"TransactionID,TransactionDate,ProfileName,ProfileType,GroupName,GroupType,CategoryName,TransactionAmt\n"];
        } else {
            tmp=[x objectAtIndex:i];
            [stringToWrite appendFormat:@"%@,%@,%@,%@,%@,%@,%@,%@\n",
             [tmp valueForKey:@"TraID"],[tmp valueForKey:@"TraDate"],[tmp valueForKey:@"ProfileName"],[tmp valueForKey:@"ProfileType"],[tmp valueForKey:@"GroupName"],[tmp valueForKey:@"GroupType"],[tmp valueForKey:@"CatName"],[tmp valueForKey:@"TraAmt"]];
        }
    }
    [stringToWrite writeToFile:[self pathOfCSVForExport] atomically:YES encoding:NSStringEncodingConversionAllowLossy error:nil];
//  [stringToWrite writeToFile:[self pathOfCSVForExport] atomically:YES];


    picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate=self;
//  picker.delegate=self;

    [picker setSubject:@"开发者_StackOverflow中文版Hello from Sugar!"];


     //Set up recipients
    NSArray *toRecipients = [NSArray arrayWithObject:@"sugar.srk@pqr.com"]; 
//  NSArray *ccRecipients = [NSArray arrayWithObjects:@"xyz.dalal@pqr.com", @"kandarp.dave@phptalent.com", nil]; 
//  NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"]; 

    [picker setToRecipients:toRecipients];
    [picker setCcRecipients:ccRecipients];  
//  [picker setBccRecipients:bccRecipients];

    // Attach an image to the email
    NSString *path = [[NSBundle mainBundle] pathForResource:@"myExport" ofType:@"csv"];
    NSData *myData = [NSData dataWithContentsOfFile:path];

    NSString *fileNameToSend=@"BudgetTracker.csv";//[NSString stringWithFormat:@"%@.csv",[x valueForKey:@"ProfileName"]];

    [picker addAttachmentData:myData mimeType:@"text/plain" fileName:fileNameToSend];
    // text/html/
    // Fill out the email body text
    NSString *emailBody = [NSString stringWithFormat:@"%@",@"Hello! This is export test."];
    [picker setMessageBody:emailBody isHTML:NO];

    [self presentModalViewController:picker animated:YES];

}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
//  NSLog(@"here2");
    switch (result)
    {
        case MFMailComposeResultCancelled: break;
        case MFMailComposeResultSaved: break;
        case MFMailComposeResultSent: break;
        case MFMailComposeResultFailed: break;
        default: break;
    }
//  self.navigationController.navigationBarHidden=NO;
//  [self becomeFirstResponder];
    [self dismissModalViewControllerAnimated:YES];
}


This looks wrong, unless it's just for testing:

// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"myExport" ofType:@"csv"];
NSData *myData = [NSData dataWithContentsOfFile:path];

the path you're using is to a file in your application bundle, which is read-only, so it can't be the CSV file you just made.

if you want to write a temporary file and email it, you need to write it to someplace like your Documents directory, i.e. a path like

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/myexport.csv", docDirectory];
0

精彩评论

暂无评论...
验证码 换一张
取 消