我的应用程序需要触发几个UILocalnotifications,但是这些依赖于系统时间,并且我无法让它们使用示例https://github.com/jbenet/ios-ntp中的NTP
该脚本可以工作,但不能修复UILocalnotifications时间。
例如,这是我的通知脚本( 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];
}发布于 2013-06-17 01:49:18
您可以计算设备日期和网络日期之间的差值:
NSTimeInterval *timeAdjustment = [[NSDate date] timeIntervalSinceDate:[NSDate networkDate]]并相应地调整通知的触发日期:
fireDate = [fireDate dateByAddingTimeInterval:timeAdjustment];https://stackoverflow.com/questions/17134154
复制相似问题