UIActivityViewController cannot save plist to local application's Documents

I try to save a configuration file (is a property-list / NSDictionary) to the local application's documents folder.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"perf.plist"];

[self.partsConfigs writeToFile:path atomically:YES]; // partsConfigs is an NSDictionary

  NSURL *url = [NSURL fileURLWithPath:path];

UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:url, nil] applicationActivities:nil];
    activityViewController.popoverPresentationController.sourceView = self.view;
[self presentViewController:activityViewController animated:YES completion:nil];

I see the popover activity view to select destination, however I don't see the local application's documents folder.

And if I click to the option "Saves to files" I get this error (however the file corresponding to the passed url still exists)

[ShareSheet] cancelled request - error: The operation couldn’t be completed. Invalid argument

I also would like to be able for the user to select/change the file name
Should I use UIDocumentInteractionController instead ?
As far as I tried, I could save a file using UIActivityViewController with the same settings in Info.plist.
My app name appears under On My iPhone after choosing Save to Files, very similar to UIDocumentPickerViewController.

I also would like to be able for the user to select/change the file name

User can tap the name where save button is active, to rename the saved file.

[ShareSheet] cancelled request - error: The operation couldn’t be completed. Invalid argument

I could not reproduce this error. Have no idea what's affecting.
In fact the first popover displays fine, however when I choose Save to file, a new popover comes from bottom and immediately closes down (I havent't time to see its content).
I've not implemented any of the UIDocumentInteractionControllerDelegate methods, is there one required ?
Its from iOS 12.4 simulator.

I added activityViewController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {

                NSLog(@"act type %@",activityType);
 };

It was called after the second view disappeared.
activityType was : com.apple.CloudDocsUI.AddToiCloudDrive
completed : NO
returnedItems : nil
activityError : Error Domain=NSPOSIXErrorDomain Code=22 "Invalid argument"

In fact the first popover displays fine, however when I choose Save to file, a new popover comes from bottom and immediately closes down (I havent't time to see its content). 

Sorry, but I cannot reproduce the issue. I haven't implemented UIDocumentInteractionControllerDelegate nor set activityViewController.completionWithItemsHandler, but can see the second popover stable there.

Maybe sort of view hierarchy is affecting. You may need to clarify what is needed to reproduce the issue.
When I specify a message besides the url as items, the second popover stays fine.

initWithActivityItems:[NSArray arrayWithObjects:tempUrl, @"Save Perf", nil]

However what is saved is the corresponding text item, not the file corresponding to the url.

Also there is still mention to action type com.apple.CloudDocsUI.AddToiCloudDrive in the log (wich I didn't choose).
Seems you do not want to hear my suggestions. Hope you can solve your issue by yourself. Good luck.
I'm also sorry, don't know what I could have provided more. I have a single view controller, the app is simple.

I have a single view controller, the app is simple.

Thanks for showing additional info. But I tried in an app with a single view controller and UIActivityViewController worked as I described. So, that cannot be a good help to solve your issue.

I can say nothing more, if no info more.
There may be some readers who want to help if you could show enough info to help.

I created a new project (with the two required options in Info.plist - UIFileSharingEnabled and LSSupportsOpeningDocumentsInPlace to YES), and using a single view controller.

The second popover also goes down, as if it could't handle the item (plist file url), with error :

Failed to determine whether URL /Users/.../Library/Developer/CoreSimulator/Devices/.../data/Containers/Data/Application/.../Documents/perf.plist (n) is managed by a file provider

On iOS12.4 iPad target (simulator), and XCode 10.3.

UPDATE : it works on a real device (iPad mini 2) (the second popover stays, I can select On my iPad, and the application directory as target. However can't rename the file).


Code Block
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;
@property (strong, nonatomic) NSDictionary *partsConfigs;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (IBAction)saveFile:(id)sender {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"perf.plist"];
    NSDictionary *firstPartConfig = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Analog 4", @"PADS", nil] forKeys:[NSArray arrayWithObjects:@"name", @"category", nil]];
    self.partsConfigs = [NSDictionary dictionaryWithObject:firstPartConfig forKey:@"1"];
    [self.partsConfigs writeToFile:path atomically:YES];
    NSURL *url = [NSURL fileURLWithPath:path];
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:url, nil] applicationActivities:nil];
 activityViewController.popoverPresentationController.sourceView = self.view;
    [self presentViewController:activityViewController animated:YES completion:nil];
}
@end


Accepted Answer

However can't rename the file

As far as I tried, renaming feature was not supported in UIActivityViewController of iOS 12.
But not sure, it was just I could not find how or it did not exist.


The difference of the versions of Xcode may be affecting the results between us.
UIActivityViewController cannot save plist to local application's Documents
 
 
Q