大佬教程收集整理的这篇文章主要介绍了用于开发、UAT 和生产的 Expo App 环境,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个内置于 Expo 的 React Native 应用程序,可连接到 Rest API。其余 API 共有三种环境 - dev、uat 和 production,如下(示例)。
dev = https://dev.myAPI.com/API
uat = https://uat.myAPI.com/API
prod = https://prod.myAPI.com/API
根据应用的使用位置,它需要连接到正确的环境。
Running in the Expo ClIEnt = Dev API
Running in TestFlight or Internal TesTing for the Play Store = UAT API
Running in the App Store or Play Store = Production API
实现这一目标的最简单方法是什么?
按照以下步骤操作
安装 expo-constants 包。要安装包,请运行以下命令。
npm i expo-constants
@H_262_24@添加 environment.js 文件并粘贴下面的代码。
import Constants from "expo-constants";
import { Platform } from "react-native";
const localhost =
Platform.OS === "ios" ? "localhost:8080" : "10.0.2.2:8080";
const ENV = {
dev: {
apiUrl: "https://dev.myapi.com/api",amplitudeApiKey: null,},staging: {
apiUrl: "https://uat.myapi.com/api",amplitudeApiKey: "[Enter your key here]",// Add other keys you want here
},prod: {
apiUrl: "https://prod.myapi.com/api",// Add other keys you want here
}
};
const getEnvVars = (env = Constants.manifest.releaseChAnnel) => {
// what is __DEV__ ?
// This variable is set to true when react-native is running in Dev mode.
// __DEV__ is true when run locally,but false when published.
if (__DEV__) {
return ENV.dev;
} else if (env === 'staging') {
return ENV.staging;
} else if (env === 'prod') {
return ENV.prod;
}
};
export default getEnvVars;
// Import getEnvVars() from environment.js
import getEnvVars from '../environment';
const { apiUrl } = getEnvVars();
/******* SESSIONS::LOG IN *******/
// LOG IN
// credentials should be an object containing phone number:
// {
// "phone" : "9876342222"
// }
export const logIn = (credentials,jsonWebToken) => (
fetch(`${apiUrl}/phone`,{
method: 'POST',headers: {
'Authorization': 'Bearer ' + jsonWebToken,'Content-Type': 'application/json',body: JSON.Stringify(credentials)
})
);
开发 - expo build:ios --release-chAnnel dev
分期 - expo build:ios --release-chAnnel 分期
生产 - expo build:ios --release-chAnnel prod
现在 Expo 支持配置文件为 app.config.js 或 app.config.ts,我们可以使用 dotenv。检查这个:https://docs.expo.io/guides/environment-variables/#using-a-dotenv-file
以上是大佬教程为你收集整理的用于开发、UAT 和生产的 Expo App 环境全部内容,希望文章能够帮你解决用于开发、UAT 和生产的 Expo App 环境所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。