Geolocation by Iphone's Ip Address

Geolocation by iPhone's IP address

You could start by trying http://ip-api.com/json, which returns a JSON as explained on their API page.

You can then convert this JSON string to a dictionary and access the data.

func getIpLocation(completion: @escaping(NSDictionary?, Error?) -> Void)
{
let url = URL(string: "http://ip-api.com/json")!
var request = URLRequest(url: url)
request.httpMethod = "GET"

URLSession.shared.dataTask(with: request as URLRequest, completionHandler:
{ (data, response, error) in
DispatchQueue.main.async
{
if let content = data
{
do
{
if let object = try JSONSerialization.jsonObject(with: content, options: .allowFragments) as? NSDictionary
{
completion(object, error)
}
else
{
// TODO: Create custom error.
completion(nil, nil)
}
}
catch
{
// TODO: Create custom error.
completion(nil, nil)
}
}
else
{
completion(nil, error)
}
}
}).resume()
}

This function returns the dictionary or an error (after you resolve the TODO's). The completion is called on the main thread assuming you'll use the result to update the UI. If not, you can remove the DispatchQueue.main.async { }.

Exactly how accurate is IP Geolocation?

IP geolocation is really hit-or-miss, depending on both how the user's ISP assigns IPs and on the IP geolocation database you're using. For instance, I made a simple PHP script, IP2FireEagle, which looks up your IP. I found that the database kept placing me 10+ km to the west of where I really was. Updating my entry in Host IP wasn't the greatest, as it soon got reverted, presumably by someone also occasionally assigned that IP by my ISP! That being said, I found that Clarke has very accurate coordinates (not that this it's using IP geolocation per se but rather Skyhook's API and their WiFi geolocation database).

If it's a website for your friends and you know they have iPhones, I would suggest using its browser's support for navigator.geolocation.getCurrentPosition(). That is, get the location via Javascript and submit it to your server via an AJAX call. Even better since you want to use Google Maps, they give you a short tutorial on how get your friends' locations and then update a map.

iOS - Determine User's country without using CLLocationManager

You could get the user's IP address and use a geolocation database to guess their location based on that as described here: https://en.wikipedia.org/wiki/IP_address_location . If you are just concerned with region, you can probably find some data at regional internet registries (https://en.wikipedia.org/wiki/Regional_Internet_registry) websites to help you, since they control all the IP addresses for a particular region.

Get user country based on ip-address without asking for permission on ios

Don't do it.

The App Store Review Guidelines clearly state

4.1 Apps that do not notify and obtain user consent before collecting, transmitting, or using location data will be rejected

Getting location coordinates from device like iPhone in Java (not based on IP more like GPS or cell signal)

This depends on what you're asking.

If you mean "How can one retrieve GPS coordinates using Java in a native application on a smartphone?", then that answer will differ for every smartphone platform, and there is no answer for iPhones because iPhones do not run Java.

If you mean "How can one retrieve GPS coordinates in a web application using Java on the backend?", then you can use the Geolocation API in HTML5, assuming the browser supports it (as far as I know, Mobile Safari and the browser in Android support this. I don't know about Blackberry, and would assume that it does not). See here.

How to determine the country of a mobile phone browser

I use an IP database lookup from MaxMind. They offer a free database & simple API which provides city level accuracy. This database is updated monthly. They also provide for a fee more accurate database's showing metro area, corporate IP identification, ISP or NetSpeed. The cost is very reasonable.

Bear in mind that for the IP lookup, if someone is connecting over a corporate Lan, you'll get the location of that Lan. Similarly, I've noticed that higher than expected traffic is reported as coming from Norway (the Opera Mini Browser transcoder) and Canada (RIM BlackBerry transcoders ?).

iOS - is a user in the United States?

I would first try to check the carrier's Mobile Country Code. It will tell you the numeric mobile country code for the user's cellular service provider and cannot be changed by the user unless they switch to a service provider in a different country.

#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>

CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [netInfo subscriberCellularProvider];
NSString *mcc = [carrier mobileCountryCode];

This of course will not work with devices that are not connected to a mobile carrier (iPod Touch, some iPads, ect). Therefor, as a fall back, I would use or create my own IP geolocation API. You can get a free database that is 99.5% accurate on a country level. This will work great to detect the country of a device that doesn't have a mobile provider.

Is it possible to get user approximate location without permission in iOS?

From apple's documentation

Important: In addition to hardware not being available, the user has
the option of denying an application’s access to location service
data. During its initial uses by an application, the Core Location
framework prompts the user to confirm that using the location service
is acceptable. If the user denies the request, the CLLocationManager
object reports an appropriate error to its delegate during future
requests. You can also check the application’s explicit authorization
status using the authorizationStatus method.

So even if there exist some tricky way to avoid permission, you will be rejected at review stage.


What other way do you suggest to guess what country the user is
located?

You can get this information by using ip address.


Swift 4 - Get device's IP Address:

Add #include<ifaddrs.h> in your bridging header.

This is the framework needed to get IP address.

class func getIPAddress() -> String? {
var address: String?
var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
if getifaddrs(&ifaddr) == 0 {
var ptr = ifaddr
while ptr != nil {
defer { ptr = ptr?.pointee.ifa_next }

let interface = ptr?.pointee
let addrFamily = interface?.ifa_addr.pointee.sa_family
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {

if let name: String = String(cString: (interface?.ifa_name)!), name == "en0" {
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
getnameinfo(interface?.ifa_addr, socklen_t((interface?.ifa_addr.pointee.sa_len)!), &hostname, socklen_t(hostname.count), nil, socklen_t(0), NI_NUMERICHOST)
address = String(cString: hostname)
}
}
}
freeifaddrs(ifaddr)
}
return address
}

Get country using ip address

You could start by trying http://ip-api.com/json, which returns a JSON as explained on their API page.

You can then convert this JSON string to a dictionary and access the data.

func getIpLocation(completion: @escaping(NSDictionary?, Error?) -> Void)
{
let url = URL(string: "http://ip-api.com/json")!
var request = URLRequest(url: url)
request.httpMethod = "GET"

URLSession.shared.dataTask(with: request as URLRequest, completionHandler:
{ (data, response, error) in
DispatchQueue.main.async
{
if let content = data
{
do
{
if let object = try JSONSerialization.jsonObject(with: content, options: .allowFragments) as? NSDictionary
{
completion(object, error)
}
else
{
// TODO: Create custom error.
completion(nil, nil)
}
}
catch
{
// TODO: Create custom error.
completion(nil, nil)
}
}
else
{
completion(nil, error)
}
}
}).resume()
}

This function returns the dictionary or an error (after you resolve the TODO's). The completion is called on the main thread assuming you'll use the result to update the UI. If not, you can remove the DispatchQueue.main.async { }.



Related Topics



Leave a reply



Submit