2015-11-03 57 views
5

對於我的項目,我需要獲取IPhone的公共IP地址,有很多示例可用,它們使用外部/第三方URL顯示公共IP地址。我只是想知道如何提取IPhones的IP地址,而無需使用另一個URL的幫助。如何在不使用第三方URL的情況下獲取IPhone的公共IP地址

注: - 我用這一個,但只顯示本地IP地址,我需要

func getIPAddress() -> [String] { 
    var addresses = [String]() 

    // Get list of all interfaces on the local machine: 
    var ifaddr : UnsafeMutablePointer<ifaddrs> = nil 
    if getifaddrs(&ifaddr) == 0 { 

     // For each interface ... 
     for (var ptr = ifaddr; ptr != nil; ptr = ptr.memory.ifa_next) { 
      let flags = Int32(ptr.memory.ifa_flags) 
      var addr = ptr.memory.ifa_addr.memory 

      // Check for running IPv4, IPv6 interfaces. Skip the loopback interface. 
      if (flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) { 
       if addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) { 

        // Convert interface address to a human readable string: 
        var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0) 
        if (getnameinfo(&addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count), 
         nil, socklen_t(0), NI_NUMERICHOST) == 0) { 
          if let address = String.fromCString(hostname) { 
           addresses.append(address) 
          } 
        } 
       } 
      } 
     } 
     freeifaddrs(ifaddr) 
    } 

    return addresses 
} 

我曾經使用頭文件這一個在我的SWIFT項目的公網IP地址,它會產生本地IP

+1

你試過了嗎? http://stackoverflow.com/questions/7072989/iphone-ipad-osx-how-to-get-my-ip-address-programmatically?lq=1 –

+0

這個鏈接可能會幫助你 - http://stackoverflow.com/問題/ 30748480/swift-get-devices-ip-address – Vinayk

+0

找到一些Objective-C代碼,你能否給我一個很快的代碼? :) –

回答

1
import Darwin 

var hostName = [Int8](count: 255, repeatedValue: 0) 
gethostname(&hostName, hostName.count) 

var hints = addrinfo() 
var res = UnsafeMutablePointer<addrinfo>() 
var p = UnsafeMutablePointer<addrinfo>() 
var p4 = UnsafeMutablePointer<sockaddr_in>() 
var p6 = UnsafeMutablePointer<sockaddr_in6>() 

hints.ai_flags = AI_ALL 
hints.ai_family = PF_UNSPEC 
hints.ai_socktype = SOCK_STREAM 
var ipstr = [CChar](count: Int(INET6_ADDRSTRLEN), repeatedValue: 0) 

print("List of IP addresses on host \(String.fromCString(hostName)!)\n") 

if getaddrinfo(hostName, "0", &hints, &res) == 0 { 

    var ipFamily = "" 
    var s: Int32 = 0 
    var port: in_port_t = 0 
    for(p = res; p != nil; p = p.memory.ai_next) { 

     switch p.memory.ai_family { 
     case PF_INET: 
      p4 = UnsafeMutablePointer<sockaddr_in>(p.memory.ai_addr) 
      inet_ntop(AF_INET, &p4.memory.sin_addr, &ipstr, socklen_t(INET6_ADDRSTRLEN)) 
      ipFamily = "IPv4" 
      port = p4.memory.sin_port.bigEndian 
     case PF_INET6: 
      p6 = UnsafeMutablePointer<sockaddr_in6>(p.memory.ai_addr) 
      inet_ntop(AF_INET6, &p6.memory.sin6_addr, &ipstr, socklen_t(INET6_ADDRSTRLEN)) 
      ipFamily = "IPv6" 
      port = p6.memory.sin6_port.bigEndian 
     default: 

      break 
     } 
     print("\t\(ipFamily):", String.fromCString(ipstr)!) 

    } 
    // free unmanaged memmory !!!!! 
    freeaddrinfo(res) 
} 

我自己的電腦

List of IP addresses on host Ivos-MacBook.local 

    IPv6: fe80::225:ff:fe40:9c72 
    IPv6: 2a02:130:200:1350:225:ff:fe40:9c72 
    IPv4: 192.168.1.106 

外部IP上打印,你需要外部幫助服務(不是迅捷具體:-))

+0

感謝您的代碼,但問題是這顯示手機IP,我需要獲得Iphone公共IP,IP位置和相關數據。 :( –

+0

你說的'公共'IP代表什麼意思?代碼列出了所有的公共IP(通過它可以在你的局域網中訪問該設備的IP),它接近你正在尋找如何獲得設備的外部IP的方式。因爲我很傷心,你需要外部服務的幫助。我建議你閱讀https://en.wikipedia.org/wiki/Network_address_translation – user3441734

+0

是的,我不想使用外部服務,我需要一個內部編碼解決方案,這就是問題所在,因爲不能依靠外部服務 –

相關問題