iOS 获取系统,APP 部分信息 设备型号 (iphone12)

开放中时长会需要收集一些手机设备信息以及 app 版本信息,列举了比较常见的几种信息,持续更新!


//
//  CXGSystemInfo.swift
//
//  Created by CuiXg on 2018/7/21.
//  Copyright © 2018年 CuiXg. All rights reserved.
//

import UIKit

class CXGSystemInfo {

    /// 手机获取设备名称
    class var deviceName: String {
        return UIDevice.current.name
    }

    /// 手机获取系统名称
    class var systemName: String {
        return UIDevice.current.systemName
    }

    /// 手机系统版本号
    class var systemVersion: String {
        return UIDevice.current.systemVersion
    }

    /// 获取设备唯一标示
    class var deviceUUID: String {
        return getUUID()// UIDevice.current.identifierForVendor?.uuidString
    }

    /// 获取设备型号
    class var deviceModel: String {
        return UIDevice.current.model
    }

    /// 获取 app 名称
    class var appName: String? {
        return Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String
    }

    /// 获取 app 版本号
    class var appVersion: String? {
        return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
    }

    /// 获取 app 构建版本号
    class var appBuildVersion: String? {
        return Bundle.main.infoDictionary?["CFBundleVersion"] as? String
    }

    /// 获取设备具体型号 iPhone *
    class var deviceModelName: 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)))
        }

        switch identifier {

        case "i386": return "Simulator"
        case "x86_64": return "Simulator"

        // iphone 系列
        case "iPhone1,1": return "iPhone 1G"
        case "iPhone1,2": return "iPhone 3G"
        case "iPhone2,1": return "iPhone 3GS"
        case "iPhone3,1": return "iPhone 4 (GSM)"
        case "iPhone3,2": return "Verizon iPhone 4"
        case "iPhone3,3": return "iPhone 4 (CDMA/Verizon/Sprint)"
        case "iPhone4,1": return "iPhone 4S"
        case "iPhone5,1": return "iPhone 5"
        case "iPhone5,2": return "iPhone 5"
        case "iPhone5,3": return "iPhone 5C"
        case "iPhone5,4": return "iPhone 5C"
        case "iPhone6,1": return "iPhone 5S"
        case "iPhone6,2": return "iPhone 5S"
        case "iPhone7,1": return "iPhone 6 Plus"
        case "iPhone7,2": return "iPhone 6"
        case "iPhone8,1": return "iPhone 6s"
        case "iPhone8,2": return "iPhone 6s Plus"
        case "iPhone8,4": return "iPhone SE"
        case "iPhone9,1": return "iPhone 7 (CDMA)"
        case "iPhone9,2": return "iPhone 7 Plus (CDMA)"
        case "iPhone9,3": return "iPhone 7 (GSM)"
        case "iPhone9,4": return "iPhone 7 Plus (GSM)"
        case "iPhone10,1": return "iPhone 8 (CDMA)"
        case "iPhone10,2": return "iPhone 8 Plus (CDMA)"
        case "iPhone10,3": return "iPhone X (CDMA)"
        case "iPhone10,4": return "iPhone 8 (GSM)"
        case "iPhone10,5": return "iPhone 8 Plus (GSM)"
        case "iPhone10,6": return "iPhone X (GSM)"
        case "iPhone11,2": return "iPhone XS"
        case "iPhone11,4": return "iPhone XS Max"
        case "iPhone11,6": return "iPhone XS Max"
        case "iPhone11,8": return "iPhone XS XR"
        case "iPhone12,1": return "iPhone 11"
        case "iPhone12,3": return "iPhone 11 Pro"
        case "iPhone12,5": return "iPhone 11 Pro Max"
        case "iPhone12,8": return "iPhone SE (2nd generation)"
        // iPod 系列
        case "iPod1,1": return "iPod Touch 1G"
        case "iPod2,1": return "iPod Touch 2G"
        case "iPod3,1": return "iPod Touch 3G"
        case "iPod4,1": return "iPod Touch 4G"
        case "iPod5,1": return "iPod Touch 5G"
        case "iPod7,1": return "iPod Touch 6G"
        case "iPod9,1": return "iPod Touch 7G"
        // iPad
        case "iPad1,1": return "iPad"
        case "iPad1,2": return "iPad 3G"
        case "iPad2,1": return "iPad 2 (WiFi)"
        case "iPad2,2": return "iPad 2 (GSM)"
        case "iPad2,3": return "iPad 2 (CDMA)"
        case "iPad2,4": return "iPad 2 (32nm)"
        case "iPad2,5": return "iPad Mini (WiFi)"
        case "iPad2,6": return "iPad Mini (GSM)"
        case "iPad2,7": return "iPad Mini (CDMA)"
        case "iPad3,1": return "iPad 3(WiFi)"
        case "iPad3,2": return "iPad 3(CDMA)"
        case "iPad3,3": return "iPad 3(4G)"
        case "iPad3,4": return "iPad 4 (WiFi)"
        case "iPad3,5": return "iPad 4 (4G)"
        case "iPad3,6": return "iPad 4 (CDMA)"
        case "iPad4,1": return "iPad Air"
        case "iPad4,2": return "iPad Air"
        case "iPad4,3": return "iPad Air"
        case "iPad4,4": return "iPad Mini 2"
        case "iPad4,5": return "iPad Mini 2"
        case "iPad4,6": return "iPad Mini 2"
        case "iPad4,7": return "iPad Mini 3"
        case "iPad4,8": return "iPad Mini 3"
        case "iPad4,9": return "iPad Mini 3"
        case "iPad5,1": return "iPad Mini 4"
        case "iPad5,2": return "iPad Mini 4"
        case "iPad5,3": return "iPad Air 2"
        case "iPad5,4": return "iPad Air 2"
        case "iPad6,3", "iPad6,4": return "iPad Pro (9.7-inch)"
        case "iPad6,7", "iPad6,8": return "iPad Pro (12.9-inch)"
        case "iPad6,11": return "iPad 5"
        case "iPad6,12": return "iPad 5"
        case "iPad7,1": return "iPad PRO 2 (12.9)"
        case "iPad7,2": return "iPad PRO 2 (12.9)"
        case "iPad7,3": return "iPad PRO (10.5)"
        case "iPad7,4": return "iPad PRO (10.5)"
        case "iPad7,5": return "iPad (6th generation)"
        case "iPad7,6": return "iPad (6th generation)"
        case "iPad7,11", "iPad7,12": return "iPad (7th generation)"
        case "iPad11,6", "iPad11,7": return "iPad (8th generation)"
        case "iPad8,1", "iPad8,2", "iPad8,3", "iPad8,4": return "iPad Pro (11-inch)"
        case "iPad8,5", "iPad8,6", "iPad8,7", "iPad8,8": return "iPad Pro (12.9-inch) (3rd generation)"
        case "iPad8,9", "iPad8,10": return "iPad Pro (11-inch) (2nd generation)"
        case "iPad8,11", "iPad8,12": return "iPad Pro (12.9-inch) (4th generation)"
        case "iPad11,1", "iPad11,2": return "iPad mini (5th generation)"
        case "iPad11,3", "iPad11,4": return "iPad Air (3rd generation)"
        case "iPad13,1", "iPad13,2": return "iPad Air (4th generation)"

        default: return identifier
        }
    }

}

