Last active
August 7, 2025 12:02
-
-
Save camyoh/aa341e4e40afaa40c84813d899369566 to your computer and use it in GitHub Desktop.
Get the IP on iphone, swift 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GetIPAddress.swift | |
// | |
// Created by @camyoh | |
// | |
// MIT License | |
// | |
// Copyright (c) 2025 @camyoh | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
let ipName = getIpAddress() ?? "" | |
func getIpAddress() -> String? { | |
var address : String? | |
// Get list of all interfaces on the local machine: | |
var ifaddr : UnsafeMutablePointer<ifaddrs>? | |
guard getifaddrs(&ifaddr) == 0 else { return nil } | |
guard let firstAddr = ifaddr else { return nil } | |
// For each interface ... | |
for ifptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) { | |
let interface = ifptr.pointee | |
// Check for IPv4 or IPv6 interface: | |
let addrFamily = interface.ifa_addr.pointee.sa_family | |
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) { | |
// Check interface name: | |
let name = String(cString: interface.ifa_name) | |
if name == "en0" { | |
// Convert interface address to a human readable string: | |
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) | |
} else if (name == "pdp_ip0" || name == "pdp_ip1" || name == "pdp_ip2" || name == "pdp_ip3") { | |
// Convert interface address to a human readable string: | |
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(1), NI_NUMERICHOST) | |
address = String(cString: hostname) | |
} | |
} | |
} | |
freeifaddrs(ifaddr) | |
return address | |
} |
great job!
@camyoh Thanks your code :)
Can I use your code in my commercial app?
Please let me know how I can use the code for commercial distribution.
@seyoung-hyun Yes, you’re welcome to use the code in your commercial app!
Just make sure to keep any original license or attribution if it’s included.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well done!