Skip to content

Instantly share code, notes, and snippets.

@samuelbeek
Created November 24, 2015 12:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelbeek/466981c098969a870d2c to your computer and use it in GitHub Desktop.
Save samuelbeek/466981c098969a870d2c to your computer and use it in GitHub Desktop.
Detect if Simulator is used in Swift
struct Platform
{
static let isSimulator: Bool = {
var isSim = false
#if arch(i386) || arch(x86_64)
isSim = true
#endif
return isSim
}()
}
@kiroskirin
Copy link

I think you can also extend UIDevice like this

extension UIDevice {
    var isSimulator: Bool {
        #if arch(i386) || arch(x86_64)
            return true
        #else
            return false
        #endif
    }
}

@artemnovichkov
Copy link

Swift 4.1+:

static var audioStatus: AVAuthorizationStatus {
    #if targetEnvironment(simulator)
        return .authorized
    #else
        return AVCaptureDevice.authorizationStatus(for: .audio)
    #endif
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment