2015-11-22 49 views
3

如何獲取CPU模型,CPU使用率,CPU空閒,RAM詳細信息,存儲細節等硬件信息?我使用Xcode 7.1.1和Swift 2.我不想製作應用程序。我試過,NSProcessInfo。我看到了幾個帖子,但我沒有找到任何解決辦法。如何在Swift中獲得硬件細節?

+1

能否請你表現出一定的努力,向我們展示到目前爲止你已經嘗試了什麼? –

+0

@ try-catch-finally我試過UIDevice,NSProcessInfo – Alexander

回答

3

大部分這些東西都可以通過一些較低級別的C API展現出來。這對你來說可能是一個很好的機會,可以根據它們編寫一些簡單的Swift擴展,但是你可以直接在Swift中編寫C代碼來獲得答案。

我喜歡邀功,但我只是鏈接到一些答案的其他人提供:

0

繼承人,HAS」擴展,我修改了從UIDevice獲取處理器和速度。可能不是最好的方法(所有的值都是硬編碼的),但它的工作原理!

import Foundation 
import NotificationCenter 

public extension UIDevice 
{ 
    //Original Author: HAS 
    // https://stackoverflow.com/questions/26028918/how-to-determine-the-current-iphone-device-model 
    // Modified by Sam Trent 

    /********************************************** 
    * getCPUName(): 
    *  Returns a hardcoded value of the current 
    * devices CPU name. 
    ***********************************************/ 
    public func getCPUName() -> String 
    { 
     var processorNames = Array(CPUinfo().keys) 
     return processorNames[0] 
    } 

    /********************************************** 
    * getCPUSpeed(): 
    *  Returns a hardcoded value of the current 
    * devices CPU speed as specified by Apple. 
    ***********************************************/ 
    public func getCPUSpeed() -> String 
    { 
     var processorSpeed = Array(CPUinfo().values) 
     return processorSpeed[0] 
    } 

    /********************************************** 
    * CPUinfo: 
    *  Returns a dictionary of the name of the 
    * current devices processor and speed. 
    ***********************************************/ 
    private func CPUinfo() -> Dictionary<String, String> { 

     #if (arch(i386) || arch(x86_64)) && os(iOS) 
     let identifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"]! 
     #else 

     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))) 
    } 
    #endif 

    switch identifier { 
    case "iPod5,1":         return ["A5":"800 MHz"] // underclocked 
    case "iPod7,1":         return ["A8":"1.4 GHz"] 
    case "iPhone3,1", "iPhone3,2", "iPhone3,3":  return ["A4":"800 MHz"] // underclocked 
    case "iPhone4,1":        return ["A5":"800 MHz"] // underclocked 
    case "iPhone5,1", "iPhone5,2":     return ["A6":"1.3 GHz"] 
    case "iPhone5,3", "iPhone5,4":     return ["A6":"1.3 GHz"] 
    case "iPhone6,1", "iPhone6,2":     return ["A7":"1.3 GHz"] 
    case "iPhone7,2":        return ["A8":"1.4 GHz"] 
    case "iPhone7,1":        return ["A8":"1.4 GHz"] 
    case "iPhone8,1":        return ["A9":"1.85 GHz"] 
    case "iPhone8,2":        return ["A9":"1.85 GHz"] 
    case "iPhone9,1", "iPhone9,3":     return ["A10 Fusion":"2.34 GHz"] 
    case "iPhone9,2", "iPhone9,4":     return ["A10 Fusion":"2.34 GHz"] 
    case "iPhone8,4":        return ["A9":"1.85 GHz"] 
    case "iPhone10,1", "iPhone10,4":    return ["A11 Bionic":"2.39 GHz"] 
    case "iPhone10,2", "iPhone10,5":    return ["A11 Bionic":"2.39 GHz"] 
    case "iPhone10,3", "iPhone10,6":    return ["A11 Bionic":"2.39 GHz"] 
    case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return ["A5":"1.0 GHz"] 
    case "iPad3,1", "iPad3,2", "iPad3,3":   return ["A5X":"1.0 GHz"] 
    case "iPad3,4", "iPad3,5", "iPad3,6":   return ["A6X":"1.4 GHz"] 
    case "iPad4,1", "iPad4,2", "iPad4,3":   return ["A7":"1.4 GHz"] 
    case "iPad5,3", "iPad5,4":      return ["A8X":"1.5 GHz"] 
    case "iPad6,11", "iPad6,12":     return ["A9":"1.85 GHz"] 
    case "iPad2,5", "iPad2,6", "iPad2,7":   return ["A5":"1.0 GHz"] 
    case "iPad4,4", "iPad4,5", "iPad4,6":   return ["A7":"1.3 GHz"] 
    case "iPad4,7", "iPad4,8", "iPad4,9":   return ["A7":"1.3 GHz"] 
    case "iPad5,1", "iPad5,2":      return ["A8":"1.5 GHz"] 
    case "iPad6,3", "iPad6,4":      return ["A9X":"2.16 GHz"] // underclocked 
    case "iPad6,7", "iPad6,8":      return ["A9X":"2.24 GHz"] 
    case "iPad7,1", "iPad7,2":      return ["A10X Fusion":"2.34 GHz"] 
    case "iPad7,3", "iPad7,4":      return ["A10X Fusion":"2.34 GHz"] 
    case "AppleTV5,3":        return ["A8":"1.4 GHz"] 
    case "AppleTV6,2":        return ["A10X Fusion":"2.34 GHz"] 
    case "AudioAccessory1,1":      return ["A8":"1.4 GHz"] // clock speed is a guess 
    default:          return ["N/A":"N/A"] 
    } 
} 
} 

您可以使用它像這樣:

print(UIDevice.current.getCPUName()) 
print(UIDevice.current.getCPUSpeed()) 
相關問題