Xamarin iOS WasCancelled Issue in UIDocumentPickerViewController

Liz 0 Reputation points
2023-06-28T14:18:36.1333333+00:00

I'm writing a document editing app. I am trying to use a Document Picker in iOS to 'Save As' or create a new file that I need to maintain write access to.

From what I can tell, the Document Picker is supposed to return the DidPickDocumentAtUrls event to tell me the new NSUrl for the moved file when the user clicks Save.

However, instead the document picker often incorrectly gives the WasCancelled event is incorrectly invoked so I can't get the new NSUrl.

I'm following these guides

https://developer.apple.com/documentation/uikit/uidocumentpickerviewcontroller

https://learn.microsoft.com/en-us/xamarin/ios/platform/document-picker

Steps to Reproduce

  1. Run my sample app at which is an iOS app that handles .txt files and use a UIDocumentPicker to write a .txt file to a new location (LINK https://github.com/elizeon/BugSample_iOSFilePickerCancelled)
  2. Click the Open Document Picker Write button
  3. Observe that if you choose to save in google drive, the NSUrl for the new file is not retrieved in an event. To check this, set a breakpoint in the WasCancelled and OnDocPickerFinishedPickingWRITE events and observe that when you click 'Save' in the UIDocument picker to write the new file, it calls only the WasCancelled event which does not have an NSUrl parameter.

4 Interestingly, observe that if you navigate to a different location such as dropbox or other cloud storage, the correct event is called which gives you an NSUrl. Very odd.

Here's a snippet of the sample code showing the setup of events.


        var docPicker = new UIDocumentPickerViewController(suggestedNSUrl, UIDocumentPickerMode.ExportToService);

        // Set event handlers
        docPicker.DidPickDocument += delegate (object o, UIDocumentPickedEventArgs e) { OnDocPickerFinishedPickingWRITE(o, e, contents); };
        docPicker.DidPickDocumentAtUrls += delegate (object o, UIDocumentPickedAtUrlsEventArgs e) { OnDocPickerFinishedPickingAtUrlsWRITE(o, e, contents); };
        docPicker.WasCancelled += OnDocPickerCancelledWRITE;

        // Present UIImagePickerController;
        UIWindow window = UIApplication.SharedApplication.KeyWindow;
        var viewController = window.RootViewController;
        viewController.PresentViewController(docPicker, true, null);
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,357 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,877 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Liz 0 Reputation points
    2023-06-30T07:06:56.19+00:00

    Thankfully it turns out the issue was resolved by updating Google Drive, so it's unlikely to be an issue affecting users unless their drive app is out of date.

    The Google Drive app was installed as a different user, so it hadn't updated for a while.

    It's a shame there wasn't a helpful error message. If anyone knows how I could show an error message when the UIDocumentPickerViewController fails to write like this, please let me know. The events just indicate that the USER cancelled the dialog box when that's not so.

    0 comments No comments

  2. george727 0 Reputation points
    2023-07-04T15:36:46.1933333+00:00

    It seems like you are experiencing an issue with the iOS Document Picker in your document editing app. The problem you described, where the WasCancelled event is incorrectly invoked instead of the DidPickDocumentAtUrls event, can be frustrating. Here are a few suggestions that might help you troubleshoot and resolve the issue:

    Verify permissions: Ensure that your app has the necessary permissions to access and write to the chosen file location. Check the required entitlements and file access permissions for the cloud storage services you are testing, such as Google Drive or Dropbox.

    Test with different cloud storage services: Since you mentioned that the issue occurs specifically when saving to Google Drive but not when using other services, it might be worth investigating if there are any specific differences or limitations in the way each cloud storage provider handles file operations. This could help pinpoint if the problem is related to a particular service.

    Test on different devices and iOS versions: It's possible that the issue could be device or iOS version-specific. Test your app on different iOS devices and versions to see if the problem persists consistently or if it varies.

    Check for any additional delegate methods: Review the documentation and make sure you are implementing all the necessary delegate methods correctly. There might be additional delegate methods that need to be handled to capture the desired behavior.

    Consider using alternative approaches: If the issue persists, you could explore alternative approaches for file handling and saving. For example, you might consider using the iOS File Provider framework or investigating third-party libraries that offer more flexible file management capabilities.

    If the problem persists after trying these suggestions, you may need to reach out to the Apple Developer Support or the Xamarin community for further assistance. They will have more expertise and resources to help you debug and resolve the issue specific to your app's implementation.also visit fontbots

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.