如何通过 UActivityViewController 命名存储到文件应用程序的文件
How to name file stored to Files app via UActivityViewController
我的应用程序中的字符串变量中存储了内容,我想将其作为文件保存到“文件”应用程序中。好吧,这很简单,例如在此处 进行了描述,但我想知道是否可以为文件指定自定义名称。 iOS 分配隐式名称 Text.txt。
谢谢
@IBAction func btnExportData(_ sender: Any) {
let prompt = UIAlertController(title: "Export data for counter", message: "Provide file name:", preferredStyle: .alert)
prompt.addTextField {(textField) in textField.text = ""}
prompt.addAction(UIAlertAction(title: "Export", style: .default, handler: {
(_) in
do {
let export = try self.objMeasurements.exportData(forCounterID: self.counterID)
let fm = FileManager.default
let fileFolder = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = fileFolder.appendingPathComponent(prompt.textFields![0].text! + ".csv")
do {
try export.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
} catch {
self.showOkAlert(messageTitle: "File " + fileURL.absoluteString + " can't be created.", messageText: "Method: btnExportData", okText: "OK", {})
}
let sharing = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
sharing.completionWithItemsHandler = {
(type, completed, items, error) in
do {
try fm.removeItem(at: fileURL)
self.showOkAlert(messageTitle: "Temporary file was successfully deleted.", messageText: fileURL.absoluteString, okText: "OK", {})
} catch {
self.showOkAlert(messageTitle: "File " + fileURL.absoluteString + " can't be deleted.", messageText: "Method: btnExportData", okText: "OK", {})
}
}
sharing.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItems?.first
self.present(sharing, animated: true, completion: nil)
} catch measurementErrors.measurementGetFailed(let message) {
self.showOkAlert(messageTitle: message[0], messageText: message[1], okText: "OK", {})
} catch counterErrors.counterGetFailed(let message) {
self.showOkAlert(messageTitle: message[0], messageText: message[1], okText: "OK", {})
} catch elementErrors.elementGetFailed(let message) {
self.showOkAlert(messageTitle: message[0], messageText: message[1], okText: "OK", {})
} catch {
self.showOkAlert(messageTitle: "Unknown error during data export", messageText: "Method: btnExportData", okText: "OK", {})
}
}))
prompt.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(prompt, animated: true, completion: nil)
}
我的应用程序中的字符串变量中存储了内容,我想将其作为文件保存到“文件”应用程序中。好吧,这很简单,例如在此处
@IBAction func btnExportData(_ sender: Any) {
let prompt = UIAlertController(title: "Export data for counter", message: "Provide file name:", preferredStyle: .alert)
prompt.addTextField {(textField) in textField.text = ""}
prompt.addAction(UIAlertAction(title: "Export", style: .default, handler: {
(_) in
do {
let export = try self.objMeasurements.exportData(forCounterID: self.counterID)
let fm = FileManager.default
let fileFolder = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = fileFolder.appendingPathComponent(prompt.textFields![0].text! + ".csv")
do {
try export.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
} catch {
self.showOkAlert(messageTitle: "File " + fileURL.absoluteString + " can't be created.", messageText: "Method: btnExportData", okText: "OK", {})
}
let sharing = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
sharing.completionWithItemsHandler = {
(type, completed, items, error) in
do {
try fm.removeItem(at: fileURL)
self.showOkAlert(messageTitle: "Temporary file was successfully deleted.", messageText: fileURL.absoluteString, okText: "OK", {})
} catch {
self.showOkAlert(messageTitle: "File " + fileURL.absoluteString + " can't be deleted.", messageText: "Method: btnExportData", okText: "OK", {})
}
}
sharing.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItems?.first
self.present(sharing, animated: true, completion: nil)
} catch measurementErrors.measurementGetFailed(let message) {
self.showOkAlert(messageTitle: message[0], messageText: message[1], okText: "OK", {})
} catch counterErrors.counterGetFailed(let message) {
self.showOkAlert(messageTitle: message[0], messageText: message[1], okText: "OK", {})
} catch elementErrors.elementGetFailed(let message) {
self.showOkAlert(messageTitle: message[0], messageText: message[1], okText: "OK", {})
} catch {
self.showOkAlert(messageTitle: "Unknown error during data export", messageText: "Method: btnExportData", okText: "OK", {})
}
}))
prompt.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(prompt, animated: true, completion: nil)
}