views:

664

answers:

2

Hi guys,

How would I make a GUI program that displays your Ip address with a click of a button? Please, no difficult explanations, I just started Cocoa not long ago.

Thanks,

Kevin

+2  A: 

For determining the IP address, I found this.

As for making it into a Cocoa app, add an NSTextField (label) to your main window in Interface Builder, put in a button, add in an application controller (a subclass of NSObject that you make), put in the outlet and the action, do the proper connenctions, and in the "get IP" method, put in that code and set the value for the label's stringValue.

You can use [[NSHost currentHost] address], but it won't always display what you like. On my system, for example, it gives my IPv6 address.

EDIT: On my system, [[[NSHost currentHost] addresses] objectAtIndex:0] has my IPv4 address.

Cinder6
+4  A: 

[NSHost addresses] will get you an array of IPs. Read the xcode documentation for NSHost.

As for displaying that in a GUI, I recommend getting Aaron Hillegass' book Cocoa Programming for Mac OS X, or any Cocoa beginners book should teach that.

Phillip Jacobs
Is NSHost consistent in the ordering of the addresses? I've read some people saying it isn't, but that was talking about just plain address, not addresses. I've never had occasion to use NSHost.
Cinder6
Well, an array does maintain order, vs a dictionary.. I couldn't really say how reliable it is though.
Phillip Jacobs
Alright. What I meant was, does it always go IPv6, IPv4, link local, IPv4 loopback, IPv6 loopback?
Cinder6
Hmm.. it seems like if you have IPv6 enabled, it'll show that first, then IPv4.Otherwise I think it follows the sequence of your system preferences -> network interfaces list.
Phillip Jacobs