如何在运行时告诉iOS应用程序是否正在运行TestFlight Beta安装

是否有可能在运行时检测到应用程序已通过TestFlight Beta(通过iTunes Connect提交)与App Store安装? 您可以提交一个应用程序包,并通过这两个应用程序包提供。 有没有一个API可以检测到它安装的方式? 或者收据是否包含允许确定的信息?

对于通过TestFlight Beta安装的应用程序,收据文件被命名为StoreKit\sandboxReceipt与通常的StoreKit\receipt 。 使用[NSBundle appStoreReceiptURL]您可以在URL的末尾查找sandboxReceipt。

 NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL]; NSString *receiptURLString = [receiptURL path]; BOOL isRunningTestFlightBeta = ([receiptURLString rangeOfString:@"sandboxReceipt"].location != NSNotFound); 

请注意,在本地运行构build以及在模拟器中运行构build时, sandboxReceipt也是收据文件的名称。

基于组合的答案,我创build了以下SWIFT辅助类。 有了这个类,你可以确定它是一个debugging,testing或AppStore构build。

 enum AppConfiguration { case Debug case TestFlight case AppStore } struct Config { // This is private because the use of 'appConfiguration' is preferred. private static let isTestFlight = NSBundle.mainBundle().appStoreReceiptURL?.lastPathComponent == "sandboxReceipt" // This can be used to add debug statements. static var isDebug: Bool { #if DEBUG return true #else return false #endif } static var appConfiguration: AppConfiguration { if isDebug { return .Debug } else if isTestFlight { return .TestFlight } else { return .AppStore } } } 

我们在项目中使用这些方法为每个环境提供不同的跟踪ID连接string

  func getURL(path: String) -> String { switch (Config.appConfiguration) { case .Debug: return host + "://" + debugBaseUrl + path default: return host + "://" + baseUrl + path } } 

要么:

  static var trackingKey: String { switch (Config.appConfiguration) { case .Debug: return debugKey case .TestFlight: return testflightKey default: return appstoreKey } } 

UPDATE 05-02-2016:使用像#if DEBUG这样的预处理器macros的先决条件是设置一些Swift编译器自定义标志。 在这个答案更多的信息: https : //stackoverflow.com/a/24112024/639227

现代Swift版本,占模拟器(根据接受的答案):

 private func isSimulatorOrTestFlight() -> Bool { guard let path = Bundle.main.appStoreReceiptURL?.path else { return false } return path.contains("CoreSimulator") || path.contains("sandboxReceipt") } 

这也适用:

 if NSBundle.mainBundle().pathForResource("embedded", ofType: "mobileprovision") != nil { // TestFlight } else { // App Store (and Apple reviewers too) } 

如果iOS应用程序从Apple的Testflight下载,则会在Detect中发现

有一种方法可以用于我的项目。 这是步骤。

在Xcode中,转到项目设置(项目,而不是目标),并将“beta”configuration添加到列表中:

在这里输入图像描述

然后,您需要创build新的scheme,以“testing版”configuration运行项目。 创build计划去这里:

在这里输入图像描述

无论你想要什么名字这个计划。 你应该编辑这个scheme的设置。 要做到这一点,点击这里:

在这里输入图像描述

select存档选项卡,您可以在其中selectBuild configuration

在这里输入图像描述

然后你需要添加一个Config值为$(CONFIGURATION)的项目信息属性列表,如下所示:

在这里输入图像描述

那么它只是你在代码中需要做什么特定于testing版本的事情:

 let config = Bundle.main.object(forInfoDictionaryKey: "Config") as! String if config == "Debug" { // app running in debug configuration } else if config == "Release" { // app running in release configuration } else if config == "Beta" { // app running in beta configuration }