thomasguenzel.com

Apps/Programming

UIActivityViewController NSData with Filename

In one of my upcoming apps I want the user to be able to send a file using the UIActivityViewController. As I create the NSData object during runtime, I passed the data to the activity view like this:

NSData *someData = /*...*/;
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[someData] applicationActivities:nil];

But unfortunately this resulted in something like this:

without_filename

So in order to get a filename instead of “Attachment-1” I create a file in the temporary directory and pass the activity view the url.

// create url
NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@"My File.txt"]];
NSData *data = /*...*/;
// write data
[dataToWrite writeToURL:url atomically:NO];

// create activity view controller
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[url] applicationActivities:nil];
[activityViewController setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
	//Delete file
	NSError *errorBlock;
	if([[NSFileManager defaultManager] removeItemAtURL:url error:&errorBlock] == NO) {
		NSLog(@"error deleting file %@",error);
		return;
	}
}];

As a result, the messages screen looks now like this:

with_filename

8 Comments

  1. Hi! nice tutorial. It works for messages app, but did you test to share a mail with this code? I get next error and don’t know why…

    [General] #Attachments Error confirming URL is readable [file:/private/var/mobile/Containers/Data/Application/3E4D88D3-DCC7-4461-9183-3537FCCFC074/tmp/aoi_history.pdf — file:///]

    • Thomas Günzel

      February 6, 2017 at 14:35

      Hey Carlos,

      I just tried to share a mail containing the file and it worked. Are you sure that the file exists when opening the UIActivityViewController? If you unzipped or generated the file, you might also want to check whether the file has the correct permissions set (although this is unlikely on iOS).

      Thomas

  2. Thanks Thomas for answering,

    sure file exists, I download it from api, write local, and show inside a UIWebView and from this view I try to share it. For messages and dropbox works fine, but mail throws the error I have mentionated before…

    • Thomas Günzel

      February 6, 2017 at 16:37

      Hey Carlos,

      I’ve tried it a few more times, using differently constructed URLs and different files, but I couldn’t reproduce the error. The only help I could find was http://stackoverflow.com/questions/21867923/ios-7-uiactivityviewcontroller-email-attachment maybe this solves your problem.

      Thomas

      • I don’t know why exactly, but when I have changed the creation of the NSUrl from:

        NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
        NSURL *fileURL = [tmpDirURL URLByAppendingPathComponent:@”aoi_history.pdf”];

        to:

        NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:filename]];

        work fine in every share option…

        Thanks for your help Thomas!

  3. How to share audio file with this in swift3 please update code in swift3 with audio file sharing example.

    Thanx,
    Uzer

    • Thomas Günzel

      May 24, 2017 at 13:07

      Hey Uzer,

      unfortunately, I do not like Swift at all. Therefore I’m trying to code as little as possible in Swift and I won’t update any code on this site to Swift. If you take a look at the documentation for NSURL/URL, NSData/Data, UIActivityController and NSFileManager/FileManager it should be easy to translate the code to Swift (you can even switch the documentation between Obj-C and Swift).

      Thanks,
      Thomas

  4. file:///var/mobile/Containers/Data/Application/5A158F94-39D0-4DCE-9B51-EA5A09566B93/Documents/sound.caf

    My Audio url is generate above type how can i share this url with default share in swift3

Leave a Reply

Your email address will not be published.

© 2017 thomasguenzel.com

Theme by Anders NorenUp ↑