1

I have a video analysis app and i'm trying to make it compatible with visionOS. It crashes because camera is not allowed; Apple only allows to import videos from Photos.

So I would need to check on launch if the device runs visionOS to show the photos picker rather than the camera view, similarly to what I use right now to check if I'm on macCatalyst or iOS:

#if targetEnvironment(macCatalyst)
   print("We are in macOS Catalyst")
#else
   print("We are in iOS")
#endif

Thanks in advance.

CarlosBF
  • 384
  • 1
  • 17

3 Answers3

1

Try os(visionOS) or os(xrOS). At the moment both names are correct.

#if os(visionOS)
   print("We are in visionOS")
#else
   print("We are in iOS")
#endif
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • Thanks, but that's the first thing I tried and it doesn't work. It always print the "We are in iOS" message. Even though I'm running in visionOS simulator, it detects it as iOS. Maybe I'm missing something? – CarlosBF Jul 25 '23 at 13:36
  • It depends where you put your preprocessor macros. I use it inside ContentView (#else statement is automatically dimmed out). – Andy Jazz Jul 25 '23 at 13:40
  • I use it in the viewDidLoad method of a specific UIViewController, and it doesn’t work. UIdevice.model tells me it’s an iPad… – CarlosBF Jul 26 '23 at 13:51
  • @CarlosBF The reason for this is that the UIKit app will run in iPad-compatible mode, whereas OS(xrOS) will return false. – Roger Lee Aug 22 '23 at 03:15
1

I believe you're running your iOS app directly on visionOS, without recompiling the code against the visionOS SDK, correct?

If so, the app is in compatible mode, and any conditional compile directives like #if os(xrOS) won't work. It's similar to an iOS app running on a Mac with Apple Silicon (check ProcessInfo.isiOSAppOnMac).

However, I haven't found a suitable API to determine if an iOS app is running on visionOS at runtime.

Forum Link: https://developer.apple.com/forums/thread/733029

Gong Zhang
  • 11
  • 2
  • Thanks a lot, Gong. You were correct. It seems that there are no options right now to check if an iOS app is running on Vision Pro. The only viable option at this point is to compile adding Vision Pro as a new device rather than using the compatibility mode. It will likely break parts of the code though – CarlosBF Jul 26 '23 at 16:24
  • Hi again Gong, check my updated accepted answer. I've found a piece of code that detects the Vision Pro simulator as "RealityDevice". It might be helpful. Cheers – CarlosBF Jul 27 '23 at 06:44
1

I've found a workaround with a piece of code here, created to detect iPhone/iPad models:

    func modelIdentifier() -> String {
    if let simulatorModelIdentifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] { return simulatorModelIdentifier }
    var sysinfo = utsname()
    uname(&sysinfo) // ignore return value
    return String(bytes: Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)), encoding: .ascii)!.trimmingCharacters(in: .controlCharacters)
}

When calling it in viewDidLoad, it successfully detects the Vision Pro simulator as 'RealityDevice14,1'.

HangarRash
  • 7,314
  • 5
  • 5
  • 32
CarlosBF
  • 384
  • 1
  • 17