gpt4 book ai didi

ios - 更改 iPhone 的日期和时间

转载 作者:可可西里 更新时间:2023-11-01 05:22:04 40 4
gpt4 key购买 nike

我正在玩弄 iOS SDK,我正在尝试以编程方式更改设备的日期和时间。我读到标准 SDK 是不可能的,这是有道理的,但我想知道是否可以使用私有(private) API 这样做,因为我只是在研究一个应用程序,并不打算发布它在 App Store 上

最佳答案

1) 添加到您的应用授权 com.apple.timed 键, bool 值设置为 YES

2) 禁用自动时间设置(如果需要,还可以禁用时区)

链接到私有(private) 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);

关于ios - 更改 iPhone 的日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19278450/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com