views:

4868

answers:

3

Does anyone know how to programmatically get an iPhone's MAC address and IP address?

A: 

You can access it using Settings -> General -> About. It is listed under "Wi-fi Address."

If you're talking about doing this programmatically, maybe we should talk about what you're trying to do :)

JoshJordan
i want to get MAC address and IP address programmatically in an iPhone application.
+9  A: 

Somthing I stumbled across a while ago. Originally from here I modified it a bit and cleaned things up.

IPAddress.h
IPAddress.c

And to use it

InitAddresses();
GetIPAddresses();
GetHWAddresses();

int i;
NSString *deviceIP = nil;
for (i=0; i<MAXADDRS; ++i)
{
 static unsigned long localHost = 0x7F000001;  // 127.0.0.1
 unsigned long theAddr;

 theAddr = ip_addrs[i];

 if (theAddr == 0) break;
 if (theAddr == localHost) continue;

 NSLog(@"Name: %s MAC: %s IP: %s\n", if_names[i], hw_addrs[i], ip_names[i]);

        //decided what adapter you want details for
 if (strncmp(if_names[i], "en", 2) == 0)
 {
  NSLog(@"Adapter en has a IP of %@", [NSString stringWithFormat:@"%s", ip_names[i]]);
 }
}

Adapter names vary depending on the simulator/device as well as wifi or cell on the device.

Hope that helps.

chris.

PyjamaSam
i just wonder if this is any different from a generic OS X approach (i'm asking because i'm a mac n00b)
DrJokepu
@DrJokepuI think its basic OSX stuff. Actually its basic BSD stuff. The same approach would most likely work on linux as well (even possibly windows with a little tweaking)
PyjamaSam
hi...I want to get my current location using wifi in iphone not by using GPS.can u help me for that?
@abc: I recommend you post another question about that. Be better then burying it in comments for a different question.
PyjamaSam
but what about the call to ether_ntoa ? this is private API isn't it?
stigi
I don't belive any of the methods used in this implimentation are concidered part of the private api. They are all base BSD calls.You can see more about ether_ntoa here:http://developer.apple.com/Mac/library/documentation/Darwin/Reference/ManPages/man3/ether_ntoa.3.htmlIts just converting the binary representation of the Ethernet address to a string form.
PyjamaSam
A: 

Okay this looks nice but I am new to MAC OS X programming.so I want to know can I use the same code for getting MAC address if a iMac or Mac book? If yes what kind of project should I use in X code - command line application or cocoa application? and if not, what changes are required in this code or where can I find he appropriate reference ? thank you.

Aniket