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.
Asked
Active
Viewed 7.8k times
38

Cœur
- 37,241
- 25
- 195
- 267

user757812
- 581
- 1
- 7
- 10
-
How is this a duplicate if the other question doesn't have a marked answer? – Morkrom May 16 '14 at 19:40
-
@Morkrom a marked answer is only relevant to one single person: the one who initially wrote the question. – Cœur Jul 01 '17 at 14:26
2 Answers
123
#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;
}

Gabriel Jensen
- 4,082
- 1
- 17
- 14

Saurabh
- 22,743
- 12
- 84
- 133
-
1
-
-
If I were you, I would use an allocated string, just so that it doesn't stay in memory too long. – Nate Symer May 19 '12 at 19:59
-
Thanks a lot @Saurabh for this sample code, helped me a lot in my current project! – Linus Jäderlund Jun 01 '12 at 09:34
-
This method didn't retrieve the iP address when i using LTE / 3G internet connection – wod Nov 05 '13 at 11:40
-
8This gives a different ip address than http://www.whatsmyip.org/ – Yunus Nedim Mehel Dec 27 '13 at 12:00
-
1this one works with IPv6, too https://stackoverflow.com/questions/33125710/how-to-get-ipv6-interface-address-using-getifaddr-function/33127330#33127330 – lucianoenrico Jul 20 '16 at 15:41
-
-
-
@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 – Saurabh Aug 07 '16 at 02:27
-
4@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. – Simon Tillson Mar 16 '17 at 04:21
-
Thanks. 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. – abhimuralidharan Jun 27 '17 at 12:15
-
-
-
2
-
-
wouldn't it be better to break the loop after you acquired the address? – cessmestreet Sep 23 '19 at 04:43
-1
I believe [NSHost address];
is documented, it's listed here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSHost_Class/Reference/Reference.html
Use [[NSHost currentHost] address];
to get the IP.

TaylorP
- 1,039
- 2
- 13
- 28
-
13
-
4Avoid 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 :( – Matt Melton Aug 20 '12 at 08:02
-
3
-
3@AbhiBeckert, I'm I missing something? I can't seem to get this working with iOS 8 – John Riselvato Oct 30 '14 at 17:09
-
3@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? – Abhi Beckert Nov 03 '14 at 00:43
-
@Jesse, How can you say that? [Apple](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSHost_Class/) is saying **Available in OS X v10.0 and later** – Mrugesh Tank Jan 02 '16 at 09:55
-
-
@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). – Mrugesh Tank Mar 08 '16 at 09:12