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:
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:



Recent Comments