Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal clock in iPhone background mode

Tags:

xcode

ios

iphone

In my application i want to run an internal clock in background mode [while application is not running in foreground].

The whole functionality will be like this:

The objective is to get the server time to use in the application, because using device time may sometimes cause issues. The issues may be in situations like somebody has changed the user's iPhone time etc. So i am following the below method.

-Running an internal clock in my application background even if the application is not running. -Communicate with server every 15 minutes to get the real time and run a timer. -If net is disconnected in between,timer will continue and take the timer time.

My application is heavily depended on this time factor as this is a ticket booking system.Kindly help me to implement this or please confirm whether this is possible or not?

I am developing an iPhone application which involves Ticket Booking System. I registered my application as location based beacuse it is using user's location taken in background for a purpose.

My problem is that i need to run an internal clock in my application in background mode. I need to write the codes for internal clock in core location delegate methods, so that internal clock will also run along with the location bsed services. Will my app get rejected? Is anything wrong in doing like this?

I need to get the correct time to use in my app, so that i am running this internal clock. I can use NSDate, but that will return the device time. Anyone can change the device time. So once somebody chaged, wrong time will affect the smooth functioning of the app. Kindly some body suggest to get the correct time with out running the internal clock ?

like image 668
Praveen Avatar asked Dec 01 '22 06:12

Praveen


2 Answers

Update: Sorry to say my original answer isn't correct. When the device goes to sleep (can happen sometime after it's locked) the internal CPU clock stops ticking, and mach_absolute_time won't update either. Theoretically it will return the exact same value if you call it right before the device went to sleep, and after it awakes.

The best available way I know of to check for date changes is kern.boottime, it holds the boot time, and is modified whenever the system time changes. Among other things, kern.boottime will be updated if the user changes the time, or if the OS changes the time itself according to info from cell towers.

So, in your case, you can take the original time you calculated and modify it according to the modifications in kern.boottime. If you see kern.boottime changed significantly, it may mean that the device was turned off, and in this case you will need to contact the server to ask it for the time until the flight.

Relevant code:

time_t getBootTimeSecs(void)
{
    struct timeval boottime;    
    size_t size = sizeof(boottime);
    int ret = sysctlbyname("kern.boottime", &boottime, &size, NULL, 0);
    assert(ret == 0);
    return boottime.tv_sec;
}

Original (incorrect) answer: You can use mach_absolute_time which isn't affected by date changes made by the user.

When you book the ticket, get the correct date from the server and record mach_absolute_time. Now you will always be able to call mach_absolute_time whenever you want, calculate the difference from the one you recorded initially, and display the correct date.

This will only work as long as the device wasn't shut down, in that case it makes sense for the app to re-connect to the server to get the correct date.

You can also either Local or Push Notifications to alert the user when the target date is getting closer, even if the app isn't running.

like image 130
Danra Avatar answered Dec 02 '22 18:12

Danra


apple is support small task for background mode which will work for approximate 10 sec only.

so you can do one thing when app is active then get time form server and update your local time according that.

like image 37
priyanka Avatar answered Dec 02 '22 19:12

priyanka