问题
I'm developing iOS app and need to identify the environment where the app is running to classify the API endpoint. I want to know the app is running under whether production, simulator and also Test Flight. I've already done classifying production and simulator by User-defined setting, but am still not sure how I can identify Test Flight. Any tips? thanks!
回答1:
If you are asking to get this information from within the app, you can get all this from appStoreReceiptURL
of NSBundle
From apple documentation...
For an application purchased from the App Store, use this application bundle property to locate the receipt. This property makes no guarantee about whether there is a file at the URL—only that if a receipt is present, that is its location.
NSBundle.mainBundle().appStoreReceiptURL?.lastPathComponent
For implementation refer to this question
回答2:
Also can use environment
field from receipt fields. Please check the attached screenshot for sandbox
and production
receipts.
回答3:
Swift 5. Use the below WhereAmIRunning class to check the environment.
import Foundation
class WhereAmIRunning {
// MARK: Public
func isRunningInTestFlightEnvironment() -> Bool{
if isSimulator() {
return false
} else {
if isAppStoreReceiptSandbox() && !hasEmbeddedMobileProvision() {
return true
} else {
return false
}
}
}
func isRunningInAppStoreEnvironment() -> Bool {
if isSimulator(){
return false
} else {
if isAppStoreReceiptSandbox() || hasEmbeddedMobileProvision() {
return false
} else {
return true
}
}
}
// MARK: Private
private func hasEmbeddedMobileProvision() -> Bool{
if let _ = Bundle.main.path(forResource: "embedded", ofType: "mobileprovision") {
return true
}
return false
}
private func isAppStoreReceiptSandbox() -> Bool {
if isSimulator() {
return false
} else {
if let appStoreReceiptURL = Bundle.main.appStoreReceiptURL,
let appStoreReceiptLastComponent = appStoreReceiptURL.lastPathComponent as? String, appStoreReceiptLastComponent == "sandboxReceipt" {
return true
}
return false
}
}
private func isSimulator() -> Bool {
#if arch(i386) || arch(x86_64)
return true
#else
return false
#endif
}
}
来源:https://stackoverflow.com/questions/37237583/how-to-identify-the-app-running-environment-including-test-flight