Guest User

Untitled

a guest
Feb 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. warning: no '+currentHost' method found
  2.  
  3. warning: (Messages without a matching method signature)
  4.  
  5. -(NSString*)getAddress {
  6. char iphone_ip[255];
  7. strcpy(iphone_ip,"127.0.0.1"); // if everything fails
  8. NSHost* myhost = [NSHost currentHost];
  9. if (myhost)
  10. {
  11. NSString *ad = [myhost address];
  12. if (ad)
  13. strcpy(iphone_ip,[ad cStringUsingEncoding: NSISOLatin1StringEncoding]);
  14. }
  15. return [NSString stringWithFormat:@"%s",iphone_ip];
  16.  
  17. #include <arpa/inet.h>
  18. #include <netdb.h>
  19. #include <net/if.h>
  20. #include <ifaddrs.h>
  21.  
  22. // retun the host name
  23. + (NSString *)hostname
  24. {
  25. char baseHostName[256];
  26. int success = gethostname(baseHostName, 255);
  27. if (success != 0) return nil;
  28. baseHostName[255] = '';
  29.  
  30. #if !TARGET_IPHONE_SIMULATOR
  31. return [NSString stringWithFormat:@"%s.local", baseHostName];
  32. #else
  33. return [NSString stringWithFormat:@"%s", baseHostName];
  34. #endif
  35. }
  36.  
  37. // return IP Address
  38. + (NSString *)localIPAddress
  39. {
  40. struct hostent *host = gethostbyname([[self hostname] UTF8String]);
  41. if (!host) {herror("resolv"); return nil;}
  42. struct in_addr **list = (struct in_addr **)host->h_addr_list;
  43. return [NSString stringWithCString:inet_ntoa(*list[0]) encoding:NSUTF8StringEncoding];
  44. }
  45.  
  46. <?php
  47. $ip = getenv("REMOTE_ADDR");
  48. echo $ip;
  49. ?>
  50.  
  51. NSURL *scriptURL = [[NSURL alloc] initWithString:@"http://yourserver.com/script.php"];
  52. NSString *ip = [NSString stringWithContentsOfURL: scriptURL encoding:NSASCIIStringEncoding error:nil];
  53.  
  54. - (NSString *)getIPAddress {
  55.  
  56. NSString *address = @"error";
  57. struct ifaddrs *interfaces = NULL;
  58. struct ifaddrs *temp_addr = NULL;
  59. int success = 0;
  60. // retrieve the current interfaces - returns 0 on success
  61. success = getifaddrs(&interfaces);
  62.  
  63. if (success == 0) {
  64. // Loop through linked list of interfaces
  65.  
  66. temp_addr = interfaces;
  67. while(temp_addr != NULL) {
  68.  
  69. if(temp_addr->ifa_addr->sa_family == AF_INET) {
  70. // Check if interface is en0 which is the wifi connection on the iPhone
  71. // it may also be en1 on your ipad3.
  72. if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
  73. // Get NSString from C String
  74. address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
  75. }
  76. }
  77.  
  78. temp_addr = temp_addr->ifa_next;
  79. }
  80. }
  81.  
  82. // Free memory
  83. freeifaddrs(interfaces);
  84. return address;
  85. }
  86.  
  87. #include <ifaddrs.h>
  88. #include <arpa/inet.h>
  89.  
  90. NSString* ip=@"http://www.whatismyip.org/";
  91. NSURL *url = [[NSURL alloc] initWithString:ip];
  92. NSString *ans = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
  93. NSLog(@"%@",ans);
  94.  
  95. NSString *myIp = [UIDevice localIPAddress];
  96. NSString *myIp = [UIDevice localWiFiIPAddress];
  97. NSString *myIp = [UIDevice whatismyipdotcom]; // is using @aamritrao solution
  98.  
  99. bool success;
  100. struct ifaddrs *addrs;
  101. const struct ifaddrs *cursor;
  102. const struct sockaddr_dl *dlAddr;
  103. const uint8_t *base;
  104. int i;
  105.  
  106. success = getifaddrs(&addrs) == 0;
  107. if (success) {
  108. cursor = addrs;
  109. while (cursor != NULL) {
  110. if ( (cursor->ifa_addr->sa_family == AF_LINK)
  111. && (((const struct sockaddr_dl *) cursor->ifa_addr)->sdl_type ==IFT_ETHER)
  112. ) {
  113. dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
  114. // fprintf(stderr, " sdl_nlen = %dn", dlAddr->sdl_nlen);
  115. // fprintf(stderr, " sdl_alen = %dn", dlAddr->sdl_alen);
  116. base = (const uint8_t *) &dlAddr->sdl_data[dlAddr->sdl_nlen];
  117. printf(" MAC address ");
  118. for (i = 0; i < dlAddr->sdl_alen; i++) {
  119. if (i != 0) {
  120. printf(":");
  121. }
  122. printf("%02x", base[i]);
  123. }
  124. printf("n");
  125. }
  126. cursor = cursor->ifa_next;
  127. }
  128. }
Add Comment
Please, Sign In to add comment