get iphone ip address


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  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. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.