iOS용 NSBundle.infoDictionary 래핑
import Foundation
import SystemConfiguration
//MARK: - Convertors
infix operator >> { associativity right precedence 90 }
func >> <T, R>(x: T, f: (T) -> R) -> R {
return f(x)
}
private func string(object: AnyObject?) -> String? {
return object as? String
}
private func stringToInt(object: AnyObject?) -> Int? {
return Int(object as! String)
}
private func int(object: AnyObject?) -> Int? {
return object as? Int
}
private func array<T>(object: AnyObject?) -> Array<T>? {
return object as? Array
}
public struct AppInfo {
private static let bundleInfo = NSBundle.mainBundle().infoDictionary!
public static var macAddressSeparator = ":"
public static var CFBundleIdentifier: String? {
return bundleInfo["CFBundleIdentifier"] >> string
}
public static var DTPlatformName: String? {
return bundleInfo["DTPlatformName"] >> string
}
public static var UIMainStoryboardFile: String? {
return bundleInfo["UIMainStoryboardFile"] >> string
}
public static var CFBundleVersion: Int? {
return bundleInfo["CFBundleVersion"] >> stringToInt
}
public static var CFBundleSignature: String? {
return bundleInfo["CFBundleSignature"] >> string
}
public static var CFBundleExecutable: String? {
return bundleInfo["CFBundleExecutable"] >> string
}
public static var LSRequiresIPhoneOS: Int? {
return bundleInfo["LSRequiresIPhoneOS"] >> int
}
public static var CFBundleName: String? {
return bundleInfo["CFBundleName"] >> string
}
public static var CFBundleDisplayName: String? {
return bundleInfo["CFBundleDisplayName"] >> string
}
public static var UILaunchStoryboardName: String? {
return bundleInfo["UILaunchStoryboardName"] >> string
}
public static var CFBundleSupportedPlatforms: Array<String>? {
return bundleInfo["CFBundleSupportedPlatforms"] >> array
}
public static var CFBundlePackageType: String? {
return bundleInfo["CFBundlePackageType"] >> string
}
public static var CFBundleNumericVersion: Int? {
return bundleInfo["CFBundleNumericVersion"] >> int
}
public static var CFBundleInfoDictionaryVersion: String? {
return bundleInfo["CFBundleInfoDictionaryVersion"] >> string
}
public static var UIRequiredDeviceCapabilities: Array<String>? {
return bundleInfo["UIRequiredDeviceCapabilities"] >> array
}
public static var UISupportedInterfaceOrientations: Array<String>? {
return bundleInfo["UISupportedInterfaceOrientations"] >> array
}
public static var CFBundleInfoPlistURL: String? {
return bundleInfo["CFBundleInfoPlistURL"] >> string
}
public static var CFBundleDevelopmentRegion: String? {
return bundleInfo["CFBundleDevelopmentRegion"] >> string
}
public static var DTSDKName: String? {
return bundleInfo["DTSDKName"] >> string
}
public static var UIDeviceFamily: Array<Int>? {
return bundleInfo["UIDeviceFamily"] >> array
}
public static var CFBundleShortVersionString: String? {
return bundleInfo["CFBundleShortVersionString"] >> string
}
public static var ipAddresses: [String] = {
var addresses = [String]()
// Get list of all interfaces on the local machine:
var ifaddr : UnsafeMutablePointer<ifaddrs> = nil
guard getifaddrs(&ifaddr) == 0 else {
return addresses
}
// For each interface ...
var ptr = ifaddr
while (ptr != nil) {
let flags = Int32(ptr.memory.ifa_flags)
var addr = ptr.memory.ifa_addr.memory
// Check for running IPv4, IPv6 interfaces. Skip the loopback interface.
guard (flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) else {
continue
}
guard addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) else {
continue
}
// Convert interface address to a human readable string:
var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
guard getnameinfo(&addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count), nil, socklen_t(0), NI_NUMERICHOST) == 0,
let address = String.fromCString(hostname) else {
continue
}
addresses.append(address)
ptr = ptr.memory.ifa_next
}
freeifaddrs(ifaddr)
return addresses
}()
public static var ipAddress: String = {
let addresses = AppInfo.ipAddresses
for address in addresses {
if address.characters.contains(".") {
return address
}
}
return addresses.first!
}()
}
유승훈 7년전 질문