-1

I have a requirement to get the IP address of user while making a booking for hotels and car. The backend needs IP address to get the exact location of the user.

I tried few suggested method in Objective-C (will convert later) like,

#include <ifaddrs.h>
#include <arpa/inet.h>

- (NSString *)getIPAddress {

NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
    // Loop through linked list of interfaces
    temp_addr = interfaces;
    while(temp_addr != NULL) {
        if(temp_addr->ifa_addr->sa_family == AF_INET) {
            // Check if interface is en0 which is the wifi connection on the iPhone
            if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                // Get NSString from C String
                address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];

            }

        }

        temp_addr = temp_addr->ifa_next;
    }
}
// Free memory
freeifaddrs(interfaces);
return address;

} 

I got only Local IP Address, something like 192.168.1.29. But by this we didn't get the location. Thanks in advance.

picciano
  • 22,341
  • 9
  • 69
  • 82
Deepak Chaudhary
  • 1,723
  • 1
  • 15
  • 29
  • 1
    How to get the external IP address: https://stackoverflow.com/questions/27708887/objective-c-how-to-get-the-public-ip-address-of-the-device . It's not possible to determine the external IP address without contacting an external server. – FBergo May 01 '18 at 06:41
  • 1
    Why don't your server help you do this. other local methods: https://stackoverflow.com/questions/30748480/swift-get-devices-ip-address – Devanshu Saini May 01 '18 at 06:46
  • @DevanshuSaini Coz Android guy already did it :( . – Deepak Chaudhary May 01 '18 at 06:48
  • Despite what you might see in CSI, an IP is not a very reliable indicator of a person's location. Further, you can't trust any information sent from the client anyway, because an attacker could spoof it. Your server should check the originating IP address, but even that could be spoofed or inaccurate, as I said. – Paulw11 May 01 '18 at 10:37
  • why don't you use location services...? – holex May 01 '18 at 11:01
  • Check out this post https://stackoverflow.com/questions/30748480/swift-get-devices-ip-address – Satish May 01 '18 at 11:49
  • @holex what if user denied for location service, we are using it for hotel and car booking... so we need to track the location. – Deepak Chaudhary May 01 '18 at 11:49
  • @Satish That is giving wifi address, I want public IP to get the location from the IP. – Deepak Chaudhary May 01 '18 at 11:52
  • @DeepakChaudhary whose public IP you need? If you need device location, you can't get it without location permission in iOS. – Satish May 01 '18 at 12:06
  • @Satish Yes we can, If we want. We can track at least the city, from where he is requesting the API at the backend. For example check this IP : 159.104.22.237 at https://www.iplocation.net/, they will give even lat long. For the same , my backend developer need ip. – Deepak Chaudhary May 01 '18 at 17:15

1 Answers1

1

I found some third party who is giving Public IP Address.

do { let ipAddress = try String(contentsOf: URL.init(string: "http://icanhazip.com/")!, encoding: String.Encoding.utf8)
        print("IP AddRess : ", ipAddress)

    } catch { print(error) }

But don't know accepted by Apple or not.

Deepak Chaudhary
  • 1,723
  • 1
  • 15
  • 29
  • It's https so no *obvious* reason Apple should reject, but see [this answer](https://superuser.com/questions/420969/services-that-just-return-your-public-ip-as-text) from a few years back suggesting that one is unreliable and giving some others and the roll-your-own solution. – CRD May 01 '18 at 08:39
  • @CRD If we use http instead of https.. does it works? – Deepak Chaudhary May 01 '18 at 09:06
  • I see you've changed https to http in the sample. Yes it might make a difference, Look at Apple's requirements. – CRD May 01 '18 at 12:03
  • I have used this in a high-profile app. It works and Apple does not reject. Do use HTTPS, however. – picciano May 01 '18 at 14:45