Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9000701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:20:36+00:00 2026-06-16T00:20:36+00:00

So I am creating a .csv file and then allowing the user to share

  • 0

So I am creating a .csv file and then allowing the user to share it using the UIActivityViewController.

My code to create the csv file will return the NSURL of the file:

- (NSURL *)exportToCSV
{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"results.csv"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:docPath]) {
        [[NSFileManager defaultManager] createFileAtPath:filePath
                                                contents:nil
                                              attributes:nil];
    }

    NSMutableString *contents = [NSMutableString stringWithCapacity:0];

    //fill contents with data in csv format
    // ...

    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
    [fileHandle writeData:[contents dataUsingEncoding:NSUTF8StringEncoding]];
    [fileHandle closeFile];

    return [NSURL fileURLWithPath:filePath];
}

and then my activity uses that NSURL to initiate the UIActivityViewController:

- (IBAction)shareButtonPressed:(id)sender {

    NSArray *activityItems = @[@"results.csv", [self.object exportToCSV]];
    UIActivityViewController *shareScreen = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];

    [self presentViewController:shareScreen animated:YES completion:nil];
}

When I select mail an option, the csv file is not attached. it just has the text “results.csv”

what am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:20:37+00:00Added an answer on June 16, 2026 at 12:20 am

    The problem would appear to be in your fileExistsAtPath line. You appear to be saying “if the documents directory doesn’t exist, then create the file”. That’s certainly not right.

    Personally, I’d lose that stuff and change it to be

    - (NSURL *)exportToCSV
    {
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
        NSString *filePath = [docPath stringByAppendingPathComponent:@"results.csv"];
    
        NSMutableString *contents = [NSMutableString stringWithCapacity:0];
    
        //fill contents with data in csv format
        // ...
    
        NSError *error;
    
        [contents writeToFile:filePath
                   atomically:YES
                     encoding:NSUTF8StringEncoding
                        error:&error];
    
        // check for the error
    
        return [NSURL fileURLWithPath:filePath];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a CSV File that is then mailed to a user group. In
Using PHP to extract data from SQL and then creating a .csv file on
I'm using Asp.net to create a csv file that the user can open directly
I am creating a website that pulls data from a CSV file and then
I'm creating a script that will read a csv file and display it on
I am creating a CSV file and I am using StringBuilder at the moment
I am currently creating a CSV file and then I ftp that file. This
Is it possible or any library available for creating .csv file in ObjC ?
I'm creating a csv file on the fly in my ASP.NET web app and
So I am creating a CSV file and everytime an action occurs I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.