iOS 10.3.2, , . , "applicationWillTerminate" . .
, , , plist . .
Swift 3.0.2:
func applicationWillTerminate(_ application: UIApplication) {
let plistPath = getPath()
if !FileManager.default.fileExists(atPath: plistPath) {
FileManager.default.createFile(atPath: plistPath, contents: nil, attributes: nil)
}
let data:NSMutableDictionary = [:]
data.setValue("test_Value", forKey: "test_key")
data.write(toFile: plistPath, atomically: true)
}
func getPath() -> String {
let plistFileName = "data.plist"
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentPath = paths[0] as NSString
let plistPath = documentPath.appendingPathComponent(plistFileName)
return plistPath
}
func displaySavedData() {
let plistPath = self.getPath()
if FileManager.default.fileExists(atPath: plistPath) {
if let savedData = NSMutableDictionary(contentsOfFile: plistPath) {
print(savedData)
}
}
}
When the application is running, values may be available, as shown below:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
displaySavedData()
return true
}
As described in other answers, “applicationWillTerminate” will not be called if the application supports background mode, so this solution is useful when your application does not support background mode, and you know that killing the application causes “applicationWillTerminate”.
source
share