0

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

该脚本有效,但不能修复 UILocalnotifications 时间。

EG 这是我的通知脚本(来自 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];
}
4

1 回答 1

0

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

NSTimeInterval *timeAdjustment = [[NSDate date] timeIntervalSinceDate:[NSDate networkDate]]

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

fireDate = [fireDate dateByAddingTimeInterval:timeAdjustment];
于 2013-06-16T17:49:18.293 回答