1

Possible Duplicate:
How do I query the iPhone's current IP address?

I'm trying to find the local ip address using gethostname/gethostbyname but I always get "Unable to Resolve" when doing it.

Why could it be failing? Are there other way to get the local ip address in the iphone?

Thx a lot

Community
  • 1
  • 1

5 Answers5

3
    -(NSString*)getAddress {
          char iphone_ip[255];
          strcpy(iphone_ip,"127.0.0.1"); // if everything fails
          NSHost* myhost =[NSHost currentHost];
          if (myhost)
          {
              NSString *ad = [myhost address];
              if (ad)
                  strcpy(iphone_ip,[ad cStringUsingEncoding: NSISOLatin1StringEncoding]);
          }

          return [NSString stringWithFormat:@"%s",iphone_ip]; 
     }
Andy Jacobs
  • 15,187
  • 13
  • 60
  • 91
2

I found a solution using gethostname/gethostbyname in the Erica Sadun book. The trick is to append the ".local" suffix to the hostname before calling gethostbyname.

ggarber
  • 8,300
  • 5
  • 27
  • 32
2

In addition to the other answers here, I would suggest reviewing the Reachability sample code on the ADC site.

Rog
  • 17,070
  • 9
  • 50
  • 73
0

NSHost is not found in iPhone OS Library, it is present in Mac OS Core Library.

I am getting 2 warnings

"warning: no '+currentHost' method found" "warning: no '-address' method found"

Any way to remove these warnings ???

Biranchi
  • 16,120
  • 23
  • 124
  • 161
0

It's worth noting that many carriers (especially outside the "first world") hand out RFC1918 (usually 10.0.0.0/8) addresses and so doing a HTTP request to one of the services could be better (except, of course, that many carriers, out of necessity use multiple public addresses for large NAT pools)

LapTop006
  • 512
  • 4
  • 9