Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include <ifaddrs.h>
  2. #include <arpa/inet.h>
  3.  
  4. - (NSString *)getIPAddress {
  5.  
  6. NSString *address = @"error";
  7. struct ifaddrs *interfaces = NULL;
  8. struct ifaddrs *temp_addr = NULL;
  9. int success = 0;
  10. // retrieve the current interfaces - returns 0 on success
  11. success = getifaddrs(&interfaces);
  12. if (success == 0) {
  13. // Loop through linked list of interfaces
  14. temp_addr = interfaces;
  15. while(temp_addr != NULL) {
  16. if(temp_addr->ifa_addr->sa_family == AF_INET) {
  17. // Check if interface is en0 which is the wifi connection on the iPhone
  18. if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
  19. // Get NSString from C String
  20. address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
  21.  
  22. }
  23.  
  24. }
  25.  
  26. temp_addr = temp_addr->ifa_next;
  27. }
  28. }
  29. // Free memory
  30. freeifaddrs(interfaces);
  31. return address;
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement