I already know how to save things and I can automatically load them when opening the app. But I have no idea how to save them as it closes! I have a button for now. I am trying to save just one value, StreakNumber
. However, whenever I try, I fail.
When in app delegate I try saying ViewController.AppSave(ViewController)
. I get an error:
Editor placeholder in source file Expression resolves to an unused function
and if I use ViewController.AppSave()
I get the following error:
Instance member 'SaveApp' cannot be used on type 'ViewController'; did you mean to use a value of this type instead?
import UIKit
import UserNotifications
class ViewController: UIViewController {
//TEST PLAY AREA//
@IBAction func SaveButton(_ sender: Any) {
SaveApp()
}
@IBAction func LoadButton(_ sender: Any) {
LoadApp()
}
///TEST PLAY AREA ENDS
public var streakNumber = 0
public var streakNumberString = ""
public var activated = false
let defaults = UserDefaults.standard
//Labels
@IBOutlet var streakNumberLabel: UILabel!
@IBOutlet var remainingTimeTextLabel: UILabel!
@IBOutlet var remainingTimeNumberLabel: UILabel!
//Buttons
@IBAction func startButton(_ sender: Any) {
Activate()
}
@IBAction func stopButton(_ sender: Any) {
Deactivate()
seconds = 59
minutes = 59
hours = 47
}
//Timer
var timer = Timer()
var seconds = 0
var minutes = 0
var hours = 0
// Nitifications
//END
override func viewDidLoad() {
super.viewDidLoad()
LoadApp()
// this enables notifications
UNUserNotificationCenter.current().requestAuthorization(options:[.alert , .sound , .badge], completionHandler:{ didAllow, error in
})
}
func Activate (){
if activated == false {
streakNumber += 1
}
activated = true
streakNumberLabel.text = String(streakNumber)
}
func Deactivate (){
if activated == true{
activated = false
ActivTimer()
// Notification
let content = UNMutableNotificationContent()
content.title = " 10 secodns are up "
content.subtitle = " yep time passed"
content.body = " The 10 seconds are really up!!"
content.badge = 1
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 600, repeats:false)
let request = UNNotificationRequest(identifier: "timerDone", content: content , trigger: trigger)
UNUserNotificationCenter.current().add(request,withCompletionHandler:nil)
}
}
@objc func Clock(){
seconds = seconds-1
if seconds == -1{
seconds = 59
minutes = minutes-1
}
if minutes == -1{
minutes = 59
hours = hours-1
}
remainingTimeNumberLabel.text = (String(hours) + ":" + String(minutes) + ":" + String(seconds))
if seconds == 0 && hours == 0 && minutes == 0 {
timer.invalidate()
}
}
func ActivTimer(){
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.Clock), userInfo: nil, repeats: true)
}//SAVING STUFF DOESNT WORK
//
func SaveApp(){
streakNumberString = String(streakNumber)
defaults.set(streakNumberString, forKey: "streakNumbers")
print("Saved" + String(defaults.integer(forKey: "streakNumbers")))
}
func LoadApp(){
streakNumberString = defaults.string(forKey: "streakNumbers")!
print (defaults.integer(forKey: "streakNumbers") )
streakNumber = Int(streakNumberString)!
streakNumberLabel.text = String(streakNumber)
}//
}