/// 获取app唯一标志 UUID
extension QWSystemInfo {

    fileprivate class func getUUID() -> String {

        // F1E477A4-2904-4ECD-8F34-55ED7AF4CF6B
        if let uuid = QWSystemInfo.keyChainReadData(identifier: "com.qiuwen.qiuwenios.uuid") as? String {
            return uuid
        }else {
            if let uuid = UIDevice.current.identifierForVendor?.uuidString {
                if QWSystemInfo.keyChainSaveData(data: uuid, withIdentifier: "com.qiuwen.qiuwenios.uuid") {
                    return uuid
                }
            }
        }
        return "simulator"
    }

    // MARK: 创建查询条件
    fileprivate class func createQuaryMutableDictionary(identifier: String) -> NSMutableDictionary {
        // 创建一个条件字典
        let keychainQuaryMutableDictionary = NSMutableDictionary.init(capacity: 0)
        // 设置条件存储的类型
        keychainQuaryMutableDictionary.setValue(kSecClassGenericPassword, forKey: kSecClass as String)
        // 设置存储数据的标记
        keychainQuaryMutableDictionary.setValue(identifier, forKey: kSecAttrService as String)
        keychainQuaryMutableDictionary.setValue(identifier, forKey: kSecAttrAccount as String)
        // 设置数据访问属性
        keychainQuaryMutableDictionary.setValue(kSecAttrAccessibleAfterFirstUnlock, forKey: kSecAttrAccessible as String)
        // 返回创建条件字典
        return keychainQuaryMutableDictionary
    }

