0

i have the following issue:

i wanna to get the ip address from my app iOS.

enter image description here

enter image description here

enter image description here

But I get error when trying to import from swift

CODE:

module.modulemap

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 *
    }

}

Bridging-header

#include <ifaddrs.h>
#include <net/if_dl.h>

import and functions

import ifaddrs

func getIFAddresses() -> [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
}

func getDataUsage() -> [UInt32] {
    var ifaddr : UnsafeMutablePointer<ifaddrs> = nil
    var networkData: UnsafeMutablePointer<if_data>! = nil
    var wifiDataSent:UInt32 = 0
    var wifiDataReceived:UInt32 = 0
    var wwanDataSent:UInt32 = 0
    var wwanDataReceived:UInt32 = 0

    if getifaddrs(&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<if_data>.self)

                    wifiDataSent += networkData.memory.ifi_obytes
                    wifiDataReceived += networkData.memory.ifi_ibytes
                }

                if name?.hasPrefix("pdp_ip") == true {
                    networkData = unsafeBitCast(ptr.memory.ifa_data, UnsafeMutablePointer<if_data>.self)

                    wwanDataSent += networkData.memory.ifi_obytes
                    wwanDataReceived += networkData.memory.ifi_ibytes
                }
            }
        }

        freeifaddrs(ifaddr)
    }

    return [wifiDataSent, wifiDataReceived, wwanDataSent, wwanDataReceived]
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Miller Mosquera
  • 119
  • 4
  • 14
  • Please do not post your code as images. Please copy and paste the actual relevant code as text into your question. Add errors as comments if needed to clearly indicate where the error occurs and its message. – rmaddy Nov 11 '16 at 16:15
  • That code looks familiar http://stackoverflow.com/a/25627545/1187415 :) – Martin R Nov 11 '16 at 16:39
  • If this is an iOS app compiled in Xcode then you don't need a module map, only the bridging header file, and you should remove the `import ifaddrs`. Or are you trying to compile a framework? – Martin R Nov 11 '16 at 16:42

0 Answers0