如何识别应用 运行 环境,包括试飞

How to identify the app running environment including Test Flight

我正在开发 iOS 应用程序,需要识别应用程序所在的环境 运行 对 API 端点进行分类。我想知道该应用程序是否在生产、模拟器和试飞下 运行。 我已经通过用户定义的设置对生产和模拟器进行了分类,但我仍然不确定如何识别试飞。 有小费吗?谢谢!

如果您要求从应用内获取此信息,您可以从 NSBundle

appStoreReceiptURL 获取所有这些信息

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

实现参考this问题

也可以使用收据字段中的 environment 字段。请查看随附的屏幕截图以获取 sandboxproduction 收据。

Swift 5. 使用下面的 WhereAmIRunning class 检查环境。

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
    }
}