how to get ip address of iphone programmatically
75,224
#include <ifaddrs.h>
#include <arpa/inet.h>
// Get the INTERNAL ip address
- (NSString *)getIPAddress {
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
}
Author by
user757812
Updated on May 24, 2020Comments
-
user757812 almost 4 years
I got a webserver up and running in iphone by using Mongoose. But the problem is how can I get the ip address of my iphone/ipad to let the user's know where they can access the server. I found that
[NSHost addresses]
can do the job but I am developing for app store and this is undocumented method. -
user757812 almost 13 yearsits available for Mac OS ... not for iOS!!
-
Raptor over 12 yearsis it officially accepted by Apple?
-
Saurabh over 12 years@Shivan Raptor - yes this is officially accepted by apple!
-
Nate Symer almost 12 yearsIf I were you, I would use an allocated string, just so that it doesn't stay in memory too long.
-
Linus Jäderlund almost 12 yearsThanks a lot @Saurabh for this sample code, helped me a lot in my current project!
-
Saurabh almost 12 yearsYou are most welcome @LinusPersson and thanks for upvote!
-
Matt Melton over 11 yearsAvoid using NSHost: it's not thread safe. It must be run on the main thread, it's blocking and not deterministic. I've had an obvious-but-hard-to-explain deadlock thanks to it :(
-
wod over 10 yearsThis method didn't retrieve the iP address when i using LTE / 3G internet connection
-
Yunus Nedim Mehel over 10 yearsThis gives a different ip address than whatsmyip.org
-
Abhi Beckert over 9 yearsThis is available on iOS 8.
-
John Riselvato over 9 years@AbhiBeckert, I'm I missing something? I can't seem to get this working with iOS 8
-
Abhi Beckert over 9 years@JohnRiselvato I'm not sure what's going on. I was using NSHost on iOS 8 in August, but I ended up removing that feature from my app. Typing NSHost into
file -> open quickly
brings up NSStream.h but then that file doesn't contain a definition for the class. Weird. Perhaps Apple accidentally switched it from a private API to a public one, then reversed it? -
Mrugesh Tank over 8 years@Jesse, How can you say that? Apple is saying Available in OS X v10.0 and later
-
Yogi about 8 years@MrugeshTank: I think Jesse is speaking about iOS and not Mac OS X
-
Mrugesh Tank about 8 years@Yogi, Yes but he said it's available in iOS also. So I forwarded link of Apple Docs which is saying that available in OS X (no sign of iOS).
-
lucianoenrico almost 8 yearsthis one works with IPv6, too stackoverflow.com/questions/33125710/…
-
Mathieu almost 8 yearsThe ip is different from whatsmyip.org, why?
-
Ali Mohyudin over 7 yearsCan I change the ip address obtained from this code?
-
Saurabh over 7 years@AliMohyudin ip address is not something which we can change. It is provided or assigned by ISP or router. You can go for static IP address in setting but you can not change that through code
-
Simon Tillson about 7 years@Mathieu, and others who ask why the address given by this method is different to what you get from whatsmyip.org etc. - That's because this method gives your LOCAL IP address. That is, the IP which other devices on your local area network (home hub, WiFi network, office intranet etc.) will use to talk to your device. These addresses usually start with 10. or 192.168. which marks them out as internal, un-routeable from the internet addresses. WhatsMyIP.org gives your EXTERNAL IP address, which is your local network gateway address, and all of the devices on your local area network share it.
-
abhimuralidharan almost 7 yearsThanks. This is what I wanted. This method gives the local IP address instead of your public ip which you will get when you search " my ip" in google. I wanted the local wifi ip and this code works great.
-
Cœur almost 7 years@Saurabh feel free to rollback my edit once your blog is fixed
-
Saurabh almost 7 years@Cœur thanks .. i will fix all the links by tomorrow..
-
Admin over 6 yearsreturns error always :(
-
jose920405 almost 6 yearsNot working for me
-
cessmestreet over 4 yearswouldn't it be better to break the loop after you acquired the address?