Time difference between set automatically and by user in iOS device - ios

I'll show my question by example.
So, f.e., it's 12:00 (set automatically) now, but user decided to change the current time to 12:10 (it won't be really exact time, but user wants so).
Is there any way to get this 10 minutes in code?

One method is to connect to a time server on your own. Or, if you app is running when it changes, you could keep a timer running and look for discontinuities in time.
Possible duplicate of: How can I locally detect iPhone clock advancement by a user between app runs?

Related

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)

Build a app to remind 6 times a day in different time a whole year?

I am a beginner and would like to make an app for iOS. I do not know where to start but I am not sure whether you can make an app that remembers you every day for a year at different times (I have time to write them in) .. ? Can you make something which can run in the background as the program 'Reminder'?
I will also have it's different in another selected city ..
Thanks
You can use local notification for that.
See this.
You can set time difference as per your requirement.
And even if your app is not running, or is not in background. It will work.
Like this,
local.fireDate = [calendar dateFromComponents:dateComps];
See this link or this , you will get the hint.
you cant run a program in the background all the time in IOS,
you can do 2 things:
you can add reminders to the calendar, so your app wont need to run all the time, only once to add it.
you can use push notifications and just send push to the users every time you want it, so all calculations will be on some php or java or any other server you want.

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