views:

913

answers:

5

Hi,

I want to find IP address in an application. I am able to find it. But, problem is, it works fins in iphone os 2.0 or so. But, in iphone os 3.0 it is giving me a warning:

warning: no '+currentHost' method found

warning: (Messages without a matching method signature)

I am using this code, and it works fine with os version 2.0.

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

}

How to find IP address in iphone os 3.0 or greater os version?

Thanks in advance.

A: 

you have to bundle a server along with the application. There is one bundle application available at google code search it.

Ankit Sachan
+1  A: 

As far as I know there is only one hacky way to do that. You basically open a socket and get its address using POSIX functions. Here is the code I used for this:

http://iphonesdksnippets.com/post/2009/09/07/Get-IP-address-of-iPhone.aspx

[NSHost currentHost] will also work, but it is deprecated and considered a "Private API" by Apple, so you won't be able to submit your application to App Store.

Wicharek
+1  A: 

Simple! Just visit http://tejji.com/ip/default.aspx and find your ip address on iphone. If the url is too big use tejji.com/ip

+1  A: 

Getting the IP address is a bit hacky. Are you sure you couldn't live with the device ID (UDID) that is unique to each iPhone and can be retrieved easily via the public API ?

[UIDevice currentDevice].uniqueIdentifier

yonel
A: 

You can use Moan my IP to get your current IP.

jbrennan
Inappropriate response and -- depending on how unlucky you are -- not safe for work.
Dave Peck
Everyone needs a little humour in their day.
jbrennan