Swift 确定用户保存文档后何时取消共享表
我向用户展示了一个共享表,这显然允许他们做很多事情,但在我的例子中,他们正在将一个gpx文件保存到他们选择的位置 我希望在文档成功保存后显示UIView,因此我的方法是在操作系统关闭共享表后显示UIView。但是,我遇到的问题是,如果用户在不保存文件的情况下手动取消共享表选项,则会调用委托方法,而我只希望在用户从共享表中选择选项时调用委托方法 我的代码:Swift 确定用户保存文档后何时取消共享表,swift,xcode,ios-sharesheet,Swift,Xcode,Ios Sharesheet,我向用户展示了一个共享表,这显然允许他们做很多事情,但在我的例子中,他们正在将一个gpx文件保存到他们选择的位置 我希望在文档成功保存后显示UIView,因此我的方法是在操作系统关闭共享表后显示UIView。但是,我遇到的问题是,如果用户在不保存文件的情况下手动取消共享表选项,则会调用委托方法,而我只希望在用户从共享表中选择选项时调用委托方法 我的代码: func shareAction() { if let dir = FileManager.default.urls(for: .do
func shareAction() {
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileURL = dir.appendingPathComponent("\(gpxNameTextField.text!).gpx")
URLSession.shared.dataTask(with: fileURL) { data, response, error in
guard let data = data, error == nil else { return }
let tmpURL = FileManager.default.temporaryDirectory
.appendingPathComponent(response?.suggestedFilename ?? "\(self.gpxNameTextField.text!)")
do {
try data.write(to: tmpURL)
DispatchQueue.main.async {
self.share(url: tmpURL)
self.deleteDraftGPXFile()
}
}
catch {
self.presentAlertView(title: "Error Saving File", message: "There was an error saving the GPX file to disk./nError: \(error.localizedDescription)")
print(error)
}
}.resume()
}
}
func share(url: URL) {
documentInteractionController.url = url
documentInteractionController.uti = url.typeIdentifier ?? "public.data, public.content"
documentInteractionController.name = url.localizedName ?? url.lastPathComponent
documentInteractionController.presentOptionsMenu(from: view.frame, in: view, animated: true)
documentInteractionController.delegate = self
}
并使用这种方法
sharedAction() { res in
if res {
// File was saved
}
感谢您的建议,但不幸的是,在用户进行任何交互之前,共享表一出现,回调就会返回true。使用是的,这就是我正在使用的,但即使用户通过向下滑动或点击“x”关闭控制器来解除InteractionController,代理也会被调用。我需要确保用户实际共享或保存文件。
sharedAction() { res in
if res {
// File was saved
}