From 0c92bd1151dce581b335976b463feb7686339707 Mon Sep 17 00:00:00 2001 From: Javier Cala Uribe Date: Thu, 30 Jul 2020 20:32:01 -0500 Subject: [PATCH] Add skip backup setting to iCloud --- Prey/Classes/AppDelegate.swift | 39 ++++++++++++++++++++++++++++++++++ Prey/Classes/PreyConfig.swift | 4 ++++ 2 files changed, 43 insertions(+) diff --git a/Prey/Classes/AppDelegate.swift b/Prey/Classes/AppDelegate.swift index 254a059f..c84eedb5 100644 --- a/Prey/Classes/AppDelegate.swift +++ b/Prey/Classes/AppDelegate.swift @@ -62,6 +62,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { GAI.sharedInstance().logger.logLevel = GAILogLevel.none GAI.sharedInstance().defaultTracker.allowIDFACollection = true + // Check settings info + checkSettingsToBackup() + // Update current localUserSettings with preview versions PreyConfig.sharedInstance.updateUserSettings() @@ -272,4 +275,40 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } } + // MARK: Check settings on backup + + // check settings + func checkSettingsToBackup() { + let fileManager = FileManager.default + let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask) + let docURL = urls[urls.endIndex-1] + let storeURL = docURL.appendingPathComponent("skpBckp") + + if !fileManager.fileExists(atPath: storeURL.path) { + // Not exist skipBackup file + // Check if app was restored from iCloud + if PreyConfig.sharedInstance.isRegistered && PreyConfig.sharedInstance.existBackup { + PreyConfig.sharedInstance.resetValues() + } + fileManager.createFile(atPath: storeURL.path, contents: nil, attributes: nil) + _ = self.addSkipBackupAttributeToItemAtURL(filePath: storeURL.path) + PreyConfig.sharedInstance.existBackup = true + PreyConfig.sharedInstance.saveValues() + } + } + + // Add skip backup + func addSkipBackupAttributeToItemAtURL(filePath:String) -> Bool { + let URL:NSURL = NSURL.fileURL(withPath: filePath) as NSURL + assert(FileManager.default.fileExists(atPath: filePath), "File \(filePath) does not exist") + var success: Bool + do { + try URL.setResourceValue(true, forKey:URLResourceKey.isExcludedFromBackupKey) + success = true + } catch let error as NSError { + success = false + PreyLogger("Error: \(error) excluding from backup"); + } + return success + } } diff --git a/Prey/Classes/PreyConfig.swift b/Prey/Classes/PreyConfig.swift index ada883ed..4a595fed 100644 --- a/Prey/Classes/PreyConfig.swift +++ b/Prey/Classes/PreyConfig.swift @@ -40,6 +40,7 @@ enum PreyConfigDevice: String { case NeedChangeIcon case IsTouchIDEnabled case ValidationUserEmail + case ExistBackup } enum PreyUserEmailValidation: String { @@ -70,6 +71,7 @@ class PreyConfig: NSObject, UIActionSheetDelegate { isTouchIDEnabled = defaultConfig.bool(forKey: PreyConfigDevice.IsTouchIDEnabled.rawValue) reportOptions = defaultConfig.object(forKey: PreyConfigDevice.ReportOptions.rawValue) as? NSDictionary validationUserEmail = defaultConfig.string(forKey: PreyConfigDevice.ValidationUserEmail.rawValue) + existBackup = defaultConfig.bool(forKey: PreyConfigDevice.ExistBackup.rawValue) } // MARK: Properties @@ -90,6 +92,7 @@ class PreyConfig: NSObject, UIActionSheetDelegate { var isTouchIDEnabled : Bool var reportOptions : NSDictionary? var validationUserEmail : String? + var existBackup : Bool // MARK: Functions @@ -113,6 +116,7 @@ class PreyConfig: NSObject, UIActionSheetDelegate { defaultConfig.set(isTouchIDEnabled, forKey:PreyConfigDevice.IsTouchIDEnabled.rawValue) defaultConfig.set(reportOptions, forKey:PreyConfigDevice.ReportOptions.rawValue) defaultConfig.set(validationUserEmail, forKey:PreyConfigDevice.ValidationUserEmail.rawValue) + defaultConfig.set(existBackup, forKey: PreyConfigDevice.ExistBackup.rawValue) } // Reset values on NSUserDefaults