To know date and time settings in device from an i

2020-03-27 08:13发布

问题:

I want to know whether the user has set their device to automatically set the date and time in the Settings.app in iPhone. I need this information because I want to make user to access some informations in the app only for a day, which he can access again only on second day. But an smart user can change the date and access the information. So I want to check whether they set the date/time automatically for the device or not.

Is there any way for doing so. Please suggest any other alternate way if exist. Thanks in advance.

回答1:

I don't believe there exists a way to detect the user's preference regarding the automatic update of the date time settings. To determine the length of time from a given point, you have a few options none of which are absolute or without any error.

  1. Use NSDate, which you already know can be circumvented by manually changing the date on the device.
  2. Use a time server. By querying a time server you can get an independent date time value. Store that value and compare against it to determine if the right amount of time has passed. The drawback of course, is this requires an internet connection to function property.
  3. Use a system clock value or series of values to roughly calculate the time elapsed. A functions like CACurrentMediaTime or mach_absolute_time() can provide a count to compare against. You can take the values and increment them until you have reached a specified duration to reset a flag for your second day check.

None of the options alone will provide an ideal solution, but by combining the approaches, you might achieve an acceptable level of assurance the user is accessing the information only during the time allowed.



回答2:

This is an alternate suggestion, I had similar problem for a conference app I was developing. I wanted to show some data to user only in the second day of conference.

After discussing getting date option from device with our team. We decided not to use this approach because as you said some user may change date settings and also some user may be reluctantly set their device to other countries time and date zones where there may be date conflict issues for users.

Finally we decided to create a simple web page and get http 200 and http 404 request for that info. You dont have to put your data or whatever to the server, we just wanted to get the response code from web.

So lets say you have a webpage http://xxxxxxxxxxxxxx.pdf or .html we dont put any pdf files to server so user always get http 404 response in that case we disable or hide the related data/button/row whatever. In the second day of conference we put a dummy page so now user can get http 200 response, so we enable button/row/data.

if you decide to use this approach download block for nsurlconnection from here

then use following code:

NSURL *theFileURL=[NSURL URLWithString:@"yoururl"];
 NSURLRequest *request = [NSURLRequest requestWithURL:theFileURL];

    [URLConnection asyncConnectionWithRequest:request
                              completionBlock:^(NSData *data, NSURLResponse *response) {
                                  //get data and response
                                  if ([response isKindOfClass:[NSHTTPURLResponse class]])
                                  {
                                      NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
                                      //NSString *fileMIMEType = [[httpResponse MIMEType] lowercaseString];
                                      NSLog(@"httpResponse.statusCode %i",httpResponse.statusCode);
                                      if (httpResponse.statusCode==200) {
                                          //correct respond show data

                                      }
                                      else if (httpResponse.statusCode==404)
                                      {
                                          //no file this
                                          NSLog(@"no file at the server");

                                      }

                                  }
                                  //[HUD removeFromSuperview];


                              } errorBlock:^(NSError *error) {
                                  //get error

                                  NSLog(@"error %@",error);
                              } uploadPorgressBlock:^(float progress) {
                                  //Upload progress (0..1)
                              } downloadProgressBlock:^(float progress) {
                                  //Download progress (0.1)
                                  //progress += 0.01;
                                  //HUD.progress = progress;
                              }];