how to get ip address of iphone programmatically

75,224
#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/

Share:
75,224
user757812
Author by

user757812

Updated on May 24, 2020

Comments

  • user757812
    user757812 almost 4 years

    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.

  • user757812
    user757812 almost 13 years
    its available for Mac OS ... not for iOS!!
  • Raptor
    Raptor over 12 years
    is it officially accepted by Apple?
  • Saurabh
    Saurabh over 12 years
    @Shivan Raptor - yes this is officially accepted by apple!
  • Nate Symer
    Nate Symer almost 12 years
    If I were you, I would use an allocated string, just so that it doesn't stay in memory too long.
  • Linus Jäderlund
    Linus Jäderlund almost 12 years
    Thanks a lot @Saurabh for this sample code, helped me a lot in my current project!
  • Saurabh
    Saurabh almost 12 years
    You are most welcome @LinusPersson and thanks for upvote!
  • Matt Melton
    Matt Melton over 11 years
    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 :(
  • wod
    wod over 10 years
    This method didn't retrieve the iP address when i using LTE / 3G internet connection
  • Yunus Nedim Mehel
    Yunus Nedim Mehel over 10 years
    This gives a different ip address than whatsmyip.org
  • Abhi Beckert
    Abhi Beckert over 9 years
    This is available on iOS 8.
  • John Riselvato
    John Riselvato over 9 years
    @AbhiBeckert, I'm I missing something? I can't seem to get this working with iOS 8
  • Abhi Beckert
    Abhi Beckert over 9 years
    @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?
  • Mrugesh Tank
    Mrugesh Tank over 8 years
    @Jesse, How can you say that? Apple is saying Available in OS X v10.0 and later
  • Yogi
    Yogi about 8 years
    @MrugeshTank: I think Jesse is speaking about iOS and not Mac OS X
  • Mrugesh Tank
    Mrugesh Tank about 8 years
    @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).
  • lucianoenrico
    lucianoenrico almost 8 years
    this one works with IPv6, too stackoverflow.com/questions/33125710/…
  • Mathieu
    Mathieu almost 8 years
    The ip is different from whatsmyip.org, why?
  • Ali Mohyudin
    Ali Mohyudin over 7 years
    Can I change the ip address obtained from this code?
  • Saurabh
    Saurabh over 7 years
    @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
  • Simon Tillson
    Simon Tillson about 7 years
    @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.
  • abhimuralidharan
    abhimuralidharan almost 7 years
    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.
  • Cœur
    Cœur almost 7 years
    @Saurabh feel free to rollback my edit once your blog is fixed
  • Saurabh
    Saurabh almost 7 years
    @Cœur thanks .. i will fix all the links by tomorrow..
  • Admin
    Admin over 6 years
    returns error always :(
  • jose920405
    jose920405 almost 6 years
    Not working for me
  • cessmestreet
    cessmestreet over 4 years
    wouldn't it be better to break the loop after you acquired the address?