如何在swift中获取IP地址

时间:2021-11-14 02:57:24

How do i get my local IpAddress?

我如何获得我的本地IpAddress?

I tried to use this Obj C example: how to get ip address of iphone programmatically

我尝试使用这个Obj C示例:如何以编程方式获取iphone的ip地址

When i get to the function getifaddrs() i can't get any further. i can't use the function.

当我到达getifaddrs()函数时,我无法继续。我不能使用该功能。

Is there an alternative way to do this, or am I approaching this the wrong way?

有没有其他方法可以做到这一点,或者我是以错误的方式接近这个?

3 个解决方案

#1


40  

As it turned out in the discussion, OP needs the interface address on a Mac and not on an iOS device as I thought initially. The code referenced in the question checks for the interface name "en0", which is the WiFi interface on the iPhone. On a Mac it makes more sense to check for any "up-and-running" interface instead. Therefore I have rewritten the answer. It is now a Swift translation of the code in Detect any connected network.

正如在讨论中所说,OP需要Mac上的接口地址而不是我最初想到的iOS设备上的接口地址。问题中引用的代码检查接口名称“en0”,即iPhone上的WiFi接口。在Mac上,检查任何“正在运行”的界面更有意义。因此我改写了答案。它现在是检测任何连接网络中的代码的Swift转换。


getifaddrs() is defined in <ifaddrs.h>, which is not included by default. Therefore you have to create a bridging header and add

getifaddrs()在 中定义,默认情况下不包含。因此,您必须创建桥接标头并添加

#include <ifaddrs.h>

The following function returns an array with the names of all local "up-and-running" network interfaces.

以下函数返回一个数组,其中包含所有本地“正在运行”的网络接口的名称。

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 ...
        var ptr = ifaddr
        while ptr != nil {
            defer { ptr = ptr.memory.ifa_next } 

            let flags = Int32(ptr.memory.ifa_flags)
            let 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(ptr.memory.ifa_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
}

Update for Swift 3: In addition to adopting the code to the many changes in Swift 3, iterating over all interfaces can now use the new generalized sequence() function:

更新Swift 3:除了对Swift 3中的许多更改采用代码之外,迭代所有接口现在可以使用新的通用sequence()函数:

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

    // Get list of all interfaces on the local machine:
    var ifaddr : UnsafeMutablePointer<ifaddrs>?
    guard getifaddrs(&ifaddr) == 0 else { return [] }
    guard let firstAddr = ifaddr else { return [] }

    // For each interface ...
    for ptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) {
        let flags = Int32(ptr.pointee.ifa_flags)
        let addr = ptr.pointee.ifa_addr.pointee

        // 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](repeating: 0, count: Int(NI_MAXHOST))
                if (getnameinfo(ptr.pointee.ifa_addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),
                                nil, socklen_t(0), NI_NUMERICHOST) == 0) {
                    let address = String(cString: hostname)
                    addresses.append(address)
                }
            }
        }
    }

    freeifaddrs(ifaddr)
    return addresses
}

#2


2  

Answering to Is there an alternative way to getifaddrs() for getting the ip-address?

回答有没有另一种方法来获取IP地址的getifaddrs()?

Yes, there is an alternative way. You can also get the ip address by utilizing ioctl(). The cleanest way would be doing it in C and then wrapping it up in Swift. So consider this:

是的,还有另一种方法。您还可以使用ioctl()获取IP地址。最干净的方法是在C中进行,然后将其包装在Swift中。所以考虑一下:

Create a C Source File (Xcode should create it together with a .h) and remember adding the header into your project's bridging header, or into umbrella-header if you have a cocoa touch framework.

创建一个C源文件(Xcode应该与.h一起创建)并记住将标题添加到项目的桥接头中,或者如果你有一个可可触摸框架,则添加到伞形头文件中。

Add the following into your .c source and method's declaration into .h:

将以下内容添加到.c源代码和方法的声明中.h:

#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>

int _interfaceAddressForName(char* interfaceName, struct sockaddr* interfaceAddress) {

    struct ifreq ifr;
    int fd = socket(AF_INET, SOCK_DGRAM, 0);
    ifr.ifr_addr.sa_family = AF_INET;

    strncpy(ifr.ifr_name, interfaceName, IFNAMSIZ-1);

    int ioctl_res;
    if ( (ioctl_res = ioctl(fd, SIOCGIFADDR, &ifr)) < 0){
        return ioctl_res;
    }

    close(fd);
    memcpy(interfaceAddress, &ifr.ifr_addr, sizeof(struct sockaddr));
    return 0;
}

Your Swift wrapper might look something like:

您的Swift包装器可能类似于:

