首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带NTP/时间服务器的UILocalnotification

带NTP/时间服务器的UILocalnotification
EN

Stack Overflow用户
提问于 2013-06-16 22:08:45
回答 1查看 176关注 0票数 0

我的应用程序需要触发几个UILocalnotifications,但是这些依赖于系统时间,并且我无法让它们使用示例https://github.com/jbenet/ios-ntp中的NTP

该脚本可以工作,但不能修复UILocalnotifications时间。

例如,这是我的通知脚本( Martin R.更新)

代码语言:javascript
复制
NSTimeInterval timeAdjustment = [[NSDate date] timeIntervalSinceDate:[NSDate networkDate]];

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setHour:15];
[comps setMinute:58];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *fireDate = [gregorian dateFromComponents:comps];

UILocalNotification *alarm = [[UILocalNotification alloc] init];

alarm.fireDate = [fireDate dateByAddingTimeInterval:timeAdjustment];
alarm.repeatInterval = NSDayCalendarUnit;
alarm.soundName = @"default";
alarm.alertBody = @"NTP Test..";
alarm.timeZone = [NSTimeZone defaultTimeZone];

通知应该在15.58发出,实时时间是15.00,但是用户想要在15.01显示通知,所以他将设备时间“黑”到15.57,这样设备就会立即显示通知。我如何防止这种情况,因为这是我的应用程序工作的必要条件。我尝试将NTP添加到我的项目中,它加载了,但不起作用:

代码语言:javascript
复制
#import "AsyncUdpSocket.h"
#import "lib/ios-ntp.h"

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    [NSDate networkDate];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-17 01:49:18

您可以计算设备日期和网络日期之间的差值:

代码语言:javascript
复制
NSTimeInterval *timeAdjustment = [[NSDate date] timeIntervalSinceDate:[NSDate networkDate]]

并相应地调整通知的触发日期:

代码语言:javascript
复制
fireDate = [fireDate dateByAddingTimeInterval:timeAdjustment];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17134154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档