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
Asked
Active
Viewed 5,094 times
4
-
AFAIK the iPhone will always sync time to the carrier. Perhaps I'm wrong though. – Eric J. Oct 09 '13 at 17:13
-
2It's an option which can be turned off. – Redwarp Oct 10 '13 at 08:27
1 Answers
8
1) Add to your app entitlements com.apple.timed
key with bool value set to YES
2) Disable automatic time setting (and timezone if you want)
Link to private CoreTime.framework
. Declare these functions
void TMSetAutomaticTimeEnabled(BOOL);
void TMSetAutomaticTimeZoneEnabled(BOOL);
Disable automatic time setting
TMSetAutomatocTimeEnabled(YES);
If you want to know in which state these settings are you can use these functions
BOOL TMIsAutomaticTimeEnabled();
BOOL TMIsAutomaticTimeZoneEnabled();
3) Change current date and time
Declare these
struct timezone
{
int tz_minutewest;
int tz_dsttime;
};
void settimeofday(struct timeval*, struct timezone*);
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
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) Notify about date/time change
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("SignificantTimeChangeNotification"), NULL, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);

creker
- 9,400
- 1
- 30
- 47
-
Hi. I get the following error: "The executable was signed with invalid entitlements". So I guess I need a jailbroken device? – Redwarp Oct 11 '13 at 09:44
-
1
-
4Hi creker, could you please explain how to link to CoreTime framework in Xcode? – Albert Zhang Mar 26 '16 at 06:58