public enum Error:ErrorType {
    case IOCTLFailed(Int32)
    case StringIsNotAnASCIIString
}

public func interfaceAddress(forInterfaceWithName interfaceName: String) throws -> sockaddr_in {

    guard let cString = interfaceName.cStringUsingEncoding(NSASCIIStringEncoding) else {
        throw Error.StringIsNotAnASCIIString
    }

    let addressPtr = UnsafeMutablePointer<sockaddr>.alloc(1)
    let ioctl_res = _interfaceAddressForName(strdup(cString), addressPtr)
    let address = addressPtr.move()
    addressPtr.dealloc(1)

    if ioctl_res < 0 {
        throw Error.IOCTLFailed(errno)
    } else {
        return unsafeBitCast(address, sockaddr_in.self)
    }
}

Then you can use it in your code like:

然后你可以在你的代码中使用它,如:

let interfaceName = "en0"
do {
    let wlanInterfaceAddress = try interfaceAddress(forInterfaceWithName: interfaceName)
    print(String.fromCString(inet_ntoa(wlanInterfaceAddress.sin_addr))!)
} catch {
    if case Error.IOCTLFailed(let errno) = error where errno == ENXIO {
        print("interface(\(interfaceName)) is not available")
    } else {
        print(error)
    }
}

en0 typically is the interface you need, which stands for WLAN

en0通常是您需要的接口,代表WLAN

If you also need to know available interface names you can utilize if_indextoname():

如果您还需要知道可用的接口名称,可以使用if_indextoname():

public func interfaceNames() -> [String] {

    let MAX_INTERFACES = 128;

    var interfaceNames = [String]()
    let interfaceNamePtr = UnsafeMutablePointer<Int8>.alloc(Int(IF_NAMESIZE))
    for interfaceIndex in 1...MAX_INTERFACES {
        if (if_indextoname(UInt32(interfaceIndex), interfaceNamePtr) != nil){
            if let interfaceName = String.fromCString(interfaceNamePtr) {
                interfaceNames.append(interfaceName)
            }
        } else {
            break
        }
    }

    interfaceNamePtr.dealloc(Int(IF_NAMESIZE))
    return interfaceNames
}

#3


1  

Unfortunately neither of methods will work in iOS 11 since Apple has blocked third-party developer access to MAC addresses. Networking apps developers even filed petition to Apple to revert this policy https://www.change.org/p/apple-save-networking-tools-on-ios-11

不幸的是,由于Apple阻止了第三方开发者访问MAC地址,因此这两种方法都不适用于iOS 11。网络应用程序开发人员甚至向Apple提交请愿书以恢复此政策https://www.change.org/p/apple-save-networking-tools-on-ios-11

#1


40  

As it turned out in the discussion, OP needs the interface address on a Mac and not on an iOS device as I thought initially. The code referenced in the question checks for the interface name "en0", which is the WiFi interface on the iPhone. On a Mac it makes more sense to check for any "up-and-running" interface instead. Therefore I have rewritten the answer. It is now a Swift translation of the code in Detect any connected network.

正如在讨论中所说,OP需要Mac上的接口地址而不是我最初想到的iOS设备上的接口地址。问题中引用的代码检查接口名称“en0”,即iPhone上的WiFi接口。在Mac上,检查任何“正在运行”的界面更有意义。因此我改写了答案。它现在是检测任何连接网络中的代码的Swift转换。


getifaddrs() is defined in <ifaddrs.h>, which is not included by default. Therefore you have to create a bridging header and add

getifaddrs()在 中定义,默认情况下不包含。因此,您必须创建桥接标头并添加

#include <ifaddrs.h>

The following function returns an array with the names of all local "up-and-running" network interfaces.

以下函数返回一个数组,其中包含所有本地“正在运行”的网络接口的名称。

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 ...
        var ptr = ifaddr
        while ptr != nil {
            defer { ptr = ptr.memory.ifa_next } 

            let flags = Int32(ptr.memory.ifa_flags)
            let 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(ptr.memory.ifa_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
}

Update for Swift 3: In addition to adopting the code to the many changes in Swift 3, iterating over all interfaces can now use the new generalized sequence() function:

更新Swift 3:除了对Swift 3中的许多更改采用代码之外,迭代所有接口现在可以使用新的通用sequence()函数:

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

    // Get list of all interfaces on the local machine:
    var ifaddr : UnsafeMutablePointer<ifaddrs>?
    guard getifaddrs(&ifaddr) == 0 else { return [] }
    guard let firstAddr = ifaddr else { return [] }

    // For each interface ...
    for ptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) {
        let flags = Int32(ptr.pointee.ifa_flags)
        let addr = ptr.pointee.ifa_addr.pointee

        // 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](repeating: 0, count: Int(NI_MAXHOST))
                if (getnameinfo(ptr.pointee.ifa_addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),
                                nil, socklen_t(0), NI_NUMERICHOST) == 0) {
                    let address = String(cString: hostname)
                    addresses.append(address)
                }
            }
        }
    }

    freeifaddrs(ifaddr)
    return addresses
}

#2


2  

Answering to Is there an alternative way to getifaddrs() for getting the ip-address?

回答有没有另一种方法来获取IP地址的getifaddrs()?

Yes, there is an alternative way. You can also get the ip address by utilizing ioctl(). The cleanest way would be doing it in C and then wrapping it up in Swift. So consider this:

是的,还有另一种方法。您还可以使用ioctl()获取IP地址。最干净的方法是在C中进行,然后将其包装在Swift中。所以考虑一下:

Create a C Source File (Xcode should create it together with a .h) and remember adding the header into your project's bridging header, or into umbrella-header if you have a cocoa touch framework.

创建一个C源文件(Xcode应该与.h一起创建)并记住将标题添加到项目的桥接头中,或者如果你有一个可可触摸框架,则添加到伞形头文件中。

Add the following into your .c source and method's declaration into .h:

将以下内容添加到.c源代码和方法的声明中.h:

#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>

int _interfaceAddressForName(char* interfaceName, struct sockaddr* interfaceAddress) {

    struct ifreq ifr;
    int fd = socket(AF_INET, SOCK_DGRAM, 0);
    ifr.ifr_addr.sa_family = AF_INET;

    strncpy(ifr.ifr_name, interfaceName, IFNAMSIZ-1);

    int ioctl_res;
    if ( (ioctl_res = ioctl(fd, SIOCGIFADDR, &ifr)) < 0){
        return ioctl_res;
    }

    close(fd);
    memcpy(interfaceAddress, &ifr.ifr_addr, sizeof(struct sockaddr));
    return 0;
}

Your Swift wrapper might look something like:

您的Swift包装器可能类似于:

public enum Error:ErrorType {
    case IOCTLFailed(Int32)
    case StringIsNotAnASCIIString
}

public func interfaceAddress(forInterfaceWithName interfaceName: String) throws -> sockaddr_in {

    guard let cString = interfaceName.cStringUsingEncoding(NSASCIIStringEncoding) else {
        throw Error.StringIsNotAnASCIIString
    }

    let addressPtr = UnsafeMutablePointer<sockaddr>.alloc(1)
    let ioctl_res = _interfaceAddressForName(strdup(cString), addressPtr)
    let address = addressPtr.move()
    addressPtr.dealloc(1)

    if ioctl_res < 0 {
        throw Error.IOCTLFailed(errno)
    } else {
        return unsafeBitCast(address, sockaddr_in.self)
    }
}

Then you can use it in your code like:

然后你可以在你的代码中使用它,如:

let interfaceName = "en0"
do {
    let wlanInterfaceAddress = try interfaceAddress(forInterfaceWithName: interfaceName)
    print(String.fromCString(inet_ntoa(wlanInterfaceAddress.sin_addr))!)
} catch {
    if case Error.IOCTLFailed(let errno) = error where errno == ENXIO {
        print("interface(\(interfaceName)) is not available")
    } else {
        print(error)
    }
}

en0 typically is the interface you need, which stands for WLAN

en0通常是您需要的接口,代表WLAN

If you also need to know available interface names you can utilize if_indextoname():

如果您还需要知道可用的接口名称,可以使用if_indextoname():

public func interfaceNames() -> [String] {

    let MAX_INTERFACES = 128;

    var interfaceNames = [String]()
    let interfaceNamePtr = UnsafeMutablePointer<Int8>.alloc(Int(IF_NAMESIZE))
    for interfaceIndex in 1...MAX_INTERFACES {
        if (if_indextoname(UInt32(interfaceIndex), interfaceNamePtr) != nil){
            if let interfaceName = String.fromCString(interfaceNamePtr) {
                interfaceNames.append(interfaceName)
            }
        } else {
            break
        }
    }

    interfaceNamePtr.dealloc(Int(IF_NAMESIZE))
    return interfaceNames
}

#3


1  

Unfortunately neither of methods will work in iOS 11 since Apple has blocked third-party developer access to MAC addresses. Networking apps developers even filed petition to Apple to revert this policy https://www.change.org/p/apple-save-networking-tools-on-ios-11

不幸的是,由于Apple阻止了第三方开发者访问MAC地址,因此这两种方法都不适用于iOS 11。网络应用程序开发人员甚至向Apple提交请愿书以恢复此政策https://www.change.org/p/apple-save-networking-tools-on-ios-11