UIDocumentInteractionController - delegate methods will not be called

Hello,

we are presenting a UIDocumentInteractionController within our app, so the user can share some documents. Sharing basically works but we are facing the problem that the two delegate methods

documentInteractionController(UIDocumentInteractionController, willBeginSendingToApplication: String?)

and

documentInteractionController(UIDocumentInteractionController, didEndSendingToApplication: String?)

are never being called. Other delegate methods such as

documentInteractionControllerWillBeginPreview(UIDocumentInteractionController)

are called just fine. Everything worked as expected when we last checked a year ago or so, but doesn't anymore now, even after updating to the latest iOS 18.3.

Does anybody know of a solution for this?

For reference, this is the simplified code we are using the reproduce the issue:

import UIKit
import OSLog

class ViewController: UIViewController, UIDocumentInteractionControllerDelegate {
    let log = Logger(subsystem: "com.me.pdfshare", category: "app")
    var documentInteractionController: UIDocumentInteractionController!

    override func viewDidLoad() {
        super.viewDidLoad()
        guard let pdfURL = Bundle.main.url(forResource: "test", withExtension: "pdf") else {
            return
        }
        
        documentInteractionController = UIDocumentInteractionController(url: pdfURL)
        documentInteractionController.delegate = self
        documentInteractionController.presentPreview(animated: true)
    }
    
    // MARK: - UIDocumentInteractionControllerDelegate
    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
        log.notice("documentInteractionControllerViewControllerForPreview")
        return self
    } // This will be called.
    
    func documentInteractionController(_ controller: UIDocumentInteractionController, willBeginSendingToApplication application: String?) {
        log.notice("willBeginSendingToApplication")
    } // This will NOT be called.
    
    func documentInteractionController(_ controller: UIDocumentInteractionController, didEndSendingToApplication application: String?) {
        log.notice("didEndSendingToApplication")
    } // This will NOT be called.
}
Answered by DTS Engineer in 827682022

My colleague invited me to take a look at this post, and so I am chiming in to comment.

Depending on how the target app handles the document you share, the delegate methods may or may not called. If the target app doesn’t even open the shared document, the delegate methods won’t be triggered. I guess that is what happened in your case.

To see how the delegate methods come to play, use the following steps:

  1. Download the Synchronizing Documents in the iCloud Environment sample, and manage to run on your device by following the steps in Readme. The sample defines its own document type and supports Open-in-place. You can use other apps for testing, but this sample has the source code, and so you can look into what it does when being launched by UIDocumentInteractionControler from the owner app side.

  2. Run the attached project (my test app) with Xcode, and tap the Share button to share the prepared “test.sicd” document. In the share sheet, you'd see SimpleiCloudDocument app.

  3. Tap the app in the share sheet to share the document in place. You'd see SimpleiCloudDocument opens “test.sicd”.

  4. In my test app, tap the app window to dismiss the share sheet, if it is still there.

You should see something similar to the following log in your Xcode console:

documentInteractionController(_:willBeginSendingToApplication:): Optional("com.example.apple-samplecode.SimpleiCloudDocumentMV96W6463W")
documentInteractionController(_:didEndSendingToApplication:): Optional("com.example.apple-samplecode.SimpleiCloudDocumentMV96W6463W")

The behavior of UIDocumentInteractionControler has been this way, and I've just confirmed that it doesn't change on iOS 18.3.1, the latest public release, and iOS 18.4 beta 2, the latest developer beta as of today.

I'm a bit surprised that your app used to work and was broken in iOS 18.3, and would suggest that you try the my steps described above, and share if you see the same result.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

I'd greatly appreciate it if you could open a bug report, include the OS version and build environment you tested on. Please post the FB number here once you do.

Bug Reporting: How and Why? has tips on creating your bug report.

Thanks, I just created a bug reported as suggested. The FB number is FB16430668.

My colleague invited me to take a look at this post, and so I am chiming in to comment.

Depending on how the target app handles the document you share, the delegate methods may or may not called. If the target app doesn’t even open the shared document, the delegate methods won’t be triggered. I guess that is what happened in your case.

To see how the delegate methods come to play, use the following steps:

  1. Download the Synchronizing Documents in the iCloud Environment sample, and manage to run on your device by following the steps in Readme. The sample defines its own document type and supports Open-in-place. You can use other apps for testing, but this sample has the source code, and so you can look into what it does when being launched by UIDocumentInteractionControler from the owner app side.

  2. Run the attached project (my test app) with Xcode, and tap the Share button to share the prepared “test.sicd” document. In the share sheet, you'd see SimpleiCloudDocument app.

  3. Tap the app in the share sheet to share the document in place. You'd see SimpleiCloudDocument opens “test.sicd”.

  4. In my test app, tap the app window to dismiss the share sheet, if it is still there.

You should see something similar to the following log in your Xcode console:

documentInteractionController(_:willBeginSendingToApplication:): Optional("com.example.apple-samplecode.SimpleiCloudDocumentMV96W6463W")
documentInteractionController(_:didEndSendingToApplication:): Optional("com.example.apple-samplecode.SimpleiCloudDocumentMV96W6463W")

The behavior of UIDocumentInteractionControler has been this way, and I've just confirmed that it doesn't change on iOS 18.3.1, the latest public release, and iOS 18.4 beta 2, the latest developer beta as of today.

I'm a bit surprised that your app used to work and was broken in iOS 18.3, and would suggest that you try the my steps described above, and share if you see the same result.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

OK, I just realize that I can't attach my test project, and so would like to briefly share how to create one:

  1. Use the Synchronizing Documents in the iCloud Environment sample app to create a .scid document named test.scid.

  2. Create a new iOS app project using Xcode.

  3. Add test.scid to the new project so you can share it with UIDocumentInteractionController.

  4. Add a Share button to configure and present a UIDocumentInteractionController instance.

Here is my UIKit code for step 4:

class ViewController: UIViewController, UIDocumentInteractionControllerDelegate {
    private var documentInteractionController: UIDocumentInteractionController?

    @IBAction func share(_ sender: Any) {
        let documentFolderURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last!
        let documentURL = documentFolderURL.appendingPathComponent("test.sicd")

        if !FileManager.default.fileExists(atPath: documentURL.path) {
            let originalURL = Bundle.main.url(forResource: "test", withExtension: "sicd")!
            try! FileManager.default.copyItem(at: originalURL, to: documentURL)
        }
        
        documentInteractionController = UIDocumentInteractionController(url: documentURL)
        documentInteractionController?.delegate = self
        documentInteractionController?.presentOpenInMenu(from: .zero, in: view, animated: true)
    }
    
    func documentInteractionController(_ controller: UIDocumentInteractionController,
                                       willBeginSendingToApplication application: String?) {
        print("\(#function): \(String(describing: application))")
    }

    func documentInteractionController(_ controller: UIDocumentInteractionController,
                                       didEndSendingToApplication application: String?) {
        print("\(#function): \(String(describing: application))")
    }
}

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

UIDocumentInteractionController - delegate methods will not be called
 
 
Q