My app open_counter resets when the app is force closed

时间:2019-04-16 22:27:01

标签: swift nsuserdefaults

I have a counter to count how many times an app is opened since I want my Google Ads to show up every 5 times an app is opened. It works well except that when the app is force closed, the counter will reset. Anyway to overcome this and save the number of app opens in counter?

Here is my code

let current_open_count = UserDefaults.standard.integer(forKey: "kAppOpenCount")
            if current_open_count == 5 {
                UserDefaults.standard.set(1, forKey: "kAppOpenCount")
            }else{
                UserDefaults.standard.set(current_open_count + 1, forKey: "kAppOpenCount")
            }
        } else {
            //Do nothing
        }

    }

0 个答案:

没有答案