【发布时间】:2019-11-26 17:02:02
【问题描述】:
我正在开发 iOS 应用,需要识别应用运行的环境以对 API 端点进行分类。我想知道该应用程序是否在生产、模拟器和试飞下运行。 我已经通过用户定义的设置对生产和模拟器进行了分类,但我仍然不确定如何识别试飞。 有小费吗?谢谢!
【问题讨论】:
标签: ios xcode testflight
我正在开发 iOS 应用,需要识别应用运行的环境以对 API 端点进行分类。我想知道该应用程序是否在生产、模拟器和试飞下运行。 我已经通过用户定义的设置对生产和模拟器进行了分类,但我仍然不确定如何识别试飞。 有小费吗?谢谢!
【问题讨论】:
标签: ios xcode testflight
如果您要求从应用内获取此信息,您可以从appStoreReceiptURL 或NSBundle 获取所有这些信息
来自apple documentation...
对于从 App Store 购买的应用程序,请使用此应用程序捆绑属性来查找收据。此属性不保证 URL 上是否存在文件,仅保证如果存在收据,则说明其位置。
NSBundle.mainBundle().appStoreReceiptURL?.lastPathComponent
具体实现请参考this问题
【讨论】:
Swift 5. 使用下面的 WhereAmIRunning 类来检查环境。
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
}
}
【讨论】: