Mulai menggunakan App Check dengan penyedia kustom di platform Apple
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Halaman ini menunjukkan cara mengaktifkan App Check di aplikasi Apple menggunakan penyedia App Check kustom. Mengaktifkan App Check akan membantu memastikan bahwa hanya aplikasi Anda yang dapat mengakses resource Firebase project Anda.
Selain itu, pastikan library klien layanan Firebase yang Anda gunakan sudah dalam versi terbaru.
Jalankan pod install, lalu buka file .xcworkspace yang dihasilkan.
2. Terapkan protokol App Check
Pertama, Anda harus membuat class yang menerapkan protokol AppCheckProvider dan AppCheckProviderFactory.
Class AppCheckProvider Anda harus memiliki metode getToken(completion:), yang mengumpulkan semua informasi yang diperlukan oleh penyedia App Check kustom Anda sebagai bukti keaslian, lalu mengirimkannya ke layanan akuisisi token Anda sebagai pengganti token App Check. App Check SDK menangani penyimpanan token ke dalam cache, jadi selalu dapatkan token baru dalam penerapan getToken(completion:) Anda.
@interfaceYourCustomAppCheckProvider : NSObject<FIRAppCheckProvider>
@propertyFIRApp*app;-(id)initWithApp:(FIRApp*)app;@end@implementationYourCustomAppCheckProvider-(id)initWithApp:app{self=[superinit];if(self){self.app=app;}returnself;}-(void)getTokenWithCompletion:(nonnullvoid(^)(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.NSTimeIntervalexp=expirationFromServer;FIRAppCheckToken*token=[[FIRAppCheckTokenalloc]initWithToken:tokenFromServerexpirationDate:[NSDatedateWithTimeIntervalSince1970:exp]];// Pass the token or error to the completion handler.handler(token,nil);});}@end
Selain itu, terapkan class AppCheckProviderFactory yang membuat instance dari penerapan AppCheckProvider:
Setelah library App Check terinstal di aplikasi Anda, mulai distribusikan aplikasi yang telah diupdate kepada pengguna.
Aplikasi klien yang telah diupdate akan mulai mengirimkan token App Check beserta setiap permintaan yang dibuatnya ke Firebase. Namun, sebelum Anda mengaktifkan penerapan di bagian App Check pada Firebase console, token tersebut tidak harus valid untuk produk Firebase.
Memantau metrik dan mengaktifkan penerapan
Sebelum mengaktifkan penerapan, Anda harus memastikan bahwa tindakan tersebut tidak akan mengganggu pengguna sah yang sudah ada. Di sisi lain, jika Anda melihat penggunaan resource aplikasi yang mencurigakan, sebaiknya segera aktifkan penerapan.
Demi membantu mengambil keputusan ini, Anda dapat melihat metrik App Check untuk layanan yang Anda gunakan:
Pantau metrik permintaan App Check untuk Firebase AI Logic, Data Connect, Realtime Database, Cloud Firestore, Cloud Storage, Authentication, Google Identity untuk iOS, Maps JavaScript API, dan Places API (Baru).
Setelah memahami pengaruh App Check terhadap pengguna dan siap melanjutkan, Anda dapat mengaktifkan penerapan App Check:
Mengaktifkan penerapan App Check untuk
Firebase AI Logic, Data Connect, Realtime Database, Cloud Firestore, Cloud Storage, Authentication, Google Identity untuk iOS, Maps JavaScript API, dan Places API (Baru).
Jika Anda telah mendaftarkan aplikasi untuk App Check dan ingin menjalankan aplikasi di lingkungan yang biasanya tidak akan diklasifikasikan sebagai valid oleh App Check, seperti simulator selama pengembangan atau dari lingkungan continuous integration (CI), Anda dapat membuat build debug aplikasi yang menggunakan penyedia debug App Check, bukan penyedia pengesahan sungguhan.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-05 UTC."],[],[],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)."]]