Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can get IP address through two ways:</p> <p>1- if you want to get the local ip address on the current used netwrok, you can use the following method to retrive it:</p> <pre><code>-(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(&amp;interfaces); if (success == 0) { // Loop through linked list of interfaces temp_addr = interfaces; while(temp_addr != NULL) { if(temp_addr-&gt;ifa_addr-&gt;sa_family == AF_INET) { // Get NSString from C String address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr-&gt;ifa_addr)-&gt;sin_addr)]; } temp_addr = temp_addr-&gt;ifa_next; } } // Free memory freeifaddrs(interfaces); return address; } </code></pre> <p>2- if you want to get the external IP address then you need to use the following method:</p> <pre><code>-(NSString*)getIP { NSUInteger an_Integer; NSArray * ipItemsArray; NSString *externalIP; NSURL *iPURL = [NSURL URLWithString:@"http://www.dyndns.org/cgi-bin/check_ip.cgi"]; if (iPURL) { NSError *error = nil; NSString *theIpHtml = [NSString stringWithContentsOfURL:iPURL encoding:NSUTF8StringEncoding error:&amp;error]; if (!error) { NSScanner *theScanner; NSString *text = nil; theScanner = [NSScanner scannerWithString:theIpHtml]; while ([theScanner isAtEnd] == NO) { // find start of tag [theScanner scanUpToString:@"&lt;" intoString:NULL] ; // find end of tag [theScanner scanUpToString:@"&gt;" intoString:&amp;text] ; // replace the found tag with a space //(you can filter multi-spaces out later if you wish) theIpHtml = [theIpHtml stringByReplacingOccurrencesOfString: [ NSString stringWithFormat:@"%@&gt;", text] withString:@" "] ; ipItemsArray =[theIpHtml componentsSeparatedByString:@" "]; an_Integer=[ipItemsArray indexOfObject:@"Address:"]; externalIP =[ipItemsArray objectAtIndex: ++an_Integer]; } NSLog(@"%@",externalIP); } else { NSLog(@"Oops... g %d, %@", [error code], [error localizedDescription]); } } return externalIP; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload