gre*_*rey 10 xcode swift xcode-cloud
environment variables
我们在Xcode 方案中有这些
这段代码在本地运行良好
let webHost = ProcessInfo.processInfo.environment["HOST_URL"]!
let apiHost = ProcessInfo.processInfo.environment["API_URL"]!
let beamsKey = ProcessInfo.processInfo.environment["BEAMS_KEY"]!
let mixpanelKey = ProcessInfo.processInfo.environment["MIXPANEL_KEY"]!
Run Code Online (Sandbox Code Playgroud)
但是,当使用Xcode Cloud与相同的environment variables
.
它构建成功,但应用程序因该日志而崩溃。
使用Xcode Cloud时读取这些环境变量的正确方法是什么?
我有一个类似的问题,主要是我想api-key
在项目中添加一个,而源代码中不存在这个问题。所以我必须创建一个ci_pre_xcodebuild.sh
文件
#!/bin/sh
echo "Stage: PRE-Xcode Build is activated .... "
# for future reference
# https://developer.apple.com/documentation/xcode/environment-variable-reference
cd ../ProjectName/
plutil -replace API_KEY_DEBUG -string $API_KEY_DEBUG Info.plist
plutil -replace API_KEY_RELEASE -string $API_KEY_RELEASE Info.plist
plutil -p Info.plist
echo "Stage: PRE-Xcode Build is DONE .... "
exit 0
Run Code Online (Sandbox Code Playgroud)
在代码中我们有
#!/bin/sh
echo "Stage: PRE-Xcode Build is activated .... "
# for future reference
# https://developer.apple.com/documentation/xcode/environment-variable-reference
cd ../ProjectName/
plutil -replace API_KEY_DEBUG -string $API_KEY_DEBUG Info.plist
plutil -replace API_KEY_RELEASE -string $API_KEY_RELEASE Info.plist
plutil -p Info.plist
echo "Stage: PRE-Xcode Build is DONE .... "
exit 0
Run Code Online (Sandbox Code Playgroud)
所以,这绝对是一个令人头痛的问题,但我终于找到了一种令人满意的方法来在代码中访问和使用这些变量。
我的解决方案使用:
ci_pre_xcodebuild.sh
用于在 JSON 中写入环境变量的文件(位于:YourProject/SupportingFiles/secrets.json)
{
"STRIPE_KEY": "",
"GOOGLE_MAPS_KEY": "",
"GOOGLE_PLACES_KEY": "",
"BASE_URL": "https://dev.api.example.fr"
}
Run Code Online (Sandbox Code Playgroud)
在此屏幕截图中,您可以看到我为不同的环境复制了密钥。为了简洁起见,我没有对此进行扩展,但是对于不同的 Xcode 方案配置,您绝对可以拥有不同的秘密 JSON 文件。
ci_pre_xcodebuild.sh
文件重要提示:文件的名称及其位置很重要。
这里的目标是添加 CI (Xcode Cloud) 每次构建时都会执行的脚本。在此脚本中,我们将创建并填充 JSON。
在此组中,添加一个名为的新文件ci_pre_xcodebuild.sh
写下以下内容:
#!/bin/sh
echo "Stage: PRE-Xcode Build is activated .... "
# Move to the place where the scripts are located.
# This is important because the position of the subsequently mentioned files depend of this origin.
cd $CI_WORKSPACE/ci_scripts || exit 1
# Write a JSON File containing all the environment variables and secrets.
printf "{\"STRIPE_KEY\":\"%s\",\"GOOGLE_MAPS_KEY\":\"%s\",\"GOOGLE_PLACES_KEY\":\"%s\",\"BASE_URL\":\"%s\"}" "$STRIPE_KEY" "$GOOGLE_MAPS_KEY" "$GOOGLE_PLACES_KEY" "$BASE_URL" >> ../Dream\ Energy/SupportingFiles/Secrets.json
echo "Wrote Secrets.json file."
echo "Stage: PRE-Xcode Build is DONE .... "
exit 0
Run Code Online (Sandbox Code Playgroud)
当然,您需要根据您的密钥和文件位置更改此文本。我添加了一些键作为示例。
chmod +x ci_pre_xcodebuild.sh
。这修复了 Xcode Cloud 中的警告。import Foundation
struct Secrets {
private static func secrets() -> [String: Any] {
let fileName = "Secrets"
let path = Bundle.main.path(forResource: fileName, ofType: "json")!
let data = try! Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
return try! JSONSerialization.jsonObject(with: data) as! [String: Any]
}
static var baseURL: String {
return secrets()["BASE_URL"] as! String
}
static var stripeKey: String {
return secrets()["STRIPE_KEY"] as! String
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4929 次 |
最近记录: |