1

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

Community
  • 1
  • 1
Paul
  • 6,108
  • 14
  • 72
  • 128
  • Whenever you have a static string that needs no modification do not use any NSString methods just store the string! `NSString *iphone_ip = @"127.0.0.1";` and next never call a method that begins with init without calling alloc first, I am sure have a compiler warnings for that as well since it is not a class method. – Joe Jun 30 '11 at 14:00
  • @Joe, thanks yes it makes sense i wrote it a bit too fast – Paul Jun 30 '11 at 14:10

1 Answers1

2

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

thomashw
  • 956
  • 4
  • 7
  • Along with compiler warnings you will get the boot from Apple when you go to submit it to the App store. – Joe Jun 30 '11 at 13:58
  • @thomashw thanks , the thing is, i took this example from appsamuck.com , it seems that a lot of their code have several "depreciated" methods or just a code that does not work. Do you know what i could do to make the ip thing work? – Paul Jun 30 '11 at 14:12
  • @thomashw thanks, why nsURLConnection? i knew NSURL and NSURLRequest to load a url, but not nsURLConnection. Thanks for the links – Paul Jul 01 '11 at 06:02
  • The NSURLConnection lets you load the URL request. It'll allow you to get the response back from the server by implementing the delegate methods. – thomashw Jul 02 '11 at 02:39