How to access the .csv file directory through array - ios

I have created save button which I guess going to create a .csv file to store x,y and name of the object on screen since I can't check it because I can't access the file its created
#IBAction func saveButton(_ sender: Any) {
let objectsName = stringObjectName.joined(separator: ",")
let coX = stringObjectX.joined(separator: ",")
let coY = stringObjectY.joined(separator: ",")
let fileName = getDocumentsDirectory().appendingPathComponent("output.csv")
CSVFilesName.append(fileName)
var csvText = "Name, Data X, Data Y\n"
let count = stringObjectName.count
if count > 0 {
for _ in 0..<(stringObjectName.count-1) {
let newLine = "\(objectsName),\(coX),\(coY)\n"
csvText.append(contentsOf: newLine)
}
}
do {
try csvText.write(to: fileName, atomically: true, encoding: String.Encoding.utf8)
} catch {
print("error")
}
print(fileName)
}
after this I try to access the file call "output.csv" which it is suppose to store in document directory in another viewport. So, I created the method to pass the method from another view controller to current view controller by using and store CSVFile
var CSVFileName = [URL]()
func assignArray() {
let cameraVC = CameraViewController()
CSVFileName = cameraVC.CSVFilesName
print(CSVFileName)
}
and the problem start here since I have array which suppose to store file name in String
let fileNames = ["sth1.csv", "sth2.csv"]
but I can't find the convert CSVFileName from saving button to String, and replace "static array" into "dynamic array" and change the method below to get
URL from FileManager().default.url instead of fileURL to give TableViewController data to show and accessible
private func prepareFileURLS() {
for file in fileNames {
let fileParts = file.components(separatedBy: ".")
print(fileParts[0])
if let fileURL = Bundle.main.url(forResource: fileParts[0], withExtension: fileParts[1]) {
if FileManager.default.fileExists(atPath: fileURL.path) {
print(fileURL)
fileURLs.append(fileURL as NSURL)
}
}
}
print(fileURLs)
}

Here is the way you can read Your CSV file :
func filterMenuCsvData()
{
do {
// This solution assumes you've got the file in your bundle
if let path = Bundle.main.path(forResource: "products_category", ofType: "csv"){
// STORE CONTENT OF FILE IN VARIABLE
let data = try String(contentsOfFile:path, encoding: String.Encoding.utf8)
var rows : [String] = []
var readData = [String]()
rows = data.components(separatedBy: "\n")
for data in 0..<rows.count - 1{
if data == 0 || rows[data].contains(""){
continue
}
readData = rows[data].components(separatedBy: ",")
Category.append(readData[0])
if readData[2] != ""{
Occassions.append(readData[2])
}
selectedOccassionsRadioButtonIndex = Array(repeating: false, count: Occassions.count)
selectedCategoryRadioButtonIndex = Array(repeating: false, count: Category.count)
}
}
} catch let err as NSError {
// do something with Error}
print(err)
}
}

Related

Is it possible to output userinput to a .csv using swift?

