文章目录
常用的获取设备信息的方法(已更新为Swift版),建议写在UIDevice 分类中,方便全局调用。
一、获取APP名称
/// 获取app名称
public var kAppName: String? {
return Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String
}
二、APP版本
/// 获取app版本
public var kAppVersion: String? {
return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}
三、APP的BundleID
/// 获取bundle ID
public var KAppBundleID: String? {
return Bundle.main.object(forInfoDictionaryKey: "CFBundleIdentifier") as? String
}
四、APP build版本
// 当前应用版本号码 int类型
let appCurVersionName = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String
print("当前应用版本号码:\(appCurVersionName ?? "")")
五、设备名称
/// 设备昵称
public var KDeviceName: String? {
return UIDevice.current.name
}
六、设备系统名称
/// 设备系统名称
public var KDeviceSystemName: String? {
return UIDevice.current.systemName
}
七、设备系统版本
/// 设备系统版本
public var kDeviceSystemVersion: String? {
return UIDevice.current.systemVersion
}
八、设备类型
/// 设备类型
public var kDeviceVersion: String? {
var systemInfo = utsname()
uname(&systemInfo)
let machineMirror = Mirror(reflecting: systemInfo.machine)
let identifier = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8, value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}
return identifier
}
九、设备运营商IP
/// 获取设备运营商IP(联通/移动/电信的移动IP)
public var kDeviceIP: String? {
var addresses = [String]()
var ifaddr: UnsafeMutablePointer<ifaddrs>?
if getifaddrs(&ifaddr) == 0 {
var ptr = ifaddr
while ptr != nil {
let flags = Int32(ptr!.pointee.ifa_flags)
var addr = ptr!.pointee.ifa_addr.pointee
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) {
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
if getnameinfo(&addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count), nil, socklen_t(0), NI_NUMERICHOST) == 0 {
if let address = String(validatingUTF8: hostname) {
addresses.append(address)
}
}
}
}
ptr = ptr!.pointee.ifa_next
}
freeifaddrs(ifaddr)
}
return addresses.first
}
十、WiFi IP
/// 获取WiFi的IP
public var kWifiIP: String? {
var address: String?
var ifaddr: UnsafeMutablePointer<ifaddrs>?
guard getifaddrs(&ifaddr) == 0 else {
return nil
}
guard let firstAddr = ifaddr else {
return nil
}
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 addr = interface.ifa_addr.pointee
var hostName = [CChar](repeating: 0, count: Int(NI_MAXHOST))
getnameinfo(&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
}
十一、设备唯一标识符
// 设备唯一标识符
let identifierStr: String = UIDevice.current.identifierForVendor?.uuidString ?? ""
print("设备唯一标识符:\(identifierStr)")
十二、地方型号
// 地方型号 (国际化区域名称)
let localPhoneModel: String = UIDevice.current.localizedModel
print("国际化区域名称:\(localPhoneModel)")
十三、设备物理信息
// 设备物理信息
let rect: CGRect = UIScreen.main.bounds
let size: CGSize = rect.size
let width: CGFloat = size.width
let height: CGFloat = size.height
print("物理尺寸:\(width) × \(height)")
let scale_screen: CGFloat = UIScreen.main.scale
print("分辨率是:\(width*scale_screen) × \(height*scale_screen)")
十四、系统时区
let systemTimeZone: TimeZone = TimeZone.current
print("systemTimeZone -->\(systemTimeZone)")
十五、是否锁屏
UIApplication.shared.isProtectedDataAvailable
如果返回true
表示设备被锁定,否则未锁定
十六、电池电量
启用电池监控
UIDevice.current.isBatteryMonitoringEnabled = true
返回电池电量百分比
let float = UIDevice.current.batteryLevel
要监控设备电池电量,您可以为以下内容添加观察者 UIDevice.batteryLevelDidChangeNotification
:
NotificationCenter.default.addObserver(self, selector: #selector(batteryLevelDidChange), name: UIDevice.batteryLevelDidChangeNotification, object: nil)
@objc func batteryLevelDidChange(_ notification: Notification) {
print(batteryLevel)
}
验证电池状态:
UIDevice.current.batteryState
case .unknown // "The battery state for the device cannot be determined."
case .unplugged // "The device is not plugged into power; the battery is discharging"
case .charging // "The device is plugged into power and the battery is less than 100% charged."
case .full // "The device is plugged into power and the battery is 100% charged."
电池状态观察者UIDevice.batteryStateDidChangeNotification
:
NotificationCenter.default.addObserver(self, selector: #selector(batteryStateDidChange), name: UIDevice.batteryStateDidChangeNotification, object: nil)
@objc func batteryStateDidChange(_ notification: Notification) {
switch batteryState {
case .unplugged, .unknown:
print("not charging")
case .charging, .full:
print("charging or full")
}
}