Looking to automatically time out users on Firebase but having some difficulty - ios

I've got an iPhone app where you create an account, and while you're logged in, you can be either active or inactive. To be clear, even when you're inactive, you're still logged in. I want to make it so that if a user is active but hasn't checked the app or used it for 45 minutes, they are automatically made inactive. Is that possible? If so, how would I go about it? I'm not trying to force a log out, but rather change their state from active to inactive. so in firebase, for each user, it has a variable for isActive. I'm trying to make it so that turns from 1 to 0 after a certain amount of idle time.
Thank you!

This can be done by leveraging a Cron Job.
There are two things that will need to happen.
You'll need determine what mechanics indicate a user is active or not. For example, every time the users taps a Search button, that timestamp is written to Firebase. As long as the difference between that time and the current time is less than 45 minutes, take no action.
A cron job to trigger code* that compares a current time stamp to the written time stamp and if greater than 45 minutes, set's the user to inactive. (actually just querying for all users where current time - posted time > 45 minutes, and set them all to inactive)
*code could be integrated with Firebase Cloud Functions, or your app could be observing a node that's set as well.
The app could also trigger this sequence but if it goes offline the active/inactive node would not be set or cleared. However, there is an onDisconnect Firebase function you may want to look into as well
We have a little app we've used to automatically log an employee out of an account if they go afk, and this design pattern works well.

Make a timer variable and make it work. Attach a mousemove event to reset the timer not stopping the timer. If the time elapsed is greater than your desired limit call the signOut method in firebase.
why mousemove?
To notice the inactivity.
Actually I am a web developer most of the time dealing with javascript. I don't know which programming language you are using so use similar method as setTimeOut in javascript.

Related

How would I reset a variable every week on specific time when the App is closed?

I created a scene to present weekly goals. I count up a variable each second a user is using my App and save it to the UserDefaults. How can I reset(Set to 0) each Monday at 6PM in order to reset the 'weekly' goal automatically in the background whether the App is terminated or not? I would also include a Usernotification. Can I even execute code within the Usernotification? I'm using SWIFT.
Thanks for any help!
If the app is not running, it isn't running, and nothing can guarantee that it will do something at a certain time except sending a Push Notification from your server at that time.
However, it would be better to revise your goals. Keep in mind that if the user sees your app at all, the user is using your app. So you can always detect the situation and change whatever needs to be changed whenever the user foregrounds/launches the app. The effect will be exactly the same.
It may work.
When users begin any event (e.g., open your app, sign in, or click a button), get the current date and save the date to a variable. Then put some code to compare the date in the variable with a current time in each Viewcontroller. When the current date is different from the date in the variable, (a) update the variable with the current date and (b) activate the codes that we want to execute using "if" statement. It works for me.

iOS how to detect user has changed system time, and if not compare device time with server time