I am very new to learning to code in Swift. I am trying to make an application that keeps a list of people who are coming in. I want it to log the name they input, time of visit, and the nature of their visit. However, I want this to be able to be exported to a program like Numbers or Excel. I have found some info on storing the inputs from the user but those seem to get deleted if the app is closed. I can't seem to find any other info, but perhaps I'm just searching the wrong info. Any help or guidance is appreciated.
Just store your log in a Array and then with this function you safe it to a .csv file.
func saveCSV(_ name : String,_ customUrl : URL) -> Bool {
let fileName = "\(name).csv"
let b = customUrl.appendingPathComponent(fileName)
var csvText = ""
var id = "1"
var name = "test"
csvText = "ID,Name\n"
let newLine = "\(id),\(name))\n"
csvText.append(newLine)
//or create a loop
// Task is my custom Struct
var array : [Task]
for task in customArray {
let newLine = "\(task.ean),\(task.menge),\(task.name)\n"
csvText.append(newLine)
}
do {
try csvText.write(to: b, atomically: true, encoding: String.Encoding.utf8)
return true
} catch {
print("Failed to create file")
print("\(error)")
return false
}
}
func createDic()->URL?{
let documentsPath1 = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
// CSV is the folder name
let logsPath = documentsPath1.appendingPathComponent("CSV")
do {
try FileManager.default.createDirectory(atPath: logsPath!.path, withIntermediateDirectories: true, attributes: nil)
return logsPath
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
return nil
}
var customUrl = createDic()
and now you can call it :
if saveCSV(userList, customUrl){
print("success")
}
and after this you can do what you want with the .csv file

swift 3.1 reading CSV or PLIST file from web

I'd like to use readStringFromURL method to obtain a file from a plist and then use it on insertDataInArrayFromPlist in order to display it or put it on CoreData, substituting let path = Bundle.main.path(forResource: plistFileName, ofType: plistFileExtension).
the ISSUE the try statement gives me this ERROR
Argument labels '(contentsOfURL:, usedEncoding:)' do not match any available overloads
in my viewDidLoad:
let obtainedfile = readStringFromURL(stringURL: kremoteSamplePlist)
print(obtainedfile ?? "nothing to print")
I retrive the file from web
func readStringFromURL(stringURL:String)-> String!{
if let url = NSURL(string: stringURL) {
do {
return try String(contentsOfURL: url, usedEncoding: nil)
} catch {
print("Cannot load contents")
return nil
}
} else {
print("String was not a URL")
return nil
}
}
then I put the data in a struct
func insertDataInArrayFromPlist(arrayOfEntities: inout [product]) {
let path = Bundle.main.path(forResource: plistFileName, ofType: plistFileExtension)
let localArray = NSArray(contentsOfFile: path!)!
for dict in localArray {
var futureEntity = product()
let bdict = dict as! [String: AnyObject]
futureEntity.name = bdict["Name"] as? String
futureEntity.ProductId = bdict["Product Id"] as? String
arrayOfEntities.append(futureEntity)
}
for element in arrayOfEntities {
print("name is \(element.name!), the id is \(element.ProductId!)")
}
}
Theres a library available via Cocoapods, CSV.swift by Yaslab. Allows you to import a csv directly in Swift code and convert to a data type of your own. Does the job for me.
https://github.com/yaslab/CSV.swift

Swift 3: cannot write data to plist file

I am trying to use a file called Data.plist to store some simple unstructured data, and I placed this file at the root folder of my app. To make it simple to read/write to this file, I created the following DataManager struct. It can read Data.plist file with no problem, but it cannot write data to the file. I am not sure where the problem is, could anyone spot where might be wrong?
struct DataManager {
static var shared = DataManager()
var dataFilePath: String? {
return Bundle.main.path(forResource: "Data", ofType: "plist")
}
var dict: NSMutableDictionary? {
guard let filePath = self.dataFilePath else { return nil }
return NSMutableDictionary(contentsOfFile: filePath)
}
let fileManager = FileManager.default
fileprivate init() {
guard let path = dataFilePath else { return }
guard fileManager.fileExists(atPath: path) else {
fileManager.createFile(atPath: path, contents: nil, attributes: nil) // create the file
print("created Data.plist file successfully")
return
}
}
func save(_ value: Any, for key: String) -> Bool {
guard let dict = dict else { return false }
dict.setObject(value, forKey: key as NSCopying)
dict.write(toFile: dataFilePath!, atomically: true)
// confirm
let resultDict = NSMutableDictionary(contentsOfFile: dataFilePath!)
print("saving, dict: \(resultDict)") // I can see this is working
return true
}
func delete(key: String) -> Bool {
guard let dict = dict else { return false }
dict.removeObject(forKey: key)
return true
}
func retrieve(for key: String) -> Any? {
guard let dict = dict else { return false }
return dict.object(forKey: key)
}
}
You cannot modify the files inside your app bundle. So all the files that you get with Bundle.main.path(forResource:ofType:) are readable but not writable.
If you want to modify this file you will need to copy it inside your app's document directory first.
let initialFileURL = URL(fileURLWithPath: Bundle.main.path(forResource: "Data", ofType: "plist")!)
let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last!
let writableFileURL = documentDirectoryURL.appendingPathComponent("Data.plist", isDirectory: false)
do {
try FileManager.default.copyItem(at: initialFileURL, to: writableFileURL)
} catch {
print("Copying file failed with error : \(error)")
}
// You can modify the file at writableFileURL

An efficient way of writing to file, swift

I'm getting data from sensors using bluetooth, I want to append the string of data I get to the end of file.
When I tried the regular approach
if let dir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
let path = NSURL(fileURLWithPath: dir).URLByAppendingPathComponent(self.file)
do {
try text.writeToURL(path, atomically: false, encoding: NSUTF8StringEncoding)
}
catch {/* error handling here */}
My app started to slow down until even labels were not updating anymore.
Tried using dispatch_async to do in background thread but still it was slowing down my app.
What approach should I use? I read sth about stream but failed to find some solutions in swift I could rely on
Probably your bluetooth is reading data faster than you are performing your file operations. You can optimize it by appending the text to the file instead of reading all the content on each write operation. You could also reuse the file handler between writes and keep the file open.
This sample is extracted from this answer:
struct MyStreamer: OutputStreamType {
lazy var fileHandle: NSFileHandle? = {
let fileHandle = NSFileHandle(forWritingAtPath: self.logPath)
return fileHandle
}()
lazy var logPath: String = {
let path : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first!
let filePath = (path as NSString).stringByAppendingPathComponent("log.txt")
if !NSFileManager.defaultManager().fileExistsAtPath(filePath) {
NSFileManager.defaultManager().createFileAtPath(filePath, contents: nil, attributes: nil)
}
print(filePath)
return filePath
}()
mutating func write(string: String) {
print(fileHandle)
fileHandle?.seekToEndOfFile()
fileHandle?.writeData(string.dataUsingEncoding(NSUTF8StringEncoding)!)
}
}
Then, you can create a single streamer and reuse it in different writes:
var myStream = MyStreamer()
myStream.write("First of all")
myStream.write("Then after")
myStream.write("And, finally")
In this case, you have the bonus that MyStreamer is also a OutputStreamType, so you can use it like this:
var myStream = MyStreamer()
print("First of all", toStream: &myStream )
print("Then after", toStream: &myStream)
print("And, finally", toStream: &myStream)
Finally I'd recommend you to move 'log.txt' string to a instance variable and pass it as a constructor parameter:
var myStream = MyStreamer("log.txt")
More info about file handler in the Apple Docs.
update #redent84's, to work in Swift 5
Code:
struct MyStreamer{
lazy var fileHandle = FileHandle(forWritingAtPath: logPath)
lazy var logPath: String = {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .allDomainsMask, true)[0]
let filePath = path + "/log.txt"
if FileManager.default.fileExists(atPath: filePath) == false{
FileManager.default.createFile(atPath: filePath, contents: nil, attributes: nil)
}
print(filePath)
return filePath
}()
mutating func write(_ string: String) {
print(fileHandle?.description ?? "呵呵")
fileHandle?.seekToEndOfFile()
if let data = string.data(using: String.Encoding.utf8){
fileHandle?.write(data)
}
}
}
Usage:
var myStream = MyStreamer()
myStream.write("First of all")
myStream.write("Then after")
myStream.write("And, finally")
try to write file like this..
var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let filePath = paths[0].stringByAppendingString("/filename.mov")
do
{
try NSFileManager.defaultManager().removeItemAtURL(outputURL)
}
catch
{
error as NSError
}
do {
try text.writeToURL(path, atomically: false, encoding: NSUTF8StringEncoding)
}
I mean to say at last is you have to remove first.. If any query you can ask me

How do i exactly export a csv file from iOS written in swift?

I am trying to export an cvs file.
With the following code i manage to get the file
let fileName = "sample.csv"//"sample.txt"
#IBAction func createFile(sender: AnyObject) {
let path = tmpDir.stringByAppendingPathComponent(fileName)
let contentsOfFile = "No,President Name,Wikipedia URL,Took office,Left office,Party,Home State\n1,George Washington,http://en.wikipedia.org/wiki/George_Washington,30/04/1789,4/03/1797,Independent,Virginia\n2,John Adams,http://en.wikipedia.org/wiki/John_Adams,4/03/1797,4/03/1801,Federalist,Massachusetts\n3,Thomas Jefferson,http://en.wikipedia.org/wiki/Thomas_Jefferson,4/03/1801,4/03/1809,Democratic-Republican,Virginia\n4,James Madison,http://en.wikipedia.org/wiki/James_Madison,4/03/1809,4/03/1817,Democratic-Republican,Virginia\n5,James Monroe,http://en.wikipedia.org/wiki/James_Monroe,4/03/1817,4/03/1825,Democratic-Republican,Virginia\n6,John Quincy Adams,http://en.wikipedia.org/wiki/John_Quincy_Adams,4/03/1825,4/03/1829,Democratic-Republican/National Republican,Massachusetts"
//"Sample Text repacement for future cvs data"content to save
// Write File
do {
try contentsOfFile.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding)
print("File sample.txt created at tmp directory")
} catch {
print("Failed to create file")
print("\(error)")
}
}
// Share button
#IBAction func shareDoc(sender: AnyObject) {
print("test share file")
docController.UTI = "public.comma-separated-values-text"
docController.delegate = self//delegate
docController.name = "Export Data"
docController.presentOptionsMenuFromBarButtonItem(sender as! UIBarButtonItem, animated: true)
//}
}
When i click the share file button in the simulator i see the following:
and with quick look it shows
So the next thing i did was testing with my iphone 5 and i tried to email sample.csv but i am only getting the message body and not the csv file???
how can i actually email the .csv file?
which export possibilities are there?
In order to email the .csv file, you can do the following:
Add this import to the top of the class. It allows you to use MFMailComposeViewController, which is a way to send emails.
import MessageUI
Generate your data, a sample I have done is:
// Creating a string.
var mailString = NSMutableString()
mailString.appendString("Column A, Column B\n")
mailString.appendString("Row 1 Column A, Row 1 Column B\n")
mailString.appendString("Row 2 Column A, Row 2 Column B\n")
// Converting it to NSData.
let data = mailString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
// Unwrapping the optional.
if let content = data {
print("NSData: \(content)")
}
Generate the MFMailComposeViewController
// Generating the email controller.
func configuredMailComposeViewController() -> MFMailComposeViewController {
let emailController = MFMailComposeViewController()
emailController.mailComposeDelegate = self
emailController.setSubject("CSV File")
emailController.setMessageBody("", isHTML: false)
// Attaching the .CSV file to the email.
emailController.addAttachmentData(data!, mimeType: "text/csv", fileName: "Sample.csv")
return emailController
}
// If the view controller can send the email.
// This will show an email-style popup that allows you to enter
// Who to send the email to, the subject, the cc's and the message.
// As the .CSV is already attached, you can simply add an email
// and press send.
let emailViewController = configuredMailComposeViewController()
if MFMailComposeViewController.canSendMail() {
self.presentViewController(emailViewController, animated: true, completion: nil)
}
In your case, as you have already created the file, you can simply attach that directly by changing the line where the CSV is attached to the mail for:
emailController.addAttachmentData(NSData(contentsOfFile: "YourFile")!, mimeType: "text/csv", fileName: "Sample.csv")
Answer based on: Attach csv to email xcode ,
Create CSV file in Swift and write to file
Creating CSV file in Swift
class ViewController: UIViewController {
var taskArr = [Task]()
var task: Task!
override func viewDidLoad() {
super.viewDidLoad()
task = Task()
for _ in 0..<5 {
task.name = "Raj"
task.date = "\(Date())"
task.startTime = "Start \(Date())"
task.endTime = "End \(Date())"
taskArr.append(task!)
}
creatCSV()
}
// MARK: CSV file creating
func creatCSV() -> Void {
let fileName = "Tasks.csv"
let path = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
var csvText = "Date,Task Name,Time Started,Time Ended\n"
for task in taskArr {
let newLine = "\(task.date),\(task.name),\(task.startTime),\(task.endTime)\n"
csvText.append(newLine)
}
do {
try csvText.write(to: path!, atomically: true, encoding: String.Encoding.utf8)
} catch {
print("Failed to create file")
print("\(error)")
}
print(path ?? "not found")
}
}
Task Model class
class Task: NSObject {
var date: String = ""
var name: String = ""
var startTime: String = ""
var endTime: String = ""
}
CSV output show as below format
SWIFT 5
func creatCSV() {
let fileName = "exportar_serv.csv"
/* CREAR UN ARCHIVO NUEVO EN EL DIRECTORIO PRINCIPAL */
guard let path = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent(fileName) as NSURL else {
return }
var csvText = "id,name,imageName\n"
for task in taskArr {
let x : Int64 = task.id!
let id2 = String(x)
let newLine = "\(id2),\(task.name),\(task.imageName)\n"
csvText.append(newLine)
} // for
do {
try csvText.write(to: path as URL, atomically: true, encoding: String.Encoding.utf8)
print("DATOS GUARDADOS")
} catch {
print("Failed to create file")
print("\(error)")
} // catch
} // CreateCSV
Swift 5.0.1, Xcode 10.2.1 version
private func injectExportButton() {
var csvIcon: UIImage
switch self.theme {
case .dark:
csvIcon = UIImage(named: "csv-export.dark", in: Bundle.framework)!
case .light:
csvIcon = UIImage(named: "csv-export.light", in: Bundle.framework)!
}
let imgWidth = csvIcon.size.width
let imgHeight = csvIcon.size.height
let button: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: imgWidth, height: imgHeight))
button.setBackgroundImage(csvIcon, for: UIControl.State())
button.alpha = 0
button.addTarget(self, action: #selector(csvExportButtonClicked), for: .touchUpInside)
UIView.animate(withDuration: 0.5) {
button.alpha = 1
}
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
}
#objc private func csvExportButtonClicked() {
debugPrint("export clicked")
createCSV()
}
private func createCSV() -> Void {
let fileName = getDocumentsDirectory().appendingPathComponent("OutputD.csv")
var csvOutputText = "Result, Date, Name\n"
history.results.forEach { result in
let newLine = "\(String(describing: result.value)),\(String(describing: result.date)),\(String(describing: result.name))\n"
csvOutputText.append(newLine)
}
do {
try csvOutputText.write(to: fileName, atomically: true, encoding: String.Encoding.utf8)
} catch {
print("Failed to create file")
print("\(error)")
}
let activity = UIActivityViewController(activityItems: ["your results", fileName], applicationActivities: nil)
present(activity, animated: true)
}
private func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}

Resources