I looked for some code that will help me to get the ip that the iPhone connect with.
I find this one:
- (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;
}
but the problem is that he get me this ip
10.0.0.1
How to get the external ip?
The easiest way to get your internet ip address from code is to use NSURLConnection.
For the URL you can use:
http://www.whatismyip.com/m/mobile.asp
or
http://checkip.dyndns.com/
Just parse the return data and you have your external ip address.
Have a look at the example in my second Answer here.
In a nutshell it uses *http://www.dyndns.org/cgi-bin/check_ip.cg*i to get the extenal I.P
Check Apple's PortMapper, does exactly what you want.
As of iOS7 this is irrelevant.
Late to the party, but https://api4.ipify.org or http://api4.ipify.org returns nothing else but the external IPv4 address of your connection. Code:
NSURL *ipifyUrl = [NSURL URLWithString:#"https://api4.ipify.org/"];
NSString *externalAddr = [NSString stringWithContentsOfURL:ipifyUrl encoding:NSUTF8StringEncoding error:nil];
https://api6.ipify.org returns the external IPv6 address and https://api64.ipify.org either the IPv4 or the IPv6 address. Simple documentation can be found at https://www.ipify.org
Related
This question already has answers here:
How to get the public IP address of the device
(9 answers)
Closed 4 years ago.
I have a requirement to get the IP address of user while making a booking for hotels and car. The backend needs IP address to get the exact location of the user.
I tried few suggested method in Objective-C (will convert later) like,
#include <ifaddrs.h>
#include <arpa/inet.h>
- (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;
}
I got only Local IP Address, something like 192.168.1.29.
But by this we didn't get the location.
Thanks in advance.
I found some third party who is giving Public IP Address.
do { let ipAddress = try String(contentsOf: URL.init(string: "http://icanhazip.com/")!, encoding: String.Encoding.utf8)
print("IP AddRess : ", ipAddress)
} catch { print(error) }
But don't know accepted by Apple or not.
I am working on project, which has find the camera IP-address which is connect in LAN network, i am using LAN search for iOS to find the camera, it works but some times camera has self assigned to (169.254.164.XXX) default IP address. LAN search can’t find it. How can i find the all camera which is present in LAN network, even though camera in default network?
This will returns local 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 == 2) {
// Check if interface is end 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;
}
I'm trying to create a streamer in my app over RTSP protocol to use it for Facebook live. I got this Project and it seems to work perfect except one thing which is the URL. The library get the device IP via this code :
+ (NSString*) getIPAddress
{
NSString* address;
struct ifaddrs *interfaces = nil;
// get all our interfaces and find the one that corresponds to wifi
if (!getifaddrs(&interfaces))
{
for (struct ifaddrs* addr = interfaces; addr != NULL; addr = addr->ifa_next)
{
if (([[NSString stringWithUTF8String:addr->ifa_name] isEqualToString:#"en0"]) &&
(addr->ifa_addr->sa_family == AF_INET))
{
struct sockaddr_in* sa = (struct sockaddr_in*) addr->ifa_addr;
address = [NSString stringWithUTF8String:inet_ntoa(sa->sin_addr)];
break;
}
}
}
freeifaddrs(interfaces);
return address;
}
So the stream can be accessed via the IP. My question is: is there anyway to make the RTSP to stream to a URL so I can provide it to the server?
I found some iOS app which could scan the local network and list all the device information.
I wonder how.
Pic:
Step 1 - Gel local IP
For iOS 7 and lower
#include <ifaddrs.h>
#include <arpa/inet.h>
- (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;
}
iOS 8
[[NSHost currentHost] address]
Step 2 - Get online IPs
https://github.com/tonymillion/Reachability
Using Reachability, you can get subnet IP's reachability.
Reachability* reach = [Reachability reachabilityWithHostname:subnetIp];
Step 3 - Resolve hostname
Apple has a great article, which covers HostnameResolving topics.
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/ResolvingDNSHostnames.html
You can try also simpler variant in iOS8
[[NSHost hostWithAddress:#"173.194.34.24"] name]
Example project
https://github.com/mongizaidi/LAN-Scan
here is a small project, that does the Local Area Network scan, you need only to resolve the hostnames, and you will get result that you want.
I want to use NSStream to send send and receive information to server.
I thought if I can obtain the ip address the server can send information back in order to replace the silent push notification.
Is is possible to register a notification when ip address changed?
P.S.
I'm currently using reachability class to register a notification when network changed but if there is a better way it'll be great!
I think there is not straight way to achieve this but Use
`[[NSHost currentHost] address];`
save value into userDefault and every time when you connect check with stored value and get notify
i found this on SO hope this work for you
#include <ifaddrs.h>
#include <arpa/inet.h>
- (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;
}