Swift gets iphone native IP address

Source: Internet
Author: User
Tags get ip

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.