開始在 Apple 平台上搭配自訂供應商使用 App Check
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本頁面說明如何使用自訂 App Check 提供者,在 Apple 應用程式中啟用 App Check。啟用 App Check 後,只有您的應用程式可以存取專案的 Firebase 資源。
如要搭配內建供應商使用 App Check,請參閱「搭配 App Attest 使用 App Check」和「搭配 DeviceCheck 使用 App Check」說明文件。
事前準備
1. 將 App Check 程式庫新增至應用程式
將 App Check 的依附元件新增至專案的 Podfile
:
pod 'FirebaseAppCheck'
或者,您也可以改用 Swift Package Manager。
此外,請確認您使用的任何 Firebase 服務用戶端程式庫都是最新版本。
執行 pod install
,然後開啟建立的 .xcworkspace
檔案。
2. 導入 App Check 通訊協定
首先,您需要建立實作 AppCheckProvider
和 AppCheckProviderFactory
通訊協定的類別。
您的 AppCheckProvider
類別必須具備 getToken(completion:)
方法,該方法會收集自訂 App Check 供應商要求的任何資訊,做為驗證證明,並將資訊傳送至權杖取得服務,以換取 App Check 權杖。App Check SDK 會處理權杖快取,因此請務必在 getToken(completion:)
的實作中取得新權杖。
Swift
class YourCustomAppCheckProvider: NSObject, AppCheckProvider {
var app: FirebaseApp
init(withFirebaseApp app: FirebaseApp) {
self.app = app
super.init()
}
func getToken() async throws -> AppCheckToken {
let getTokenTask = Task { () -> AppCheckToken in
// ...
// Create AppCheckToken object.
let exp = Date(timeIntervalSince1970: expirationFromServer)
let token = AppCheckToken(
token: tokenFromServer,
expirationDate: exp
)
if Date() > exp {
throw NSError(domain: "ExampleError", code: 1, userInfo: nil)
}
return token
}
return try await getTokenTask.value
}
}
Objective-C
@interface YourCustomAppCheckProvider : NSObject <FIRAppCheckProvider>
@property FIRApp *app;
- (id)initWithApp:(FIRApp *)app;
@end
@implementation YourCustomAppCheckProvider
- (id)initWithApp:app {
self = [super init];
if (self) {
self.app = app;
}
return self;
}
- (void)getTokenWithCompletion:(nonnull void (^)(FIRAppCheckToken * _Nullable,
NSError * _Nullable))handler {
dispatch_async(dispatch_get_main_queue(), ^{
// Logic to exchange proof of authenticity for an App Check token.
// ...
// Create FIRAppCheckToken object.
NSTimeInterval exp = expirationFromServer;
FIRAppCheckToken *token
= [[FIRAppCheckToken alloc] initWithToken:tokenFromServer
expirationDate:[NSDate dateWithTimeIntervalSince1970:exp]];
// Pass the token or error to the completion handler.
handler(token, nil);
});
}
@end
此外,請實作 AppCheckProviderFactory
類別,建立 AppCheckProvider
實作的例項:
Swift
class YourCustomAppCheckProviderFactory: NSObject, AppCheckProviderFactory {
func createProvider(with app: FirebaseApp) -> AppCheckProvider? {
return YourCustomAppCheckProvider(withFirebaseApp: app)
}
}
Objective-C
@interface YourCustomAppCheckProviderFactory : NSObject <FIRAppCheckProviderFactory>
@end
@implementation YourCustomAppCheckProviderFactory
- (nullable id<FIRAppCheckProvider>)createProviderWithApp:(FIRApp *)app {
return [[YourCustomAppCheckProvider alloc] initWithApp:app];
}
@end
3. 初始化 App Check
在應用程式委派或應用程式初始化程式中,加入下列初始化程式碼:
Swift
let providerFactory = YourAppCheckProviderFactory()
AppCheck.setAppCheckProviderFactory(providerFactory)
FirebaseApp.configure()
Objective-C
YourAppCheckProviderFactory *providerFactory =
[[YourAppCheckProviderFactory alloc] init];
[FIRAppCheck setAppCheckProviderFactory:providerFactory];
[FIRApp configure];
後續步驟
在應用程式中安裝 App Check 程式庫後,即可開始向使用者發布更新版應用程式。
更新後的用戶端應用程式會開始在每次向 Firebase 發出的要求中,一併傳送 App Check 權杖,但您必須在 Firebase 控制台的「App Check」App Check 部分啟用強制執行功能,Firebase 產品才會要求權杖有效。
監控指標並啟用強制執行功能
不過,啟用強制執行前,請先確認這麼做不會影響現有的合法使用者。另一方面,如果發現應用程式資源有可疑的使用情形,建議盡快啟用強制執行功能。
如要協助做出這項決定,您可以查看所用服務的 App Check 指標:
啟用App Check強制執行功能
瞭解 App Check 對使用者的影響後,即可啟用 App Check 強制執行:
在偵錯環境中使用 App Check
在註冊應用程式以使用 App Check 後,如果想在 App Check 通常不會歸類為有效的環境中執行應用程式 (例如開發期間的模擬器,或來自持續整合 (CI) 環境),可以建立應用程式的偵錯版本,使用 App Check 偵錯供應器,而非實際的認證供應器。
請參閱「在 Apple 平台上搭配使用 App Check 與偵錯供應器」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-23 (世界標準時間)。
[null,null,["上次更新時間:2025-08-23 (世界標準時間)。"],[],[],null,["This page shows you how to enable App Check in an Apple app, using [your\ncustom App Check provider](/docs/app-check/ios/custom-provider). When you enable App Check,\nyou help ensure that only your app can access your project's Firebase resources.\n\nIf you want to use App Check with the built-in providers, see the docs for\n[App Check with App Attest](/docs/app-check/ios/app-attest-provider) and\n[App Check with DeviceCheck](/docs/app-check/ios/devicecheck-provider).\n\nBefore you begin\n\n- [Add Firebase to your Apple project](/docs/ios/setup) if you haven't\n already done so.\n\n- [Implement your custom App Check provider's server-side logic](/docs/app-check/custom-provider).\n\n1. Add the App Check library to your app\n\n1. Add the dependency for App Check to your project's `Podfile`:\n\n ```\n pod 'FirebaseAppCheck'\n ```\n\n Or, alternatively, you can use [Swift Package\n Manager](/docs/ios/swift-package-manager) instead.\n\n Also, make sure you're using the latest version of any Firebase service\n client libraries you depend on.\n2. Run `pod install` and open the created `.xcworkspace` file.\n\n2. Implement the App Check protocols\n\nFirst, you need to create classes that implement the `AppCheckProvider` and\n`AppCheckProviderFactory` protocols.\n\nYour `AppCheckProvider` class must have a `getToken(completion:)` method, which\ncollects whatever information your custom App Check provider requires as\nproof of authenticity, and sends it to your token acquisition service in\nexchange for an App Check token. The App Check SDK handles token\ncaching, so always get a new token in your implementation of\n`getToken(completion:)`. \n\nSwift \n\n```swift\nclass YourCustomAppCheckProvider: NSObject, AppCheckProvider {\n var app: FirebaseApp\n\n init(withFirebaseApp app: FirebaseApp) {\n self.app = app\n super.init()\n }\n\n func getToken() async throws -\u003e AppCheckToken {\n let getTokenTask = Task { () -\u003e AppCheckToken in\n // ...\n\n // Create AppCheckToken object.\n let exp = Date(timeIntervalSince1970: expirationFromServer)\n let token = AppCheckToken(\n token: tokenFromServer,\n expirationDate: exp\n )\n\n if Date() \u003e exp {\n throw NSError(domain: \"ExampleError\", code: 1, userInfo: nil)\n }\n\n return token\n }\n\n return try await getTokenTask.value\n }\n\n}\n```\n\nObjective-C \n\n```objective-c\n@interface YourCustomAppCheckProvider : NSObject \u003cFIRAppCheckProvider\u003e\n\n@property FIRApp *app;\n\n- (id)initWithApp:(FIRApp *)app;\n\n@end\n\n@implementation YourCustomAppCheckProvider\n\n- (id)initWithApp:app {\n self = [super init];\n if (self) {\n self.app = app;\n }\n return self;\n}\n\n- (void)getTokenWithCompletion:(nonnull void (^)(FIRAppCheckToken * _Nullable,\n NSError * _Nullable))handler {\n dispatch_async(dispatch_get_main_queue(), ^{\n // Logic to exchange proof of authenticity for an App Check token.\n // ...\n\n // Create FIRAppCheckToken object.\n NSTimeInterval exp = expirationFromServer;\n FIRAppCheckToken *token\n = [[FIRAppCheckToken alloc] initWithToken:tokenFromServer\n expirationDate:[NSDate dateWithTimeIntervalSince1970:exp]];\n\n // Pass the token or error to the completion handler.\n handler(token, nil);\n });\n}\n\n@end\n```\n\nAlso, implement a `AppCheckProviderFactory` class that creates instances of your\n`AppCheckProvider` implementation: \n\nSwift \n\n```swift\nclass YourCustomAppCheckProviderFactory: NSObject, AppCheckProviderFactory {\n func createProvider(with app: FirebaseApp) -\u003e AppCheckProvider? {\n return YourCustomAppCheckProvider(withFirebaseApp: app)\n }\n}\n```\n\nObjective-C \n\n```objective-c\n@interface YourCustomAppCheckProviderFactory : NSObject \u003cFIRAppCheckProviderFactory\u003e\n@end\n\n@implementation YourCustomAppCheckProviderFactory\n\n- (nullable id\u003cFIRAppCheckProvider\u003e)createProviderWithApp:(FIRApp *)app {\n return [[YourCustomAppCheckProvider alloc] initWithApp:app];\n}\n\n@end\n```\n\n3. Initialize App Check\n\nAdd the following initialization code to your app delegate or app initializer: \n\nSwift \n\n```swift\nlet providerFactory = YourAppCheckProviderFactory()\nAppCheck.setAppCheckProviderFactory(providerFactory)\n\nFirebaseApp.configure()\n```\n\nObjective-C \n\n```objective-c\nYourAppCheckProviderFactory *providerFactory =\n [[YourAppCheckProviderFactory alloc] init];\n[FIRAppCheck setAppCheckProviderFactory:providerFactory];\n\n[FIRApp configure];\n```\n\nNext steps\n\nOnce the App Check library is installed in your app, start distributing the\nupdated app to your users.\n\nThe updated client app will begin sending App Check tokens along with every\nrequest it makes to Firebase, but Firebase products will not require the tokens\nto be valid until you enable enforcement in the App Check section of the\nFirebase console.\n\nMonitor metrics and enable enforcement\n\nBefore you enable enforcement, however, you should make sure that doing so won't\ndisrupt your existing legitimate users. On the other hand, if you're seeing\nsuspicious use of your app resources, you might want to enable enforcement\nsooner.\n\nTo help make this decision, you can look at App Check metrics for the\nservices you use:\n\n- [Monitor App Check request metrics](/docs/app-check/monitor-metrics) for Firebase AI Logic, Data Connect, Realtime Database, Cloud Firestore, Cloud Storage, Authentication, Google Identity for iOS, Maps JavaScript API, and Places API (New).\n- [Monitor App Check request metrics for Cloud Functions](/docs/app-check/monitor-functions-metrics).\n\nEnable App Check enforcement\n\nWhen you understand how App Check will affect your users and you're ready to\nproceed, you can enable App Check enforcement:\n\n- [Enable App Check enforcement](/docs/app-check/enable-enforcement) for Firebase AI Logic, Data Connect, Realtime Database, Cloud Firestore, Cloud Storage, Authentication, Google Identity for iOS, Maps JavaScript API, and Places API (New).\n- [Enable App Check enforcement for Cloud Functions](/docs/app-check/cloud-functions).\n\nUse App Check in debug environments\n\nIf, after you have registered your app for App Check, you want to run your\napp in an environment that App Check would normally not classify as valid,\nsuch as a simulator during development, or from a continuous integration (CI)\nenvironment, you can create a debug build of your app that uses the\nApp Check debug provider instead of a real attestation provider.\n\nSee [Use App Check with the debug provider on Apple platforms](/docs/app-check/ios/debug-provider)."]]