ios - xcode模擬器教學 - 我如何以編程方式確定我的應用程序是否在iphone模擬器中運行?
xcode模擬器app store (12)
更新的代碼:
這被稱為正式工作。
#if TARGET_IPHONE_SIMULATOR
NSString *hello = @"Hello, iPhone simulator!";
#elif TARGET_OS_IPHONE
NSString *hello = @"Hello, device!";
#else
NSString *hello = @"Hello, unknown target!";
#endif
原文(自棄用)
這段代碼會告訴你,如果你正在模擬器中運行。
#ifdef __i386__
NSLog(@"Running in the simulator");
#else
NSLog(@"Running on a device");
#endif
正如問題所述,我主要想知道我的代碼是否在模擬器中運行,但也有興趣知道正在運行或正在模擬的特定iphone版本。
編輯:我添加了單詞“編程”的問題名稱。 我的問題的關鍵在於能夠根據運行的版本/模擬器動態地包含/排除代碼,所以我真的會在尋找類似可以為我提供此信息的預處理器指令。
///如果模擬器不是設備,則返回true
public static var isSimulator: Bool {
#if (arch(i386) || arch(x86_64)) && os(iOS)
return true
#else
return false
#endif
}
在Swift的情況下,我們可以執行以下操作
我們可以創建允許您創建結構化數據的結構
struct Platform {
static let isSimulator: Bool = {
#if arch(i386) || arch(x86_64)
return true
#endif
return false
}()
}
然後,如果我們想要檢測應用程序是在Swift中為設備還是模擬器構建的話。
if Platform.isSimulator {
// Do one thing
}
else {
// Do the other
}
不是預處理器指令,但是當我想到這個問題時,這就是我正在尋找的東西;
NSString *model = [[UIDevice currentDevice] model];
if ([model isEqualToString:@"iPhone Simulator"]) {
//device is simulator
}
在我看來,答案(上面提到並在下面重複):
NSString *model = [[UIDevice currentDevice] model];
if ([model isEqualToString:@"iPhone Simulator"]) {
//device is simulator
}
是最好的答案,因為它顯然是在運行時執行而不是COMPILE DIRECTIVE。
已經問過,但標題非常不同。
我會從那裡重複我的答案:
它位於“有條件編譯源代碼”下的SDK文檔中
相關定義是TARGET_OS_SIMULATOR,它在iOS框架中的/usr/include/TargetConditionals.h中定義。 在早期版本的工具鏈中,您必須編寫:
#include "TargetConditionals.h"
但對於當前的(Xcode 6 / iOS8)工具鏈,這不再是必需的。
所以,例如,如果你想檢查你是否在設備上運行,你應該這樣做
#if TARGET_OS_SIMULATOR
// Simulator-specific code
#else
// Device-specific code
#endif
取決於哪一個適合您的使用情況。
我的回答是基於@Daniel Magnusson的回答以及@Nuthatch和@ n.Drake的評論。 並且我寫它為在iOS9及更高版本上工作的快速用戶節省了一些時間。
這對我來說很有用:
if UIDevice.currentDevice().name.hasSuffix("Simulator"){
//Code executing on Simulator
} else{
//Code executing on Device
}
所有這些答案都很好,但它會讓我這樣的新手感到困惑,因為它沒有闡明編譯檢查和運行時檢查。 預處理器在編譯之前,但我們應該更清楚
這篇博客文章展示瞭如何檢測iPhone模擬器? 明確地
運行
首先,我們簡要討論一下。 UIDevice已經提供了有關設備的信息
[[UIDevice currentDevice] model]
將根據應用程序的運行位置返回“iPhone模擬器”或“iPhone”。
編譯時間
然而你想要的是使用編譯時定義。 為什麼? 因為您嚴格編譯應用程序以在模擬器或設備中運行。 Apple製作了一個名為TARGET_IPHONE_SIMULATOR
的定義。 所以讓我們看看代碼:
#if TARGET_IPHONE_SIMULATOR
NSLog(@"Running in Simulator - no app store or giro");
#endif
有沒有人考慮過here提供的答案?
我想這個objective-c相當於
+ (BOOL)isSimulator {
NSOperatingSystemVersion ios9 = {9, 0, 0};
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
if ([processInfo isOperatingSystemAtLeastVersion:ios9]) {
NSDictionary<NSString *, NSString *> *environment = [processInfo environment];
NSString *simulator = [environment objectForKey:@"SIMULATOR_DEVICE_NAME"];
return simulator != nil;
} else {
UIDevice *currentDevice = [UIDevice currentDevice];
return ([currentDevice.model rangeOfString:@"Simulator"].location != NSNotFound);
}
}
現在有更好的方法!
從Xcode 9.3 beta 4開始,你可以使用#if targetEnvironment(simulator)
來檢查。
#if targetEnvironment(simulator)
//Your simulator code
#endif
這對我最有效
NSString *name = [[UIDevice currentDevice] name];
if ([name isEqualToString:@"iPhone Simulator"]) {
}