改变iPhone的日期和时间 [英] Changing iPhone's date and time
问题描述
我正在玩iOS SDK,我正在尝试以编程方式更改设备的日期和时间。
我读到标准SDK是不可能的,这是有道理的,但我想知道是否可以使用私有API这样做,因为我只是做一个应用程序作为一项研究,而不是打算在App Store上发布它
I'm toying with iOS SDK, and I'm trying to change the device's date and time programmatically. I read that it's not possible with the standard SDK, which makes sense, but I'm wondering if it's possible to do so with the private APIs, as I'm just doing an app as a study, and do not intend to publish it on the App Store
推荐答案
1)添加到您的应用程序权利 com.apple。定时
键,bool值设置为 YES
1) Add to your app entitlements com.apple.timed
key with bool value set to YES
2)禁用自动时间设置(和时区如果你想)
2) Disable automatic time setting (and timezone if you want)
链接到私人 CoreTime.framework
。声明这些函数
Link to private CoreTime.framework
. Declare these functions
void TMSetAutomaticTimeEnabled(BOOL);
void TMSetAutomaticTimeZoneEnabled(BOOL);
禁用自动时间设置
TMSetAutomatocTimeEnabled(YES);
如果你想知道这些设置在哪种状态下你可以使用这些功能
If you want to know in which state these settings are you can use these functions
BOOL TMIsAutomaticTimeEnabled();
BOOL TMIsAutomaticTimeZoneEnabled();
3)更改当前日期和时间
3) Change current date and time
声明这些
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
This API is public in OS X so you can find documentation here https://developer.apple.com/library/ios/documentation/System/Conceptual/ManPages_iPhoneOS/man2/settimeofday.2.html
作为示例,我们将时钟设置为10分钟
As an example we will set clock 10 minutes forward
struct timeval tv;
tv.tv_sec = [[NSDate dateWithTimeIntervalSinceNow:600] timeIntervalSince1970];
tv.tv_usec = 0;
settimeofday(&tv, NULL);
4)通知日期/时间变化
4) Notify about date/time change
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("SignificantTimeChangeNotification"), NULL, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
这篇关于改变iPhone的日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!