是否有一个编译器指令,我可以用于编制不同行代码时,针对模拟器而不是我的设备。是这样的:

# IF SIMULATOR
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
# ELSE
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
# END

编辑

直接链接到文档。

有帮助吗?

解决方案

#if TARGET_IPHONE_SIMULATOR
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
#else
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
#endif

其他提示

更新:(不建议使用/过时)仅工作了几年,并没有工作了。 (10+年后)

有关的记录,这里是苹果公司使用在他们的一些官方示例代码的另一种方法:

#if TARGET_CPU_ARM
  // Only executes on an iPhone or iPod touch device
  [self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
#else
  // Only executes on the Simulator
  [self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
#endif

对于那些在寻找一个现代化的迅速解决,(新的)平台的条件 targetEnvironment 提供明确的答案在这里。例如:

#if targetEnvironment(simulator)
self.imagePicker.sourceType = .photoLibrary
#else
self.imagePicker.sourceType = .camera
#endif 

目标的环境平台的条件 特介绍了 SE-0190 可以来 Swift4.1.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top