    // MARK: 存储数据
    fileprivate class func keyChainSaveData(data: Any, withIdentifier identifier: String) -> Bool {
        // 获取存储数据的条件
        let keyChainSaveMutableDictionary = self.createQuaryMutableDictionary(identifier: identifier)
        // 删除旧的存储数据
        SecItemDelete(keyChainSaveMutableDictionary)
        // 设置数据
        keyChainSaveMutableDictionary.setValue(NSKeyedArchiver.archivedData(withRootObject: data), forKey: kSecValueData as String)
        // 进行存储数据
        let saveState = SecItemAdd(keyChainSaveMutableDictionary, nil)
        if saveState == noErr {
            return true
        }
        return false
    }

    // MARK: 更新数据
    fileprivate class func keyChainUpdata(data: Any, withIdentifier identifier: String) -> Bool {
        // 获取更新的条件
        let keyChainUpdataMutableDictionary = self.createQuaryMutableDictionary(identifier: identifier)
        // 创建数据存储字典
        let updataMutableDictionary = NSMutableDictionary.init(capacity: 0)
        // 设置数据
        updataMutableDictionary.setValue(NSKeyedArchiver.archivedData(withRootObject: data), forKey: kSecValueData as String)
        // 更新数据
        let updataStatus = SecItemUpdate(keyChainUpdataMutableDictionary, updataMutableDictionary)
        if updataStatus == noErr {
            return true
        }
        return false
    }

    // MARK: 获取数据
    fileprivate class func keyChainReadData(identifier: String)-> Any {
        var idObject: Any?
        // 获取查询条件
        let keyChainReadmutableDictionary = self.createQuaryMutableDictionary(identifier: identifier)
        // 提供查询数据的两个必要参数
        keyChainReadmutableDictionary.setValue(kCFBooleanTrue, forKey: kSecReturnData as String)
        keyChainReadmutableDictionary.setValue(kSecMatchLimitOne, forKey: kSecMatchLimit as String)
        // 创建获取数据的引用
        var queryResult: AnyObject?
        // 通过查询是否存储在数据
        let readStatus = withUnsafeMutablePointer(to: &queryResult) { SecItemCopyMatching(keyChainReadmutableDictionary, UnsafeMutablePointer($0))}
        if readStatus == errSecSuccess {
            if let data = queryResult as! NSData? {
                idObject = NSKeyedUnarchiver.unarchiveObject(with: data as Data) as Any
            }
        }
        return idObject as Any
    }

    // MARK: 删除数据
    fileprivate class func keyChianDelete(identifier: String) {
        // 获取删除的条件
        let keyChainDeleteMutableDictionary = self.createQuaryMutableDictionary(identifier: identifier)
        // 删除数据
        SecItemDelete(keyChainDeleteMutableDictionary)
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值