38

I got a webserver up and running in iphone by using Mongoose. But the problem is how can I get the ip address of my iphone/ipad to let the user's know where they can access the server. I found that [NSHost addresses] can do the job but I am developing for app store and this is undocumented method.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user757812
  • 581
  • 1
  • 7
  • 10

2 Answers2

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

// Get the INTERNAL ip address

- (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;

} 

https://web.archive.org/web/20160527165909/http://www.makebetterthings.com/iphone/how-to-find-ip-address-of-iphone/

Gabriel Jensen
  • 4,082
  • 1
  • 17
  • 14
Saurabh
  • 22,743
  • 12
  • 84
  • 133
  • 1
    is it officially accepted by Apple? – Raptor Aug 10 '11 at 04:07
  • @Shivan Raptor - yes this is officially accepted by apple! – Saurabh Aug 10 '11 at 05:06
  • If I were you, I would use an allocated string, just so that it doesn't stay in memory too long. – Nate Symer May 19 '12 at 19:59
  • Thanks a lot @Saurabh for this sample code, helped me a lot in my current project! – Linus Jäderlund Jun 01 '12 at 09:34
  • This method didn't retrieve the iP address when i using LTE / 3G internet connection – wod Nov 05 '13 at 11:40
  • 8
    This gives a different ip address than http://www.whatsmyip.org/ – Yunus Nedim Mehel Dec 27 '13 at 12:00
  • 1
    this one works with IPv6, too https://stackoverflow.com/questions/33125710/how-to-get-ipv6-interface-address-using-getifaddr-function/33127330#33127330 – lucianoenrico Jul 20 '16 at 15:41
  • The ip is different from whatsmyip.org, why? – Mathieu Jul 22 '16 at 11:58
  • Can I change the ip address obtained from this code? – Ali Mohyudin Aug 06 '16 at 17:54
  • @AliMohyudin ip address is not something which we can change. It is provided or assigned by ISP or router. You can go for static IP address in setting but you can not change that through code – Saurabh Aug 07 '16 at 02:27
  • 4
    @Mathieu, and others who ask why the address given by this method is different to what you get from whatsmyip.org etc. - That's because this method gives your LOCAL IP address. That is, the IP which other devices on your local area network (home hub, WiFi network, office intranet etc.) will use to talk to your device. These addresses usually start with 10. or 192.168. which marks them out as internal, un-routeable from the internet addresses. WhatsMyIP.org gives your EXTERNAL IP address, which is your local network gateway address, and all of the devices on your local area network share it. – Simon Tillson Mar 16 '17 at 04:21
  • Thanks. This is what I wanted. This method gives the local IP address instead of your public ip which you will get when you search " my ip" in google. I wanted the local wifi ip and this code works great. – abhimuralidharan Jun 27 '17 at 12:15
  • @Saurabh feel free to rollback my edit once your blog is fixed – Cœur Jul 01 '17 at 14:23
  • @Cœur thanks .. i will fix all the links by tomorrow.. – Saurabh Jul 02 '17 at 13:30
  • 2
    returns error always :( –  Oct 18 '17 at 11:38
  • Not working for me – jose920405 Jun 28 '18 at 19:38
  • wouldn't it be better to break the loop after you acquired the address? – cessmestreet Sep 23 '19 at 04:43
-1

I believe [NSHost address]; is documented, it's listed here: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSHost_Class/Reference/Reference.html

Use [[NSHost currentHost] address]; to get the IP.

TaylorP
  • 1,039
  • 2
  • 13
  • 28
  • 13
    its available for Mac OS ... not for iOS!! – user757812 Jul 24 '11 at 15:51
  • 4
    Avoid using NSHost: it's not thread safe. It must be run on the main thread, it's blocking and not deterministic. I've had an obvious-but-hard-to-explain deadlock thanks to it :( – Matt Melton Aug 20 '12 at 08:02
  • 3
    This is available on iOS 8. – Abhi Beckert Aug 16 '14 at 09:10
  • 3
    @AbhiBeckert, I'm I missing something? I can't seem to get this working with iOS 8 – John Riselvato Oct 30 '14 at 17:09
  • 3
    @JohnRiselvato I'm not sure what's going on. I was using NSHost on iOS 8 in August, but I ended up removing that feature from my app. Typing NSHost into `file -> open quickly` brings up NSStream.h but then that file doesn't contain a definition for the class. Weird. Perhaps Apple accidentally switched it from a private API to a public one, then reversed it? – Abhi Beckert Nov 03 '14 at 00:43
  • @Jesse, How can you say that? [Apple](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSHost_Class/) is saying **Available in OS X v10.0 and later** – Mrugesh Tank Jan 02 '16 at 09:55
  • @MrugeshTank: I think Jesse is speaking about iOS and not Mac OS X – Yogi Mar 08 '16 at 09:06
  • @Yogi, Yes but he said it's available in iOS also. So I forwarded link of Apple Docs which is saying that available in OS X (no sign of iOS). – Mrugesh Tank Mar 08 '16 at 09:12