Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if the device datesettings is automatic in iOS

Tags:

ios

swift

Is there a way to check if the device settings for "date and time" is set to automatic or not in Swift? And if there is, is there possible to make the User to switch to the desired settings (automatic)

like image 735
MonkeyBizz Avatar asked May 29 '15 12:05

MonkeyBizz


2 Answers

The way iOS is structured, your app lives in a sandbox and can only communicate with the system if there is a public API that Apple has shared with the developer community. Some public APIs can be used freely like the accelerometer but others require user permission like the location information. Unfortunately the date and time settings apis are not public and so you can't play with the date and time settings at all.

There are many posts about getting the time in a user tamper proof way. The bottom line is that there is no real 100% tamper proof solution but the following code is already a good step for protecting from most users.

#import <CoreLocation/CoreLocation.h>

CLLocation* gps = [[CLLocation alloc]
                   initWithLatitude:(CLLocationDegrees) 0.0
                   longitude:(CLLocationDegrees) 0.0];
NSDate* now = gps.timestamp;

Many other resources but take a look at these two:

  • http://www.earthtools.org/webservices.htm#timezone
  • http://www.timeapi.org/utc/now
like image 64
Mika Avatar answered Nov 09 '22 05:11

Mika


You can use this tool https://github.com/instacart/TrueTime.swift to get a synchronized time.

If you really want to ask the user, then you can compare the synchronized date to current date Date(). If the date is very different then you can ask the user to check settings.

like image 27
Ted Avatar answered Nov 09 '22 07:11

Ted