開始使用
設定 appKey
appKey 是發給你們的公開識別值,格式如 ak_xxx,用來辨識是哪個 App;請放在本機、不進版控的設定檔,由 build 系統注入。
- appKey 不是密碼,但不應寫進版控或公開文件,以免混淆不同環境。
- 單獨持有 appKey 換不到資料 token;SDK 會再以平台裝置驗證換取短期 token。
- 測試環境另有「測試環境驗證碼」,由我方另行提供,注入方式與 appKey 相同,不要寫死在程式裡。
iOS
- 建立
Config/Local.xcconfig,並確認它被.gitignore忽略。
LAPID_APP_KEY = ak_xxx
LAPID_TEST_ATTESTATION = xxx- Debug、Release 的 xcconfig 以
#include? "Local.xcconfig"引用它。缺檔時不會報錯,但缺值時 SDK 會丟錯。 - 在
Info.plist新增鍵LapidAppKey,值為$(LAPID_APP_KEY),Xcode 會展開成 xcconfig 的值。試點期另加LapidTestAttestation,值為$(LAPID_TEST_ATTESTATION)。 - 以
LapidConfiguration.fromInfoPlist建立設定。
import LapidSDK
let testAttestation = Bundle.main.object(forInfoDictionaryKey: "LapidTestAttestation") as? String ?? ""
let config = try LapidConfiguration.fromInfoPlist(
baseURL: URL(string: "https://sdk-api.lapidinsight.com")!,
attestation: DebugAttestationProvider(payload: testAttestation)
)
let client = LapidClient(configuration: config)Info.plist 缺值、為空白或未展開時,會丟出 LapidConfigurationError.missingAppKey,訊息會指出要設定的鍵名。
Android
- 在專案根目錄的
local.properties(Android 預設已被.gitignore忽略)加入:
lapid.appKey=ak_xxx
lapid.testAttestation=xxx- 在
app/build.gradle.kts把它注入 manifest 佔位。
val localProps = java.util.Properties().apply {
rootProject.file("local.properties").takeIf { it.isFile }?.inputStream()?.use { load(it) }
}
android {
defaultConfig {
manifestPlaceholders["lapidAppKey"] = localProps.getProperty("lapid.appKey", "")
// 試點期:測試環境驗證碼,只放 debug 用途
buildConfigField("String", "LAPID_TEST_ATTESTATION", "\"${localProps.getProperty("lapid.testAttestation", "")}\"")
}
buildFeatures { buildConfig = true }
}- 在
AndroidManifest.xml的application內加入 meta-data。
<meta-data android:name="com.rytass.lapid.APP_KEY" android:value="${lapidAppKey}" />- 以
LapidConfig.fromManifest建立設定。
val config = LapidConfig.fromManifest(
context = this,
baseUrl = "https://sdk-api.lapidinsight.com",
attestation = DebugAttestationProvider(BuildConfig.LAPID_TEST_ATTESTATION),
)
val client = LapidClient(this, config)缺值時丟出 LapidConfigException,訊息會指出要設定的 meta-data 名稱。
Web
Web SDK 的設定沒有 appKey。token 由你的後端取得後,交給 tokenSource,詳見「第一個請求」與「授權與限流」。