Skip to content

Instantly share code, notes, and snippets.

@songjiz
Forked from joemasilotti/Endpoint.swift
Created May 14, 2023 01:49
Show Gist options
  • Save songjiz/64ee6508b4b5cac71bb9c61a74a5e581 to your computer and use it in GitHub Desktop.
Save songjiz/64ee6508b4b5cac71bb9c61a74a5e581 to your computer and use it in GitHub Desktop.
A Rails-like environment helper for iOS apps, from https://masilotti.com/rails-like-endpoint-switcher-for-ios-apps/
import Foundation
enum Environment: String {
case development, staging, production
}
extension Environment {
static var current: Environment {
if isAppStore {
return .production
} else if isTestFlight {
return .staging
}
return .development
}
static var isTestFlight: Bool {
if isSimulator {
return false
} else {
if isAppStoreReceiptSandbox, !hasEmbeddedMobileProvision {
return true
} else {
return false
}
}
}
static var isAppStore: Bool {
if isSimulator {
return false
} else {
if isAppStoreReceiptSandbox || hasEmbeddedMobileProvision {
return false
} else {
return true
}
}
}
var isDevelopment: Bool { self == .development }
var isStaging: Bool { self == .staging }
var isProduction: Bool { self == .production }
private static var hasEmbeddedMobileProvision: Bool {
guard Bundle.main.path(forResource: "embedded", ofType: "mobileprovision") == nil else {
return true
}
return false
}
private static var isAppStoreReceiptSandbox: Bool {
if isSimulator {
return false
} else {
guard let url = Bundle.main.appStoreReceiptURL else {
return false
}
guard url.lastPathComponent == "sandboxReceipt" else {
return false
}
return true
}
}
private static var isSimulator: Bool {
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment