Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. var logs : [Log] = []
  2.  
  3. func createExportString() -> String {
  4.  
  5.  
  6. var date: NSDate? = NSDate()
  7. var labe1: String?
  8. var typ: Double
  9.  
  10. var export: String = NSLocalizedString("date, label, typ, n", comment: "")
  11.  
  12.  
  13. for (index, log) in logs.enumerated() {
  14. if index < logs.count - 1 {
  15.  
  16. date = Date() as? NSDate
  17. label = log.value(forKey: "time") as? String
  18. typ = (log.value(forKey: "type") as? Double)!
  19.  
  20.  
  21. let dateString = "(log.date!)"
  22. let labelString = "(log.labe1!)"
  23. let typeString = "(log.typ)"
  24.  
  25. export += dateString + "," + labelString + "," + typeString + "," + "n"
  26. }
  27. }
  28. print("This is what the app will export: (export)")
  29. return export
  30. }
  31.  
  32. func exportDatabase() {
  33. var exportString = createExportString()
  34. saveAndExport(exportString: exportString)
  35. }
  36.  
  37.  
  38. func saveAndExport(exportString: String) {
  39. let exportFilePath = NSTemporaryDirectory() + "export.csv"
  40. let exportFileURL = NSURL(fileURLWithPath: exportFilePath)
  41. FileManager.default.createFile(atPath: exportFilePath, contents: NSData() as Data, attributes: nil)
  42. var fileHandleError: NSError? = nil
  43. var fileHandle: FileHandle? = nil
  44. do {
  45. fileHandle = try FileHandle(forWritingTo: exportFileURL as URL)
  46. } catch {
  47. print("Error with fileHandle")
  48. }
  49.  
  50. if fileHandle != nil {
  51. fileHandle!.seekToEndOfFile()
  52. let csvData = exportString.data(using: String.Encoding.utf8, allowLossyConversion: false)
  53. fileHandle!.write(csvData!)
  54.  
  55. fileHandle!.closeFile()
  56.  
  57. let firstActivityItem = NSURL(fileURLWithPath: exportFilePath)
  58. let activityViewController : UIActivityViewController = UIActivityViewController(
  59. activityItems: [firstActivityItem], applicationActivities: nil)
  60.  
  61. activityViewController.excludedActivityTypes = [
  62. UIActivityType.assignToContact,
  63. UIActivityType.saveToCameraRoll,
  64. UIActivityType.postToFlickr,
  65. UIActivityType.postToVimeo,
  66. UIActivityType.postToTencentWeibo
  67. ]
  68.  
  69. self.present(activityViewController, animated: true, completion: nil)
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement