Buildfire开发iOS应用:React代码获取构建版本及环境检测需求
在Buildfire iOS应用中获取构建版本及判断分发环境
获取构建版本
Buildfire的SDK提供了直接访问应用版本信息的接口,在React代码中可通过buildfire.appInfo对象获取:
// 获取应用版本号(如1.0.0) const appVersion = buildfire.appInfo.version; // 获取构建号(如123,需项目提前配置) const buildNumber = buildfire.appInfo.buildNumber;
确保代码在Buildfire SDK初始化完成后执行,比如放在函数组件的useEffect钩子或类组件的componentDidMount方法中。
判断Test Flight/App Store环境
Buildfire没有直接提供环境判断API,可通过以下两种方式实现:
方式1:自定义iOS原生插件检测
利用iOS系统收据特性,通过原生插件返回环境状态:
- 编写iOS原生插件代码
在Buildfire的iOS插件中添加检测方法:
- (void)getDistributionEnvironment:(BuildFirePlugin *)plugin { BOOL isTestFlight = NO; NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL]; NSString *receiptLastPathComponent = [receiptURL lastPathComponent]; // Test Flight应用的收据路径包含sandboxReceipt if ([receiptLastComponent isEqualToString:@"sandboxReceipt"]) { isTestFlight = YES; } NSDictionary *result = @{ @"isTestFlight": @(isTestFlight), @"isAppStore": @(!isTestFlight) }; [plugin success:result]; }
- 在React代码中调用插件
通过Buildfire插件接口获取环境信息:
buildfire.pluginInstance.call( 'getDistributionEnvironment', null, (response) => { const { isTestFlight, isAppStore } = response; if (isTestFlight) { console.log('当前处于Test Flight环境'); // 执行对应环境逻辑 } else if (isAppStore) { console.log('当前处于App Store环境'); // 执行对应环境逻辑 } }, (error) => { console.error('环境检测失败:', error); } );
方式2:通过Buildfire配置变量区分
在打包时为不同环境设置自定义变量,直接在React中读取:
- 在Buildfire项目打包配置中,分别为Test Flight和App Store打包设置变量(如
ENV_TYPE=testflight或ENV_TYPE=appstore) - 在React代码中读取变量:
const envType = buildfire.appInfo.customEnvVars?.ENV_TYPE; if (envType === 'testflight') { // Test Flight环境逻辑 } else if (envType === 'appstore') { // App Store环境逻辑 }
内容的提问来源于stack exchange,提问作者Deepak Pandit
相关产品推荐
相关产品推荐

