i'd like to use this code to know my ip, but i got 2 warnings that i can't fix for now. I also found this post : Accessing IP Address with NSHost
but i just wanted to understand why this code does not work, if anyone has an answer?
here's my code :
-(NSString*)getAddress { NSString *iphone_ip = [NSString initWithString:@"127.0.0.1"]; NSHost* myhost =[NSHost currentHost]; if (myhost) { NSString *ad = [myhost address]; if (ad) strcpy(iphone_ip,[ad cStringUsingEncoding: NSISOLatin1StringEncoding]); } return [NSString stringWithFormat:@"%s",iphone_ip]; }
the first warning is on :
NSHost* myhost =[NSHost currentHost]
, saying that currentHost is not recognised. The second one is on
NSString *ad = [myhost address];
" incompatible obj-c types initializing 'struct NSData ", expected 'struct NString" "
I can imagine the second warning might disappear when the first warning is resolved...
Thanks for your help
Paul
As you can see from the documentation here, it's documented under the OS X library.
NSHost
It's actually a private API on the iPhone. You should still be able to use it, but you'll get compiler warnings.
If you need to find out your IPAddress, you can use a NSURLRequest and NSURLConnection using this URL: WhatIsMyIP API
That page is there specifically for programmers to use. They ask that you ping it no more than once every 300 seconds. You can find a FAQ here: FAQ