我正在向用户展示一个共享表,这显然允许他们做很多事情,但就我而言,他们正在将 gpx 文件保存到他们选择的位置。
我想在文档成功保存后显示 UIView,所以我的方法是在操作系统关闭共享表后显示 UIView。但是,我遇到的问题是,如果用户手动关闭共享表选项而不保存文件,则调用委托方法,并且我只希望在用户从共享表中选择一个选项时调用委托方法。
我的代码:
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
}