Goal C - How to get the public IP address of a device

I found this sample code to get all the local IP addresses, but I did not find a simple solution to get the public IP address.

A legacy class from Apple is allowed to do this ... but it's a legacy ...

+6
source share
6 answers

I have used ALSystemUtilities in the past. You need to make an external call to find out.

+ (NSString *)externalIPAddress { // Check if we have an internet connection then try to get the External IP Address if (![self connectedViaWiFi] && ![self connectedVia3G]) { // Not connected to anything, return nil return nil; } // Get the external IP Address based on dynsns.org NSError *error = nil; NSString *theIpHtml = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.dyndns.org/cgi-bin/check_ip.cgi"] encoding:NSUTF8StringEncoding error:&error]; if (!error) { NSUInteger an_Integer; NSArray *ipItemsArray; NSString *externalIP; NSScanner *theScanner; NSString *text = nil; theScanner = [NSScanner scannerWithString:theIpHtml]; while ([theScanner isAtEnd] == NO) { // find start of tag [theScanner scanUpToString:@"<" intoString:NULL] ; // find end of tag [theScanner scanUpToString:@">" intoString:&text] ; // replace the found tag with a space //(you can filter multi-spaces out later if you wish) theIpHtml = [theIpHtml stringByReplacingOccurrencesOfString: [ NSString stringWithFormat:@"%@>", text] withString:@" "] ; ipItemsArray = [theIpHtml componentsSeparatedByString:@" "]; an_Integer = [ipItemsArray indexOfObject:@"Address:"]; externalIP =[ipItemsArray objectAtIndex:++an_Integer]; } // Check that you get something back if (externalIP == nil || externalIP.length <= 0) { // Error, no address found return nil; } // Return External IP return externalIP; } else { // Error, no address found return nil; } } 

Source ALSystemUtilities

+7
source

It is so simple:

 NSString *publicIP = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"https://icanhazip.com/"] encoding:NSUTF8StringEncoding error:nil]; publicIP = [publicIP stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]; // IP comes with a newline for some reason 
+8
source

You need to request an external server to find out the public IP address. Either configure your own server (1 line of php code), or use one of the many available that return IP as plain text or json when making an HTTP request. For example, http://myipdoc.com/ip.php .

0
source

For those of us who use Swift, here my Andrei translation responds with the addition of NSURLSession to run it in the background. To check the network, I use Reachability.swift . Also, be sure to add dyndns.org to NSExceptionDomains for NSAppTransportSecurity in NSAppTransportSecurity .

 var ipAddress:String? func getIPAddress() { if reachability!.isReachable() == false { return } guard let ipServiceURL = NSURL(string: "http://www.dyndns.org/cgi-bin/check_ip.cgi") else { return } let session = NSURLSession.sharedSession() let task = session.dataTaskWithURL(ipServiceURL, completionHandler: {(data, response, error) -> Void in if error != nil { print(error) return } let ipHTML = NSString(data: data!, encoding: NSUTF8StringEncoding) as? String self.ipAddress = self.scanForIPAddress(ipHTML) }) task.resume() } func scanForIPAddress(var ipHTML:String?) -> String? { if ipHTML == nil { return nil } var externalIPAddress:String? var index:Int? var ipItems:[String]? var text:NSString? let scanner = NSScanner(string: ipHTML!) while scanner.atEnd == false { scanner.scanUpToString("<", intoString: nil) scanner.scanUpToString(">", intoString: &text) ipHTML = ipHTML!.stringByReplacingOccurrencesOfString(String(text!) + ">", withString: " ") ipItems = ipHTML!.componentsSeparatedByString(" ") index = ipItems!.indexOf("Address:") externalIPAddress = ipItems![++index!] } if let ip = externalIPAddress { print("External IP Address: \(ip)") } return externalIPAddress } 
0
source

I find the answers of Andrey and Tarek useful. Both rely on a web URL to request the β€œpublic IP” of an iOS / OS X device.

However, there is a problem with this approach in some part of the world where a URL such as http://www.dyndns.org/cgi-bin/check_ip.cgi "is censored as an in andrei response:

 NSString *theIpHtml = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.dyndns.org/cgi-bin/check_ip.cgi"] encoding:NSUTF8StringEncoding error:&error]; 

In this case, we will need to use the "obscene" URL in this region, for example http://1212.ip138.com/ic.asp

Please note that the website URL may use a different HTML code and encoding than what Andrey can answer - in the above URL some very gentle changes can fix it using kCFStringEncodingGB_18030_2000 for http: // 1212 .ip138.com / ic.asp

 NSURL* externalIPCheckURL = [NSURL URLWithString: @"http://1212.ip138.com/ic.asp"]; encoding:NSUTF8StringEncoding error:nil]; NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); NSString *theIpHtml = [NSString stringWithContentsOfURL: externalIPCheckURL encoding: encoding error: &error]; 
0
source

Can you call the public IP lookup services to get this? I installed http://ipof.in as a service that returns the device IP address as JSON / XML or plain text. You can find them here.

For JSON with GeoIP Data

http://ipof.in/json

https://ipof.in/json

For XML response

http://ipof.in/xml

https://ipof.in/xml

For plaintext IP address

http://ipof.in/txt

https://ipof.in/txt

0
source

Source: https://habr.com/ru/post/980321/


All Articles