iPhone后台模式下的内部时钟 [英] Internal clock in iPhone background mode

查看:166
本文介绍了iPhone后台模式下的内部时钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我希望在后台模式下运行内部时钟[当应用程序未在前台运行时]。

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

整个功能将如下所示:

目标是让服务器在应用程序中使用时间,因为使用设备时间有时可能会导致问题。问题可能出现在有人改变了用户的iPhone时间等情况。所以我遵循以下方法。

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.

- 在我的应用程序背景中运行内部时钟即使应用程序未运行。
- 每隔15分钟与服务器通信以获得实时并运行计时器。
- 如果net在两者之间断开连接,计时器将继续并占用计时器时间。

-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?

我正在开发一个涉及票务预订系统的iPhone应用程序。我将我的应用程序注册为基于位置的,因为它正在使用用户在后台拍摄的位置。

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?

我需要在我的应用中获得正确的时间,以便我运行这个内部时钟。我可以使用NSDate,但这将返回设备时间。任何人都可以更改设备时间。因此,一旦有人唠叨,错误的时间将影响应用程序的顺利运行。有些人建议在没有运行内部时钟的情况下获得正确的时间吗?

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 ?

推荐答案

更新:很抱歉,我的原始答案不正确。当设备进入休眠状态(可能在锁定后的某个时间发生),内部CPU时钟停止滴答, mach_absolute_time 也不会更新。从理论上讲,如果你在设备进入睡眠状态之前调用它,并且在它唤醒之后,它将返回完全相同的值。

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.

我知道检查的最佳可行方式日期更改为 kern.boottime ,它保留启动时间,只要系统时间发生变化,就会被修改。除其他外,如果用户更改时间,或者操作系统根据来自单元塔的信息更改时间本身,将更新kern.boottime。

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.

因此,在您的情况下,您可以采用您计算的原始时间并根据kern.boottime中的修改进行修改。如果您看到kern.boottime发生了显着变化,则可能意味着该设备已关闭,在这种情况下,您需要联系服务器询问其直到航班的时间。

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.

相关代码:

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;
}

原始(不正确)答案:您可以使用 mach_absolute_time ,不受用户日期更改的影响。

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

预订机票时,从服务器获取正确的日期并记录 mach_absolute_time 。现在,您可以随时调用 mach_absolute_time ,计算与您最初记录的差异,并显示正确的日期。

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.

这篇关于iPhone后台模式下的内部时钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