您最好的选择是使用邮件共享操作编写导出器。在我的一个应用程序中,我允许用户通过电子邮件发送 sqlite 文件的副本来导出所有数据。
func exportAllDataSqlite() {
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
var newFilePath: URL!
var mutablePathComponents = [String]()
var sqliteFileCopied = false
do {
let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: FileManager.DirectoryEnumerationOptions())
for f in directoryContents {
let pathComponents = f.pathComponents
if pathComponents.last == "XXXXWhatever your file is called when created by the persisten store.sqliteXXXXX" {
//create a copy of the file with a dated file name
mutablePathComponents = pathComponents
let dateComponents = (Calendar.current as NSCalendar).components([.day, .month, .year], from: Date())
let dateString = "\(dateComponents.day)-\(dateComponents.month)-\(dateComponents.year)"
mutablePathComponents[mutablePathComponents.count-1] = "Events App \(dateString).sqlite"
newFilePath = NSURL.fileURL(withPathComponents: mutablePathComponents)
do {
try FileManager.default.copyItem(at: f, to: newFilePath!)
sqliteFileCopied = true
print("Copied sqlite file")
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
} catch let error as NSError {
print(error.localizedDescription)
}
if sqliteFileCopied == true {
//sharing
let activityItem:URL = newFilePath!
let objectsToShare = [activityItem]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.completionWithItemsHandler = { activity, success, items, error in
do {
try FileManager.default.removeItem(at: newFilePath!)
print("Deleted file: \(newFilePath!)")
} catch let error as NSError {
print(error.localizedDescription)
}
}
let excludeActivities = [UIActivityType.airDrop,
UIActivityType.print,
UIActivityType.assignToContact,
UIActivityType.saveToCameraRoll,
UIActivityType.addToReadingList,
UIActivityType.postToFlickr,
UIActivityType.postToVimeo]
activityVC.excludedActivityTypes = excludeActivities
self.present(activityVC, animated: true, completion: nil)
} else {
print("file not copied so can't be shared")
}
}
这是我最早的一些 Swift,所以不是很好,但它确实有效。前几天我在我的应用程序的部署副本中使用它来调试问题,只需通过电子邮件发送给自己并在你的 Mac 上使用 Sqlite 查看器打开。