Last active
November 9, 2021 21:12
-
-
Save soffes/eb5f869b643d41c5de4e0dc283a7e25d to your computer and use it in GitHub Desktop.
Find the push environment of a device. (It doesn't actually reference `UIDevice` anywhere but this seemed like a good place to put it.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From my answer https://stackoverflow.com/a/69905152/118631 | |
import UIKit | |
public extension UIDevice { | |
enum PushEnvironment: String { | |
case unknown | |
case development | |
case production | |
} | |
var pushEnvironment: PushEnvironment { | |
guard let provisioningProfile = try? provisioningProfile(), | |
let entitlements = provisioningProfile["Entitlements"] as? [String: Any], | |
let environment = entitlements["aps-environment"] as? String | |
else { | |
return .unknown | |
} | |
return PushEnvironment(rawValue: environment) ?? .unknown | |
} | |
// MARK: - Private | |
private func provisioningProfile() throws -> [String: Any]? { | |
guard let url = Bundle.main.url(forResource: "embedded", withExtension: "mobileprovision") else { | |
return nil | |
} | |
let binaryString = try String(contentsOf: url, encoding: .isoLatin1) | |
let scanner = Scanner(string: binaryString) | |
guard scanner.scanUpToString("<plist") != nil, let plistString = scanner.scanUpToString("</plist>"), | |
let data = (plistString + "</plist>").data(using: .isoLatin1) | |
else { | |
return nil | |
} | |
return try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment