How to retrieve the device's IP address without using any third-party libraries using Swift 3 programming language? I have used the following code in order to get the IP address:
func getIPAddress() -> String? {
var address : String?
var ifaddr : UnsafeMutablePointer<ifaddrs> = nil
if getifaddrs(&ifaddr) == 0 {
var ptr = ifaddr
while ptr != nil {
defer { ptr = ptr.memory.ifa_next }
let interface = ptr.memory
let addrFamily = interface.ifa_addr.memory.sa_family
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {
if let name = String.fromCString(interface.ifa_name) where name == "en0" {
var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
getnameinfo(interface.ifa_addr, socklen_t(interface.ifa_addr.memory.sa_len),
&hostname, socklen_t(hostname.count),
nil, socklen_t(0), NI_NUMERICHOST)
address = String.fromCString(hostname)
}
}
}
freeifaddrs(ifaddr)
}
return address
}
But the UnsafeMutablePointer<ifaddrs>
syntax is not working. It throws a syntax error. Do I need to import a framework to try to help me?
To get IPAddress for wifi , wired, and cellular - swift 5func getIPAddress() -> String { var address: String? var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil if getifaddrs(&ifaddr) == 0 { var ptr = ifaddr while ptr !=
Find your IP address on an iOS deviceOn the Home screen, tap Settings. Tap Wi-Fi. Tap the information icon (blue i, in a circle) to the right of the network name (eduroam). Tap DHCP and the IP Address will be listed as the first line item below the heading.
Can an iPad IP address be traced? No, an iPad's IP address cannot be traced back to you. However, your ISP and other sites may view your IP address to track your Internet history or other online activity. If you want to ensure your iPad's IP address is completely hidden, try a VPN app or proxy app to hide your IP.
I did following things in order to get the exact IP address of the device. Since I want to include the updated code to get IP address using Swift 3, I am posting the answer here. Referred from Swift - Get device's IP Address
Add #include<ifaddrs.h>
in your bridging header
Create following function in order to get the IP Address.
func getIP()-> String? {
var address: String?
var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
if getifaddrs(&ifaddr) == 0 {
var ptr = ifaddr
while ptr != nil {
defer { ptr = ptr?.pointee.ifa_next } // memory has been renamed to pointee in swift 3 so changed memory to pointee
let interface = ptr?.pointee
let addrFamily = interface?.ifa_addr.pointee.sa_family
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {
if let name: String = String(cString: (interface?.ifa_name)!), name == "en0" { // String.fromCString() is deprecated in Swift 3. So use the following code inorder to get the exact IP Address.
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
getnameinfo(interface?.ifa_addr, socklen_t((interface?.ifa_addr.pointee.sa_len)!), &hostname, socklen_t(hostname.count), nil, socklen_t(0), NI_NUMERICHOST)
address = String(cString: hostname)
}
}
}
freeifaddrs(ifaddr)
}
return address
}
In order to get the IP Address, print(getIP())
For verification: -> Goto Settings -> Wi-Fi -> Click i symbol -> you can check your device IP Address.
OUTPUT SCREENSHOT:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With