The requirement is that have to detect if the system time of iPhone is changed by user manually. For that I have used
NSNotificationCenter.defaultCenter().addObserver(self, selector: "timeChangedNotification:", name: NSSystemClockDidChangeNotification, object: nil)
and
func timeChangedNotification(notification:NSNotification){
println("Device time has been changed...")
}
But not getting the results.
Also if this time is not changed means it is automatic time provided by network then have to compare with server time.How can this be done? Please give help for the same.
As per its documentation, NSSystemClockDidChangeNotification is:
Posted whenever the system clock is changed. This can be initiated by a call to settimeofday() or the user changing values in the Date and Time Preference panel.
So unless you were expecting the user to change the date or time while your app is running, you're obviously not going to hear anything. Notifications are triggered by events.
iOS doesn't provide you with a way to ask on whose authority the system believes the time and date is the current value — whether it's carrier network time, time server time, user-supplied time or something else. There's no single server time and no way to ask for it. What you want to do cannot be done.
This code worked for me in Swift 3
NotificationCenter.default.addObserver(self, selector: #selector(ClassName.timeChangedNotification), name: NSNotification.Name.NSSystemClockDidChange, object: nil)
}
// Method get called user changed the system time manually.
func timeChangedNotification(notification:NSNotification){
print("Device time has been changed...")
}
I've tried 3 options to do this for an enterprise app that I did that requires the time to always be in sync across users. Since we have no control over the users' setting of the device time, I was also face with your predicament:
Option 1 - Using GPS time: I tried getting the GPS timestamp and comparing it with the local device time. It worked in some countries but did not work in others. Other countries return the device time for some reason.
Option 2 - Using a background process to estimate the approximate time. What you do is to get the timestamp, whenever your app goes to the background. Run a timer that fires every 5 minutes (+/- depending on your tolerance). Then calculate the estimated time by adding the 5min ticks that went by to the stored timestamp when your app becomes active. However, this approach can only detect if the time was changed while your app is already running. And not if the time was changed before your app even started.
Option 3 - Using a time-check at the backend. And this is what I ended up doing. I have a small php backend program that returns the UTC time. So every time my app is executed or returns to the foreground, my app will quickly check the time against my time-server. To handle offline cases, I store the last timestamp before the system goes to the background so you can at least check if the date/time was back-dated. Then check again with the time-server when the system goes online.
So far it's the third option that worked for me. But I guess combining 2 and 3 would even make the system more pull-proof.
It's been a while but it might be useful for anyone who will find this question in future like I did today.
You also could use NTP protocol to know real epoch time at the moment when your app enters background / foreground or in didFinishLaunching. This requires the app to be online, though. But it could help in most cases.
There are at least two open-source libraries for iOS and for Android that can help: TrueTime and Kronos.

Xcode - Run code each day on 00:00

I need a piece of code for Xcode (obj-c) that runs every day at 00:00.
The reason for this is because my server updates data each new day and I would like my app to synchronize with the update.
I was thinking about doing a timer that runs every minute and checks the time (from internet, not the phones time), but it seems like there could be an more efficient way.
Help or pointers really appreciated!
Thanks
Use the calendar app on your mac. Set up a recurring event every day at 0:00. At the point where you pick the type of alert, you can select an option to open a file (as opposed to a pop-up, etc.). Pick your app as the file to be opened.
When the app becomes active, register a local notification (UILocalNotification) to trigger at midnight.
Then, you can perform the updates at the app delegate's -application:didReceiveLocalNotification:
You can then cancel the notification when the application goes to background by calling the UIApplication's method -cancelLocalNotification:
I don't think it's even required to use notification here. Notification is usually on best effort basis and doesn't guarantee the delivery. This can be done in few simple steps.
Save the lastUpdated time in your iOS app. Probably in NSUserDefaults or if you're using local database you can store it there as well.
Everytime user launches the application check how much time has elapsed since last update.
If it's more than 24 hours then make an API call to your server and check if there's any new updates available (which you can determine probably from server timestamp). Get the latest data (possibly only the delta difference in stead of everything).
Update the lastUpdated time in your iOS app.

notice on Date and time setting change

In an iOS app, is there any method to get notified either user changed the date or time setting.
Actually in an app we want to know the time difference between the two app sessions. It can be done by saving the time when user closed the app and when user restart again. But what if user change the time setting in between.
Can we get noticed at the starting of the game whether user changed the time/date setting.
Any suggestions would be of great help.
UIApplicationSignificantTimeChangeNotification is what you're looking for. But there's another case where date may change, for example when user changes time zone. In this case you need to observe NSSystemTimeZoneDidChangeNotification.
I believe that this is what applicationSignificantTimeChange: (on the app delegate) and the UIApplicationSignificantTimeChangeNotification notification is used for (though the case where the user manually changes their time is not listed as an example)

ios How to run method on every day?

I want to check for update of the stored db every day or every X interval of day, how is it possible to call a method on every X interval of day? I found some code that is for the x interval of seconds, is it feasible to do by the same way? And main thing is it should run in background too. I also though of the way by storing the last updated date in database so that I retrive every time app lunch and compare with present date? Please help me by suggesting the better way.
Since you can't really run any app in background unless you are playing audio, doing something thing with the location or a re an voip client, you will need to do this when the user opens your app.
Just store the last update date in the NSUserDefault
You can not execute your application when it is closed. You can not create background applications or daemons.
Maybe running it on application start would be appropriate for you, but if you need information from the device every day, the user will have to make sure the app is open every day.

Resources