import Foundation #if os(macOS) import Security #endif /// Which APNs gateway this build's tokens belong to, read from the build /// itself, never guessed: a token sent to the wrong gateway is refused with /// BadDeviceToken and the server then deletes the registration. /// /// - iPhone, App Store and TestFlight: Apple strips the provisioning profile /// from the bundle and the distribution signature carries /// `aps-environment` production. No embedded profile means production. /// - iPhone, an Xcode build on a device: the embedded development profile /// says `aps-environment` development. /// - iPhone Simulator: development (the Simulator registers with the sandbox). /// - Mac: the signature's own `com.apple.developer.aps-environment`. A /// Developer ID app is production; an ad hoc build has none and cannot /// receive pushes, so it does not register. /// /// Equipo's APNs key is production only (CARLOS platform push, D1): a /// development build registers honestly as development, and the server's /// sender cannot reach it. public enum PushEnvironment { public static func resolve(platform: PushPlatform, simulator: Bool, embeddedProfile: Data?, signedEntitlement: String?) -> APNsEnvironment? { switch platform { case .macos: return signedEntitlement.flatMap(APNsEnvironment.init(rawValue:)) case .ios: if simulator { return .development } guard let embeddedProfile else { return .production } return apsEnvironment(inProfile: embeddedProfile).flatMap(APNsEnvironment.init(rawValue:)) } } /// The `aps-environment` a provisioning profile grants: its property /// list sits inside a CMS envelope. public static func apsEnvironment(inProfile data: Data) -> String? { guard let start = data.range(of: Data("".utf8), in: start.lowerBound..