How to check how long ago was the app last opened
Store the current date in UserDefaults whenever the app becomes inactive (using the applicationWillResignActive
app delegate).
Load the stored date (if any) from UserDefaults whenever the app becomes active (using the applicationDidBecomeActive
app delegate). If there is a date (there won't be the first time the app is used), calculate the number of days between the retrieved date and the current date.
See Swift days between two NSDates for methods to calculate the difference between two dates. In short, you use the Calendar dateComponents(_, from:, to:)
method.
How long since last time app was opened in iOS?
Put something like
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"kLastCloseDate"];
in both
- (void)applicationWillTerminate:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
Then check the difference at startup:
NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"kLastCloseDate"];
NSTimeInterval timeDiff = [[NSDate date] timeIntervalSinceDate:lastDate];
// your stuff
in both
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- (void)applicationWillEnterForeground:(UIApplication *)application
How to find out how many days have passed since an iOS app was opened the first time by a specific user/device?
Your app can store it's own unique ID (from a server or randomly generated perhaps) and/or a date-stamp in the iOS device's keychain. The keychain is not cleared when an app is deleted from a device.
See Apple's Keychain services programming guide.
Determining how much time has passed since the app was last used in android?
Yes, the solution via shared preferences is a doable one. You can store the date as a long and then construct the new date later from this long.
android checking if app has been opened multiple days in a row
just store the current Day of year i shared preferences and a counter of days consecutively app opened and compare the saved day in preferences on next run if day saved in shared preferences is 1 less then current day of year increment counter and save the new date and counter in preferences
just use this
SharedPreferences sharedPreferences = getSharedPreferences("YOUR PREF KEY", Context.MODE_PRIVATE);
Calendar c = Calendar.getInstance();
int thisDay = c.get(Calendar.DAY_OF_YEAR); // GET THE CURRENT DAY OF THE YEAR
int lastDay = sharedPreferences.getInt("YOUR DATE PREF KEY", 0); //If we don't have a saved value, use 0.
int counterOfConsecutiveDays = sharedPreferences.getInt("YOUR COUNTER PREF KEY", 0); //If we don't have a saved value, use 0.
if(lastDay == thisDay -1){
// CONSECUTIVE DAYS
counterOfConsecutiveDays = counterOfConsecutiveDays + 1;
sharedPreferences.edit.putInt("YOUR DATE PREF KEY", thisDay);
sharedPreferences.edit.putInt("YOUR COUNTER PREF KEY", counterOfConsecutiveDays).commit;
} else {
sharedPreferences.edit.putInt("YOUR DATE PREF KEY", thisDay);
sharedPreferences.edit.putInt("YOUR COUNTER PREF KEY", 1).commit();
}
Related Topics
Why Is the Tab Bar Disappearing
How to Create a Custom Pop Up View With Swift
How to Hide Google Maps Location Dot
How to Display 3 Cells Per Row in Uicollectionview
Iphone/Ipad: How to Make Uitextfield Readonly (But Not Disabled)
Get Current Scroll Position of Scrollview in React Native
Nsdictionary to Nsdata and Nsdata to Nsdictionary in Swift
Uiimageview - How to Get the File Name of the Image Assigned
How to Get Height of Uitableview When Cells Are Dynamically Sized
How to Prevent Tableview Section Head from Sticking While Scrolling
How to Load Image from Local Path iOS Swift (By Path)
How to Fix iOS 12 Safari Cors Preflight Error
Take Screenshots in the iOS Simulator
How to Show Verification Code Suggestion on Keyboard from Message
Wkwebview Origin Null Is Not Allowed by Access-Control-Allow-Origin
Detect Tap on a Button in Uitableviewcell for Uitableview Containing Multiple Sections