2012-03-15 237 views
17

可能重複:
Programmatically detect if app is being run on device or simulator如何檢測應用程序是在模擬器或設備上運行

我該如何檢測自己的應用程序是否在模擬器上或通過代碼運行的設備。

+2

看到這個?? http://stackoverflow.com/questions/5775420/programmatically-detect-if-app-is-being-run-on-device-or-simulator – Vin 2012-03-15 10:41:49

+1

這麼多帖子的副本:http://stackoverflow.com/questions/ 5122149/iphone-simulator-how-to-detect-when-app-is-running-on-simulator-so-can-setup,http://stackoverflow.com/questions/5775420/programmatically-detect-if-app- is-being-run-on-device-or-simulator,http://stackoverflow.com/questions/458304/how-can-i-programmatically-determine- if- my-app-is-running-in-the- iphone-simulato – Sarah 2012-03-15 10:45:58

+1

@富爾維奧抱歉,如果它聽起來很刺耳。這個問題在SO上有很多問題。我們不應該在發佈之前搜索已經存在的帖子,與我們的問題相關嗎? – Vin 2012-03-15 10:50:16

回答

66

記住爲您提供了關於設備本身的信息。

[[UIDevice currentDevice] model]

您也可以使用以下方法:

TARGET_IPHONE_SIMULATOR告訴你,如果你在iPhone模擬器是。

TARGET_OS_IPHONE告訴您,您正在使用iPhone而不是MacOS。

#if TARGET_IPHONE_SIMULATOR 

    NSLog(@"Running in Simulator - no app store or giro"); 

#else 

    NSLog(@"Running on the Device"); 

#endif 

,當在設備

#if !(TARGET_IPHONE_SIMULATOR) 

    NSLog(@"Running on device"); 

#endif 
8

只關心你可以使用這個常量

#if TARGET_OS_SIMULATOR 
    NSLog(@"This is simulator mode...."); 
#else 
    NSLog(@"This is device mode...."); 
#endif 
1

相同的已編譯的應用程序不能在模擬器和iOS設備都運行,因爲CPU指令集完全不同(x86與ARM)。 (...除非你使用lipo構建某種非常奇怪的超級通用二進制文件)

有幾種方法可以確定應用程序是否爲x86編譯。一種是添加運行時代碼,這取決於許多預定義的編譯器預處理器宏之一。您可以通過在終端命令行中鍵入以下內容來獲得x86編譯的預處理器宏列表:

gcc -arch i386 -dM -E - </dev/null |分類

相關問題