Skip to content

Instantly share code, notes, and snippets.

@pepasflo
Last active April 12, 2019 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pepasflo/4cbfedd826c929964c08535e7c18e594 to your computer and use it in GitHub Desktop.
Save pepasflo/4cbfedd826c929964c08535e7c18e594 to your computer and use it in GitHub Desktop.
BetaFlags: FeatureFlags which are automatically false for AppStore builds.

BetaFlags: FeatureFlags which are automatically false for AppStore builds.

import UIKit
import PlaygroundSupport
// FeatureFlags vs. "BetaFlags". BetaFlags are automatically false for AppStore builds, which provides
// an extra level of safety against accidentally shipping a build which has beta features enabled.
struct FeatureFlags {
// Set to false to disable support for fancy doors.
public static var HasGullWingDoors = true
// Set to true to end humanity as we know it.
public static var HasSkynetLevelAI = true
}
// Beta flags are automatically false in AppStore builds.
public struct BetaFlags {
public init() {}
public var hasRocketPoweredHangGliders: Bool {
return _wrapped(flag: BetaFlags._HasRocketPoweredHangGliders)
}
public var hasNuclearTacoSeasoning: Bool {
return _wrapped(flag: BetaFlags._HasNuclearTacoSeasoning)
}
// MARK: - Iternals
private func _wrapped(flag: Bool) -> Bool {
// Ensure we return false for all flags if this is an AppStore build.
guard !BuildConfiguration.current.isAppStore else { return false }
return flag
}
// Set to true to enable rocket-powered hang gliders.
private static var _HasRocketPoweredHangGliders = false
// Set to true to enable object reference counting console debugging.
private static var _HasNuclearTacoSeasoning = false
}
// Based on http://stackoverflow.com/a/33830605/7543271
public enum BuildConfiguration: String {
// "debug" indicates a build which:
// - used the "Debug" Xcode configuration
case debug
// "appStore" indicates a build which:
// - used the "Release" Xcode configuration
// - was installed from the AppStore
case appStore
// "testFlight" indicates a build which:
// - used the "Release" Xcode configuration
// - was installed via TestFlight
case testFlight
public static var current: BuildConfiguration {
if isDebug {
return .debug
} else if isTestFlight {
return .testFlight
} else {
return .appStore
}
}
public var isAppStore: Bool {
switch self {
case .debug, .testFlight:
return false
case .appStore:
return true
}
}
// Thanks to http://stackoverflow.com/a/26113597/7543271
fileprivate static let isTestFlight = Bundle.main.appStoreReceiptURL?.lastPathComponent == "sandboxReceipt"
fileprivate static var isDebug: Bool {
#if DEBUG
return true
#else
return false
#endif
}
}
// Usage:
class ViewController : UIViewController {
override func viewDidLoad() {
if FeatureFlags.HasSkynetLevelAI {
// do some stuff
}
}
}
class ViewController2 : UIViewController {
override func viewDidLoad() {
if BetaFlags().hasNuclearTacoSeasoning {
// do some stuff
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment