Find out if smartphone (Android and iOS) has Automatic Time Zone enabled using ngCordova

Asked

Viewed 295 times

0

An Ionic v1 app needs to know if the smartphone (Android and iOS) has Automatic Time Zone enabled, I imagine this is done using ngCordova, how can I do this?

For some actions in the app, I need to ensure that the user is with Automatic Time Zone enabled, so that they do not send data with false date/ time, if they are not able to proceed.

  • 1

    After your edition I understood more your need, so I added a few more things in the answer, with a "theoretical" example, just to understand the idea.

2 answers

0

I think finding out if this enabled will not be possible (I’m not sure, maybe there is some compiled plugin that solves, so far I found nothing), but can try to compare the value of "API Date javascript native:

And create a page with HTTP response in any format, for example JSON, that returns the time of your server, being this page configured with the desired Timezone.

For example to get the Timezone of the mobile would be this:

let tz = new Date().getTimezoneOffset();

console.log(tz);

Or take the current time and compare with some API service (own or third party):

console.log(new Date().toString());

And with an HTTP request using $http could get the data from the server and compare them, which will require some development in the back end, however as an alternative there are third party services that return this type of data, as the own of google maps: https://developers.google.com/maps/documentation/timezone/intro

Then you could send the requisition to $http for:

https://maps.googleapis.com/maps/api/timezone/json?location=<LATITUDE>,<LONGITUDE>&timestamp=<TEMPO ATUAL EM SEGUNDOS>&key=<SUA CHAVE DA CONTA DE DESENVOLVEDOR GOOGLE>

You’ll have to use something to pick up the user’s current location, so change:

  • <LATITUDE> by the caught longitude
  • <LONGITUDE> by the caught longitude
  • <TEMPO ATUAL EM SEGUNDOS> for Date.now()
  • <SUA CHAVE DA CONTA DE DESENVOLVEDOR GOOGLE> your developer key, you must have developer account to use Google Maps services.

Another possible solution would be to use the service https://timezonedb.com/api (there are others of the same type), in the case Would use this function: https://timezonedb.com/references/get-time-zone


For some actions in the app, I need to ensure that the user is with Automatic Time Zone enabled, so that they do not send data with false date/ time, if they are not able to proceed.

In this case simply send the request time next to the payload, something like:

$http({
    url: 'http://foo.bar/api/foo/bar',
    method: 'POST',
    data: {
        'foo': $scope.foo,
        'bar': $scope.bar,
        'time': new Date().toUTCString()
    }
}).then(function (response) {
   console.log("sucesso", response);
}, function (response) {
   console.log("falhou", response);
});

Of course take into account that Brazil has different time zones, so it would be interesting to send in UTC to do from this the back-end check, so in your HTTP server, where will communicate with your service, check the parameter time payload, in your case I think it is PHP, so I would do this:

strtotime($_POST['time']);

Then you will have the value in seconds, which can use the gmdate (I think it would be this, I haven’t tested it yet) and compare it to the server values, only for the year, month and time. Minutes may be irrelevant and may not be very precise.

I didn’t create anything, but it’s just the idea for you to implement it yourself.

  • About your latest issue of the show time in the request, and if the user changes the time zone and date manually to send false information?

  • I don’t want him to be able to send any time zone, but I want the "real" time zone, which would be the time zone of the cellular network he is connected to.

  • @Edsonhoraciojunior but if it changes and send the information with another time you will use the server-side TIME= to compare with the payload, if they are different is because the phone is not synchronized with the correct time, understand?

  • the app has users from different countries, with different timezones, so I can’t use the server team as the basis, right?

  • @Edsonhoraciojunior there the scenario changes, a little right, I will try to create an example that caters to different regions, will not need much, but maybe depending on where you bought the phone or the configuration give a little problem, I will do some tests to make sure that the code both JS and PHP work the best possible, will not be quick to produce an example like this, but I promise to try to be as brief, if you have a little patience :)

  • I don’t think you need to create, I’m already creating a Cordova Plugin that will check these settings natively on Android and iOS, then post the answer here.

  • @Edsonhoraciojunior this will depend on examples too, if you find I do it first than the suggestion of checking with server, but I want you to understand that even the synchronization being automatic does not guarantee to be synchronized, besides that one thing should understand about stackoverflow, we post answers to help those who ask and future visitors who arrive at your question, so the idea is to make it useful in different ways, and on codes and platforms, it’s always nice to leave an alternative, not always what you believe is best is the best.

Show 2 more comments

0


I created a ngCordova plugin to solve this, currently only supports Android, because iOS does not programmatically provide the date and time settings of iPhone.

How to use

window.VerifyAutomaticDateTimeZone.isAutomaticChecked(function(isIt){
  if (isIt == 'true') {
        // fazer algo
    } else {
        // fazer outra coisa
    }
});

Case window.VerifyAutomaticDateTimeZone be Undefined, cover the call in $ionicPlatform.ready or ionic.Platform.ready

$ionicPlatform.ready(function(){
    // codigo aqui...
});
  • Excuse me, I think the initiative is very cool, but the automatic time zone being activated does not guarantee that it is correct. But worth the incentive, maybe in the future you can implement something with the own services that Android uses to "download" the Timezone schedule.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.