static Func getipaddresses (), [String] {
var addresses = [String] ()
Get List of all interfaces in 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
}
Reference: How to get Ip address in Swift
Swift gets iphone native IP address