我的应用程序需要触发几个UILocalnotifications,但这取决于系统时间,我无法让他们使用示例中的NTP,https://github.com/jbenet/ios-ntp
该脚本有效,但无法修复UILocalnotifications时间。
E.G。这是我的通知脚本(来自Martin R的更新)
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添加到我的项目中,它加载但不起作用:
#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];
}
答案 0 :(得分:0)
您可以计算设备日期和网络日期之间的差异:
NSTimeInterval *timeAdjustment = [[NSDate date] timeIntervalSinceDate:[NSDate networkDate]]
并相应地调整通知的开火日期:
fireDate = [fireDate dateByAddingTimeInterval:timeAdjustment];