title: Get Public IP in Objective-C?
body: <p>In my application I need to get a NSString object value equal to the value of a users public/internet ip address. I have tried to ways of going about this but both return the local ip address NOT public. Below are my two methods. One is more precise and always returns the correct item in the array. The other does not. (Because one just picks a random index)...</p>
  
  <pre><code>- (NSString *)getPublicIP {   
      NSHost *publicIP = [[[NSHost currentHost] addresses] objectAtIndex:0];
      return publicIP;  
  }
  </code></pre>
  
  <p>Other more precise:(but does not get Public IP)</p>
  
  <pre><code> //start get ip
  - (NSString *)getIPWithNSHost { 
  
  NSArray *addresses = [[NSHost currentHost] addresses];
  NSString *stringAddress;
  for (NSString *anAddress in addresses) {
      if (![anAddress hasPrefix:@"127"] &amp;&amp; [[anAddress componentsSeparatedByString:@"."] count] == 4) {
      stringAddress = anAddress;
      break;
      }
      else {
      stringAddress = @"IPv4 address not available" ;
  
  }
      //NSLog(stringAddress);
  }
  NSLog (@"getIPWithNSHost: stringAddress = %@ ",stringAddress); 
  stringAddress = (@"getIPWithNSHost: stringAddress = %@ ",stringAddress);
  
  return  stringAddress;
  
  }
  </code></pre>
  
  <p>Either way I just need a way to get the external/public/internet ip address. (Just to clarify external/public/internet ip is one that can be retrieved from whatsmyip.org)</p>