Library Backup is now done through icloud

This commit is contained in:
2021-05-20 14:36:03 +02:00
parent 64e4e52b5e
commit d8975a4bd3
4 changed files with 42 additions and 12 deletions

View File

@@ -23,6 +23,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
#endif
print("disFinishLaunchung")
ICloudManager.shared.preload_all_data()
return true
}
}

View File

@@ -14,6 +14,18 @@ class ICloudManager {
static let relative_seperator : [String] = ["iCloud~Zockerhoehle", "Documents"]
static var backup_folder : URL? {
get {
return documents_folder?.appendingPathComponent("backup")
}
}
static var documents_folder : URL? {
get {
return FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")
}
}
static public func relativePathFrom(url : URL) -> String? {
return url.relative_path_after(pathComponent: relative_seperator) ?? .none
}
@@ -21,7 +33,7 @@ class ICloudManager {
static public func fileExists(at path : String?) -> Bool {
guard let path = path else { return false }
if let url = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents").appendingPathComponent(path) {
if let url = documents_folder?.appendingPathComponent(path) {
return FileManager.default.fileExists(atPath: url.path)
}
@@ -32,7 +44,7 @@ class ICloudManager {
guard let path = path else { return .none }
do {
let url = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents").appendingPathComponent(path)
let url = documents_folder?.appendingPathComponent(path)
let imageData = try Data(contentsOf: url!)
return UIImage(data: imageData)!
@@ -53,6 +65,19 @@ class ICloudManager {
return false
}
public func preload_all_data() -> Bool {
guard let docs_url = ICloudManager.documents_folder else { return false }
do {
try FileManager.default.startDownloadingUbiquitousItem(at: docs_url)
return true
}catch{
return false
}
}
private init() {
}

View File

@@ -60,12 +60,14 @@ struct LibraryExporter : Encodable {
print("Exported Library to '\(exportFileName)' (Size: \(fileSizeAsString))")
do {
let documentDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
if let documentDirectory = ICloudManager.backup_folder {
let encodedFileURL = documentDirectory.appendingPathComponent(exportFileName)
print("WRITE TO \(encodedFileURL)")
try data.write(to: encodedFileURL)
}catch {
}
}catch let error {
print("Export failed. Error \(error)")
}
}
}

View File

@@ -25,7 +25,7 @@ class LibraryImport {
}
func backupFiles() -> [String] {
do {
let documentDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
guard let documentDirectory = try ICloudManager.backup_folder else { return [] }
let filesInDocumentsDir = try FileManager.default.contentsOfDirectory(at: documentDirectory, includingPropertiesForKeys: .none, options: [])
@@ -135,13 +135,13 @@ class LibraryImport {
func importLIB(from filename: String) {
do {
let documentDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
let file = documentDirectory.appendingPathComponent(filename)
guard let file = ICloudManager.backup_folder?.appendingPathComponent(filename) else {
print("LibraryImport:importLIB Could not get icloud backup folder")
return
}
if file.isFileURL {
let library = try JSONDecoder().decode(BHLibrary.self, from: Data(contentsOf: file))
resetDatabase()
var gameDict = [UUID:Game]()