title: How to include ifaddrs.h
body: <p>i have the following issue:</p>
  
  <p>i wanna to get the ip address from my app iOS.</p>
  
  <p><a href="https://i.stack.imgur.com/pBfvi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pBfvi.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/pHXqk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pHXqk.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/FQkva.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FQkva.png" alt="enter image description here"></a></p>
  
  <p>But I get error when trying to import from swift</p>
  
  <p>CODE:</p>
  
  <p>module.modulemap</p>
  
  <pre><code>module ifaddrs [system]  [extern_c] {
      header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/ifaddrs.h"
      export *
  }
  
  module net [system]  [extern_c] {
  
      module types {
          header "/usr/include/sys/types.h"
          export *
      }
  
      module if_dl {
  
          header "/usr/include/net/if_dl.h"
          export *
      }
  
  }
  </code></pre>
  
  <p>Bridging-header</p>
  
  <pre><code>#include &lt;ifaddrs.h&gt;
  #include &lt;net/if_dl.h&gt;
  </code></pre>
  
  <p>import and functions</p>
  
  <pre><code>import ifaddrs
  
  func getIFAddresses() -&gt; [String] {
      var addresses = [String]()
  
      // Get list of all interfaces on the local machine:
      var ifaddr : UnsafeMutablePointer&lt;ifaddrs&gt; = nil
  
      if getifaddrs(&amp;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 &amp; (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(&amp;addr, socklen_t(addr.sa_len), &amp;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
  }
  
  func getDataUsage() -&gt; [UInt32] {
      var ifaddr : UnsafeMutablePointer&lt;ifaddrs&gt; = nil
      var networkData: UnsafeMutablePointer&lt;if_data&gt;! = nil
      var wifiDataSent:UInt32 = 0
      var wifiDataReceived:UInt32 = 0
      var wwanDataSent:UInt32 = 0
      var wwanDataReceived:UInt32 = 0
  
      if getifaddrs(&amp;ifaddr) == 0 {
          for (var ptr = ifaddr; ptr != nil; ptr = ptr.memory.ifa_next) {
              let name = String.fromCString(ptr.memory.ifa_name)
              let flags = Int32(ptr.memory.ifa_flags)
  
              var addr = ptr.memory.ifa_addr.memory
  
              if addr.sa_family == UInt8(AF_LINK) {
                  if name?.hasPrefix("en") == true {
                      networkData = unsafeBitCast(ptr.memory.ifa_data, UnsafeMutablePointer&lt;if_data&gt;.self)
  
                      wifiDataSent += networkData.memory.ifi_obytes
                      wifiDataReceived += networkData.memory.ifi_ibytes
                  }
  
                  if name?.hasPrefix("pdp_ip") == true {
                      networkData = unsafeBitCast(ptr.memory.ifa_data, UnsafeMutablePointer&lt;if_data&gt;.self)
  
                      wwanDataSent += networkData.memory.ifi_obytes
                      wwanDataReceived += networkData.memory.ifi_ibytes
                  }
              }
          }
  
          freeifaddrs(ifaddr)
      }
  
      return [wifiDataSent, wifiDataReceived, wwanDataSent, wwanDataReceived]
  }
  </code></pre>