How to name file stored to Files app via UActivityViewController
@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)
}
UIActivityViewController - preserve file name of attached files from read from URL
yes I think it is possible, try this
NSString *str = [[NSBundle mainBundle] pathForResource:@"AppDistributionGuide" ofType:@"pdf"];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[@"Test", [NSURL fileURLWithPath:str]] applicationActivities:nil];
Related Topics
Issues While Lightweight Core Data Migration
How to Catch an Exception in Swift
How to Remove Items from an Array When Deselecting a Row in a UItableview
Realm + Nstableview + Nsarraycontroller
iOS 14 Widget Detect System Theme Change
How to Write a Generic Function for Floating Point Values in Swift
How to Change/Modify The Displayed Title of an Nspopupbutton
Pdf417 Decode and Generate The Same Barcode Using Swift
Cleanly Handling /Usr/Local/ with Swift Package Manager and Libevent
Combining Scenekit and Spritekit in a Single Screen
Swift Pattern Match on Array<Any>
Macos, Swift 3: How to Get Data Back After Segue
Swift Running Code in Periodically Background
Abstract Class and Abstract Function in Swift
Arkit - Place a Scnplane Between 2 Vector Points on a Plane in Swift 3
How to Move an Object Towards a Direction Without Stopping
Transparency Issues with Repeated Stamping of Textures on an Mtkview