问题
My apps ability to share a PDF to another third party app has broken in iOS 11. It shows the share sheet but when I choose to open it in a different (third-party) app, it simply dismisses the share sheet and does nothing.
Things I've confirmed:
- The delegate methods for willBeginSending and didEndSending are both called
- Opening first party apps like Mail or Notes works fine
- The document picker is still in memory
- Presenting a preview works fine but then the share button from inside the preview still will not open third party apps
- I have also tried using a
UIActivityViewController
. Sharing a URL produces the same behavior. Sharing the raw data allows you to "Create a PDF" and then sharing that PDF does actually work (but that user experience sucks).
Has anyone else experienced this or have any ideas on how to fix it?
Here is the full code of an example app that shows the problem. I created it in a new "single view" app. The storyboard only has two buttons hooked up to the two actions.
class ViewController: UIViewController {
var docController: UIDocumentInteractionController!
@IBAction func open(sender: UIButton) {
// self.docController.presentPreview(animated: true)
self.docController.presentOptionsMenu(from: sender.frame, in:self.view, animated:true)
}
@IBAction func check() {
print(self.docController)
}
override func viewDidLoad() {
super.viewDidLoad()
let url = Bundle.main.url(forResource: "OrderForm", withExtension: "pdf")!
self.docController = UIDocumentInteractionController(url: url)
self.docController.delegate = self
}
}
extension ViewController: UIDocumentInteractionControllerDelegate {
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
}
func documentInteractionController(_ controller: UIDocumentInteractionController, willBeginSendingToApplication application: String?) {
print("will begin")
}
func documentInteractionController(_ controller: UIDocumentInteractionController, didEndSendingToApplication application: String?) {
print("did end")
}
}
I also filed a bug with Apple (Problem ID: 34772214)
回答1:
The problem was that I was trying to share a URL to a file inside the app bundle. It seems Apple's apps have permission to access that URL but third-party apps don't. I solved this by first copying the resource to the cache directory and then using that cache directory URL for sharing.
来源:https://stackoverflow.com/questions/46531572/uidocumentinteractioncontroller-does-not-open-other-app-in-ios-11