Skip to content

Instantly share code, notes, and snippets.

@hhyyg
Last active November 17, 2020 09:16
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhyyg/c1481853bcac27a678f707c0d1afd0f4 to your computer and use it in GitHub Desktop.
Save hhyyg/c1481853bcac27a678f707c0d1afd0f4 to your computer and use it in GitHub Desktop.
How to access current firebase user from iOS App Extension.
import Foundation
import KeychainAccess //https://github.com/kishikawakatsumi/KeychainAccess
import Firebase
//Requires shared Keychain with the same group name
class ApplicationBuilder {
enum Target {
case app
case appExtension
}
private static let appKeychain = Keychain(service: "firebase_auth_{hosted app firebase application id}") //need replace with GOOGLE_APP_ID from GoogleService-Info.plist
private static let extensionKeychain = Keychain(service: "firebase_auth_{app extension firebase application id}") //need replace with GOOGLE_APP_ID from GoogleService-Info.plist
private static let userKeychainKey = "firebase_auth_1___FIRAPP_DEFAULT_firebase_user" //__FIRAPP_DEFAULT is FirebaseApp.name
private static var target: Target = .app
static func setup(target: Target, completion: @escaping () -> Void) {
ApplicationBuilder.target = target
if FirebaseApp.app() == nil {
FirebaseApp.configure()
}
if target == .appExtension {
syncFirebaseKeychain()
}
}
private static func syncFirebaseKeychain() {
guard let data = (try? appKeychain.getData(userKeychainKey)) as? Data else {
try! extensionKeychain.remove(userKeychainKey)
return
}
try? extensionKeychain.set(data, key: userKeychainKey)
}
//...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment