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.
}
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:
-
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. -
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 seeSimpleiCloudDocument
app. -
Tap the app in the share sheet to share the document in place. You'd see SimpleiCloudDocument opens “test.sicd”.
-
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.