1) 将 bool 值设置为 YES 的 com.apple.timed 键添加到您的应用权利
2) 禁用自动时间设置(如果需要,还可以设置时区)
链接到私人CoreTime.framework。声明这些函数
void TMSetAutomaticTimeEnabled(BOOL);
void TMSetAutomaticTimeZoneEnabled(BOOL);
禁用自动时间设置
TMSetAutomatocTimeEnabled(YES);
如果您想知道这些设置处于哪个状态,您可以使用这些功能
BOOL TMIsAutomaticTimeEnabled();
BOOL TMIsAutomaticTimeZoneEnabled();
3) 更改当前日期和时间
声明这些
struct timezone
{
int tz_minutewest;
int tz_dsttime;
};
void settimeofday(struct timeval*, struct timezone*);
这个 API 在 OS X 中是公开的,所以你可以在这里找到文档https://developer.apple.com/library/ios/documentation/System/Conceptual/ManPages_iPhoneOS/man2/settimeofday.2.html
例如,我们将时钟拨快 10 分钟
struct timeval tv;
tv.tv_sec = [[NSDate dateWithTimeIntervalSinceNow:600] timeIntervalSince1970];
tv.tv_usec = 0;
settimeofday(&tv, NULL);
4) 通知日期/时间更改
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("SignificantTimeChangeNotification"), NULL, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);