如何从iOS应用程序内的Firebase云函数设置和访问环境变量(专用api密钥)
我使用Firebase作为后端、远程通知,并在iOS应用程序中使用谷歌地图和Places API。一切正常,但我想 要发送远程通知,我需要Firebase的服务器密钥;要访问Google Maps和Places API,我需要他们的API凭据如何从iOS应用程序内的Firebase云函数设置和访问环境变量(专用api密钥),ios,node.js,swift,google-cloud-functions,Ios,Node.js,Swift,Google Cloud Functions,我使用Firebase作为后端、远程通知,并在iOS应用程序中使用谷歌地图和Places API。一切正常,但我想 要发送远程通知,我需要Firebase的服务器密钥;要访问Google Maps和Places API,我需要他们的API凭据 @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? static var fbServerKe
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
static var fbServerKey = "someReallyLongFirebaseServerKey"
override init() {
super.init()
Messaging.messaging().delegate = self
FirebaseApp.configure()
GMSPlacesClient.provideAPIKey("my_Places_API_Key") // places api credential key
GMSServices.provideAPIKey("my_Maps_API_Key") // maps api credential key
GMSPlacesClient.openSourceLicenseInfo()
GMSServices.openSourceLicenseInfo()
GADMobileAds.sharedInstance().start(completionHandler: nil)
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
}
}
要发送推送通知,我从AppDelegate访问fbServerKey
:
let fbServerKey = AppDelegate.fbServerKey // *** 1. the server key is accessed here and used below ***
var params = [String: Any]() // add keys and values to params
var request = URLRequest(url: URL(string: "https://fcm.googleapis.com/fcm/send")!)
request.httpMethod = "POST"
// *** 2. the server key is used HERE ***
request.setValue("key=\(fbServerKey)", forHTTPHeaderField: "Authorization")
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in ...
我有我的Firebase规则,但是如果你看上面的代码,fbServerKey和maps/places API凭证都是纯文本的
为了避免这个问题,我转而使用CloudKit:
// these keys are all set prior to going live and won't be inside the user's applications
NSUbiquitousKeyValueStore().set("someReallyLongFirebaseServerKey", forKey: "fbServerKey")
NSUbiquitousKeyValueStore().set("my_Places_API_Key", forKey: "placesAPIKey")
NSUbiquitousKeyValueStore().set("my_Maps_API_Key", forKey: "mapsAPIKey")
NSUbiquitousKeyValueStore().synchronize()
要检索密钥,请执行以下操作:
static var fbServerKey = NSUbiquitousKeyValueStore()string(forKey: "fbServerKey")
GMSPlacesClient.provideAPIKey(NSUbiquitousKeyValueStore()string(forKey: "placesAPIKey"))
GMSServices.provideAPIKey(NSUbiquitousKeyValueStore()string(forKey: "mapsAPIKey"))
这里的问题是,如果用户未连接到iCloud,他们将无法访问密钥。
现在我正在切换到Firebase云功能。问题是我不清楚如何使用它
1-在终端Icd
中,转到我的Xcode项目文件夹并运行
// $ firebase init functions *** I initially used this command but DO NOT RUN THIS line >firebase init functions<. Read the first comment below from @DougStevenson for the reason to avoid it
$ npm install -g firebase-tools
$ npm install --save firebase-functions@latest
4-在提供的functions/index.js
文件夹中,我添加了一些代码来设置步骤3中的代码
const functions = require('firebase-functions'); // already present
// don't know what to add here???
5-要部署我运行的代码,请执行以下操作:
$ firebase deploy --only functions
6-既然代码在云中,我可以从我的应用程序中调用代码来安全访问密钥
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
static var fbServerKey = somehow_Call_The_Firebase_GetFunction_For_This_Server_Key
override init() {
super.init()
Messaging.messaging().delegate = self
FirebaseApp.configure()
GMSPlacesClient.provideAPIKey(somehow_Call_The_Firebase_GetFunction_For_This_Places_Key) // places api credential key
GMSServices.provideAPIKey(somehow_Call_The_Firebase_GetFunction_For_This_Maps_Key) // maps api credential key
GMSPlacesClient.openSourceLicenseInfo()
GMSServices.openSourceLicenseInfo()
GADMobileAds.sharedInstance().start(completionHandler: nil)
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
}
}
我需要步骤3、4和6的帮助我不会在我的xcode项目中运行
firebase init
。我会把它放在一个单独的位置,这样它就不可能和xcode的东西发生冲突。好的,谢谢道格,我将从我的问题中删除它。我不知道
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
static var fbServerKey = somehow_Call_The_Firebase_GetFunction_For_This_Server_Key
override init() {
super.init()
Messaging.messaging().delegate = self
FirebaseApp.configure()
GMSPlacesClient.provideAPIKey(somehow_Call_The_Firebase_GetFunction_For_This_Places_Key) // places api credential key
GMSServices.provideAPIKey(somehow_Call_The_Firebase_GetFunction_For_This_Maps_Key) // maps api credential key
GMSPlacesClient.openSourceLicenseInfo()
GMSServices.openSourceLicenseInfo()
GADMobileAds.sharedInstance().start(completionHandler: nil)
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
}
}