I have an iPhone app that exports CSV files. I am getting the runtime error "[AXRuntimeCommon] Unknown client: " when the fileWrapper function in the MessageDocument structure is called. I have four different CSV exporters and they all call the same MessageDocument structure.
The CSV files look fine when dragged into Numbers. Does the exporter not like CSV formatted data? The contentType is set to .plainText
Unknown Client: Is some kind of data formatter missing?
Below I have listed one of my CSV file exporters and the MessageDocument structure.
struct CreateCsvTable: View {
var startDate: Date
var endDate: Date
@EnvironmentObject var base: BaseCurrency
@EnvironmentObject var categories: Categories
@EnvironmentObject var userData: UserData
@State private var showingExporter: Bool = false
@State private var document: MessageDocument?
var dates: String = ""
var body: some View {
let totalValue = userData.grandTotal
VStack {
Button ( action: {
self.showingExporter = true
let dates = userData.formatCsvDate(startDate: startDate, endDate: endDate)
let sCode = base.baseCur.baseS // 3 digit country code
document = categories.createCategoryTotalData(dates: dates, sCode: sCode, totalValue: totalValue)
}) {
HStack (alignment: .firstTextBaseline) {
Text("Export Category Totals")
.fontWeight(.bold)
.font(.title3)
Image(systemName: "square.and.arrow.up")
}
}
}.fileExporter(
isPresented: $showingExporter,
document: document,
contentType: .plainText,
defaultFilename: "TripSenseTotals.csv"
) { result in
switch result {
case .success(let url):
print("Saved to \(url)")
case .failure(let error):
print(error.localizedDescription)
}
}
.navigationBarTitle(Text("Export Category Totals"), displayMode: .inline)
}
}
struct MessageDocument: FileDocument {
static var readableContentTypes: [UTType] { [.plainText] }
var message: String = ""
init(message: String) {
self.message = message
}
init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents,
let string = String(data: data, encoding: .utf8)
else {
throw CocoaError(.fileReadCorruptFile)
}
message = string
}
// this will be called when the system wants to write our data to disk
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
return FileWrapper(regularFileWithContents: message.data(using: .utf8)!)
}
}