在运行时,是否可能检测到应用程序是通过TestFlight Beta(通过iTunes Connect提交)还是通过App Store安装的?您可以提交一个单独的应用程序包,并使其通过两者都可用。是否有API可以检测它是哪种方式安装的?或者收据是否包含允许确定此信息的信息?
在运行时,是否可能检测到应用程序是通过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);
注意,sandboxReceipt
也是在本地运行构建和在模拟器中运行构建时的收据文件名称。
Swift版本:
let isTestFlight = Bundle.main.appStoreReceiptURL?.lastPathComponent == "sandboxReceipt"
#if TARGET_IPHONE_SIMULATOR
isRunningInTestMode = YES;
#endif
显然,需要导入头文件 #import <TargetConditionals.h>。 - Gordon Dove[[[[NSBundle mainBundle] appStoreReceiptURL] lastPathComponent] isEqualToString:@"sandboxReceipt"]
(如果运行的是通过TestFlight分发的二进制文件,则为True)通过Supertop/Haddad - Nick根据combinatorial的答案,我创建了以下SWIFT辅助类。使用该类,您可以确定它是调试、testflight还是appstore构建。
enum AppConfiguration {
case Debug
case TestFlight
case AppStore
}
struct Config {
// This is private because the use of 'appConfiguration' is preferred.
private static let isTestFlight = Bundle.main.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或连接字符串:
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
}
}
更新于 2016 年 5 月 2 日:使用类似于#if DEBUG的预处理器宏的先决条件是设置一些Swift编译器自定义标志。更多信息请参见此答案:https://dev59.com/xWAf5IYBdhLWcg3w9Wiu#24112024
-D DEBUG
标志。更多信息可以在此处找到:https://dev59.com/UGAg5IYBdhLWcg3wE3nQ。 - Caleb#if targetEnvironment(simulator)
可以确定你是否在模拟器中运行。所以我有三个选项:模拟器、TestFlight 和 AppStore(在我的情况下,这比 Debug
更好) :-) - Jeroen现代Swift版本,考虑模拟器(基于接受的答案):
private func isSimulatorOrTestFlight() -> Bool {
guard let path = Bundle.main.appStoreReceiptURL?.path else {
return false
}
return path.contains("CoreSimulator") || path.contains("sandboxReceipt")
}
isTestFlight()
。 - dbn我在Swift 5.2中使用扩展Bundle+isProduction
:
import Foundation
extension Bundle {
var isProduction: Bool {
#if DEBUG
return false
#else
guard let path = self.appStoreReceiptURL?.path else {
return true
}
return !path.contains("sandboxReceipt")
#endif
}
}
然后:if Bundle.main.isProduction {
// do something
}
我在我的项目中使用一种方法。以下是步骤。
在Xcode中,转到项目设置(项目而非目标),并将“beta”配置添加到列表中:
然后,您需要创建一个新的方案,以在“beta”配置中运行项目。要创建方案,请转到此处:
给这个方案起任何你想要的名字。然后,你应该编辑此方案的设置。为此,请点击这里:
选择存档选项卡,您可以选择构建配置
然后您需要在项目信息属性列表中添加一个键Config
,其值为$(CONFIGURATION)
,如下所示:
然后,关键是你需要在代码中做一些特定于beta版本的事情:
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
}