Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

To get IPAddress for wifi , wired, and cellular

func getIPAddress() -> 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 }

            guard let interface = ptr?.pointee else { return "" }
            let addrFamily = interface.ifa_addr.pointee.sa_family
            if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {

                // wifi = ["en0"]
                // wired = ["en2", "en3", "en4"]
                // cellular = ["pdp_ip0","pdp_ip1","pdp_ip2","pdp_ip3"]

                let name: String = String(cString: (interface.ifa_name))
                if  name == "en0" || name == "en2" || name == "en3" || name == "en4" || name == "pdp_ip0" || name == "pdp_ip1" || name == "pdp_ip2" || name == "pdp_ip3" {
                    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 ?? ""
}
Comment

PREVIOUS NEXT
Code Example
Swift :: Swift Named Tuple 
Swift :: swift 5 progress bar height 
Swift :: ios tableview hide empty cells 
Swift :: swift conditional statements 
Swift :: create a dictionary in swift 
Swift :: Swift Iterate Over a Set 
Swift :: xcode button center text 
Swift :: access tuple elements 
Swift :: special symbol ios swift 
Swift :: Swift break and continue Inside Nested Loop 
Swift :: Declare Constants in Swift 
Swift :: Swift Loop Statements 
Swift :: Swift Benefits of Using Functions 
Swift :: Swift Find Number of Array Elements 
Swift :: How to Hide Password in Text field Swift 
Swift :: swift loop site:stackoverflow.com 
Swift :: Swift print() with terminator 
Swift :: swift closure 
Swift :: sprite kitYourNextScene 
Ruby :: ruby json parse symbolize_keys 
Ruby :: rails generate model 
Ruby :: index name is too long rails 
Ruby :: activerecord less than 
Ruby :: add key and value to hash ruby 
Ruby :: edit file terminal mac 
Ruby :: rails hidden field default value 
Ruby :: ruby prepend array 
Ruby :: text_field_tag rails 
Ruby :: create_enum in rails 7 
Ruby :: jupyter notebook ruby 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =