Get iPhone IP address function

From , 5 Years ago, written in Objective-C, viewed 204 times.
URL https://pastebin.vip/view/5c2631d5
  1. #include <ifaddrs.h>
  2. #include <arpa/inet.h>
  3.  
  4.  
  5. - (NSString *)getIPAddress
  6. {
  7.         NSString *address = @"error";
  8.         struct ifaddrs *interfaces = NULL;
  9.         struct ifaddrs *temp_addr = NULL;
  10.         int success = 0;
  11.  
  12.         // retrieve the current interfaces - returns 0 on success
  13.         success = getifaddrs(&interfaces);
  14.         if (success == 0)
  15.         {
  16.                 // Loop through linked list of interfaces
  17.                 temp_addr = interfaces;
  18.                 while(temp_addr != NULL)
  19.                 {
  20.                         if(temp_addr->ifa_addr->sa_family == AF_INET)
  21.                         {
  22.                                 // Check if interface is en0 which is the wifi connection on the iPhone
  23.                                 if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en1"])
  24.                                 {
  25.                                         // Get NSString from C String
  26.                                         address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
  27.                                 }
  28.                         }
  29.  
  30.                         temp_addr = temp_addr->ifa_next;
  31.                 }
  32.         }
  33.  
  34.         // Free memory
  35.         freeifaddrs(interfaces);
  36.  
  37.         return address;
  38. }
  39. //objectc/3902

Reply to "Get iPhone IP address function"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website