Senden Sie eine iphone-Anhang per E-Mail programmgesteuert

Schreibe ich eine iPhone-app, die erfordert, dass ich senden Sie eine e-mail-Anlage, die programmgesteuert. Die Anlage ist eine csv-Datei, die ich erstellen durch den code. Ich fügen Sie dann die Datei an die E-Mail und der Anhang zeigt auf das Telefon. Wenn ich senden Sie die E-Mail an mich, aber, die Anlage erscheint nicht in der e-mail. Hier ist der code, den ich verwende.

    [self exportData];

if ([MFMailComposeViewController canSendMail])
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"expenses" ofType:@"csv"];  
    NSData *myData = [NSData dataWithContentsOfFile:filePath]; 

    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

    mailer.mailComposeDelegate = self;

    [mailer setSubject:@"Vehicle Expenses from myConsultant"];

    NSString *emailBody = @"";
    [mailer setMessageBody:emailBody isHTML:NO];

    [mailer addAttachmentData:myData mimeType:@"text/plain" fileName:@"expenses"];

    [self presentModalViewController:mailer animated:YES];

    [mailer release]; 
}
else
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                    message:@"Your device doesn't support the composer sheet"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved: you saved the email message in the drafts folder.");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
        break;
        default:
            NSLog(@"Mail not sent.");
        break;
}

//Remove the mail view
[self dismissModalViewControllerAnimated:YES];

Das wird erfolgreich erstellt - ich überprüfte in der simulator-Dateien.

- (void) exportData
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *root = [documentsDir stringByAppendingPathComponent:@"expenses.csv"];
    NSString *temp=@"Date,Purpose,Start Odometer,End Odometer, Total Driven, Fees, ";
    for(int i = 0; i < expenses.count; i++){
        VehicleExpense *tempExpense = [expenses objectAtIndex:i];
        temp = [temp stringByAppendingString:tempExpense.date];
        temp = [temp stringByAppendingString:@", "];
        temp = [temp stringByAppendingString:tempExpense.purpose];
        temp = [temp stringByAppendingString:@", "];
        temp = [temp stringByAppendingString:[NSString stringWithFormat: @"%.02f",tempExpense.start_mile]];
        temp = [temp stringByAppendingString:@", "];
        temp = [temp stringByAppendingString:[NSString stringWithFormat: @"%.02f",tempExpense.end_mile]];
        temp = [temp stringByAppendingString:@", "];
        temp = [temp stringByAppendingString:[NSString stringWithFormat: @"%.02f",tempExpense.distance]];
        temp = [temp stringByAppendingString:@", "];
        temp = [temp stringByAppendingString:[NSString stringWithFormat: @"%.02f",tempExpense.fees]];
        temp = [temp stringByAppendingString:@", "];
    }
    [temp writeToFile:root atomically:YES encoding:NSUTF8StringEncoding error:NULL];
    NSLog(@"got here in export data--- %@", documentsDir);

}
Versuchen Sie, sich anzumelden myData, um zu sehen, wenn es nicht die Rückgabe nil.
Es ist null...wie bekomme ich die Datei? Bin ich der Bearbeitung meiner ursprünglichen post zeigen, wo ich die Datei erstellen.
Es scheint, wie filePath = [[NSBundle mainBundle] pathForResource:@"expenses" ofType:@"csv"]; nicht den Pfad der Datei, die Sie suchen. Wenn ich richtig bin, diese Methode wird nur Ressourcen in Ihre .app-Ordner. Ich empfehle Ihnen, sich den Pfad auf die gleiche Weise tun, in Ihren exportData-Methode.
Dass es gelöst. Danke!

InformationsquelleAutor coder | 2011-10-19

Schreibe einen Kommentar