1

If say user has not given me location permission then how can I detect that the user is in Europe?

k-thorat
  • 4,873
  • 1
  • 27
  • 36
user3519594
  • 387
  • 1
  • 11

2 Answers2

1

May this help you:

Use current TimeZone (or try Locale). It will give (near by) time zone, set by user for its device date-time.

Objective C:

NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSString *timeZoneName = [timeZone name];
NSLog(@"timeZoneName - %@",timeZoneName);

NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSLog(@"countryCode - %@",countryCode);

Swift 4:

let name = TimeZone.current
print("TimeZone name -  \(name)")
// TimeZone name - Asia/Kolkata (current)

let currentlocale = Locale.current
print("currentlocale - \(currentlocale)")
// currentlocale - en_IN (current)

let regioncode = Locale(identifier: currentlocale.regionCode!)    
print("regioncode - \(regioncode)")
// regioncode - in (fixed)
Krunal
  • 77,632
  • 48
  • 245
  • 261
  • TimeZone or Locale depend only on device's settings, not physical location. Since OP mentioned trying to use location, my guess is he wants the latter. – mag_zbc May 18 '18 at 10:56
  • @mag_zbc - I know that. But as per my knowledge this is the possible (near by) answer. There is may not be exact solution for this question because of location permission (or I may be wrong). But to help OP I just posted near by answer. May it can be useful to find a solution. – Krunal May 18 '18 at 11:00
-1

Checking Locale will not do you much good, because it reflects only your device's setting, not physical location.

If you have been denied location service, I think the best workaround would be to use a free IP-based geolocation service, like this one.

All you need to do is perform a GET request to http://ip-api.com/json/{ipAddress}

Example:

GET http://ip-api.com/json/8.8.8.8 (google-public-dns-a.google.com) returns a json

{
    "as": "AS15169 Google LLC",
    "city": "Mountain View",
    "country": "United States",
    "countryCode": "US",
    "isp": "Google",
    "lat": 37.4229,
    "lon": -122.085,
    "org": "Google",
    "query": "8.8.8.8",
    "region": "CA",
    "regionName": "California",
    "status": "success",
    "timezone": "America/Los_Angeles",
    "zip": ""
}

As you can see, it returns you a bunch of useful information, like country name or country code.

You can easily find how to get device's IP address in Objective-c, for example here

The downside is that you will not get accurate result if the user is connected to internet via VPN.

mag_zbc
  • 6,801
  • 14
  • 40
  • 62
  • 1
    GDPR - I cannot get IP of user without his consent. And to show consent dialog I need to detect if he is in Europe. :) – user3519594 May 21 '18 at 10:23
  • According to documentation, you don't need to specify IP - _You can supply an IP address or domain to lookup, or none to use your current IP address._ – mag_zbc May 21 '18 at 10:38