2015-11-03 12 views
7

どのデバイス出力アプリが動作しているかを調べるには、多くのソリューションがあります。シミュレータ(IOS)からIphoneタイプを取得する方法

iOS: How to determine the current iPhone/device model in Swift?

しかし、シミュレータで実行されている、我々はそれが我々が異なるロジックをテストすることができますどのようにシミュレータの種類(iphone5,6,6sなど)

シミュレータですが、ないことを検出することができますシミュレータとdevicetypeに依存しますか? または、どのデバイスがコードでシミュレートされているかを検出するにはどうすればよいですか?

答えて

12

私はherehereを発見した回答に基づいて、私はあなたのために、この小さなスウィフト機能を書いた:

func getPlatformNSString() { 
    #if (arch(i386) || arch(x86_64)) && os(iOS) 
     let DEVICE_IS_SIMULATOR = true 
    #else 
     let DEVICE_IS_SIMULATOR = false 
    #endif 

    var machineSwiftString : String = "" 

    if DEVICE_IS_SIMULATOR == true 
    { 
     // this neat trick is found at http://kelan.io/2015/easier-getenv-in-swift/ 
     if let dir = NSProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] { 
      machineSwiftString = dir 
     } 
    } else { 
     var size : size_t = 0 
     sysctlbyname("hw.machine", nil, &size, nil, 0) 
     var machine = [CChar](count: Int(size), repeatedValue: 0) 
     sysctlbyname("hw.machine", &machine, &size, nil, 0) 
     machineSwiftString = String.fromCString(machine)! 
    } 

    print("machine is \(machineSwiftString)") 
} 

私はiPhoneに変換し、「iPhone8,2」、の結果を得ている6+これは私のシミュレータの設定です。

There's open source code available that you can use that would convert strings like "iPhone8,2" to the proper iPhone model name

そして、「DEVICE_IS_SIMULATOR」という魔法を使用するためのコンパイラの警告を取り除きたい場合は、here's a better solution in the form of a class

+0

大変ありがとうございました。 –

関連する問題