documentInteractionController didEndSendingToApplication未被调用。文件未被转移

时间:2017-03-30 18:55:44

标签: ios swift3 uidocumentinteraction

我的UIDocumentInteractionController有问题。我试图通过转移到支持它们的应用程序来处理不受支持的文件类型。以下是我的相关代码。

 class AttachmentsView: UICollectionView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIDocumentInteractionControllerDelegate {

 var docController: UIDocumentInteractionController!


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        switch filetype {

        case: .image:
        return

        case: .document:
        return

        case: .audio:
        return

        case: .video:
        return

        case .unknown:
        let unknownFile = files[(indexPath as NSIndexPath).item]

        guard let fileURL = unknownFile.fullPath else {

            return
        }

        self.docController = UIDocumentInteractionController(url: fileURL)
        self.docController.delegate = self
        self.docController.url = fileURL

        self.docController.presentOpenInMenu(from: (superview?.frame)!, in: self, animated: true)
        return
 }
}

这是fileURL的样子 -

file:///private/var/mobile/Containers/Data/Application/1E67D26D-B9FE-4455-8D30-6A4FED07071E/tmp/folder_attachments/a9500123-c5e6-469b-8867-af72050f453e/1b177057-c507-445a-a8a0-b7b349ae79c6.zip

在这个例子中,它是一个.zip文件。 UIDocumentInteractionController作为活动表类型视图就可以了。显示AirDrop,添加到Notes,添加到运行状况,使用Dropbox导入等等。我也可以添加到iCloud驱动器。

当我点击Dropbox选项时,它会通过:

func documentInteractionController(_ controller: UIDocumentInteractionController, willBeginSendingToApplication application: String?) {
    print("begin sending")
}

但是没有通过:

func documentInteractionController(_ controller: UIDocumentInteractionController, didEndSendingToApplication application: String?) {
    print("completed")
}

另一个提示是当我点击AirDrop时我得到Error = "Error Domain=SFOperation Code=-5 \"The transfer failed because you tried to send an invalid file.\"

知道为什么会这样吗?谢谢。

1 个答案:

答案 0 :(得分:0)

您的文件路径显示答案:您以错误的方式构建路径,因此它指向私有区域。您可以使用Bundle的pathForResource方法确保文件路径有效,或者构建以例如NSSearchPathForDirectoriesInDomains,将其转换为网址(使用URL(fileURLWithPath: folderPath)),然后使用appendingPathComponent附加组件。