我使用 Firebase 创建了一个可用的 Cloud Function,它可以使用我的浏览器工作。现在,我正在使用我的 iOS Swift 代码,并已成功安装所有依赖项。
但是,我是 iOS/Swift 的新手,想知道从哪里调用 Cloud Function 的 URL?以下是 Firebase 提供的用于从 iOS 应用程序中调用的代码:
functions.httpsCallable("addMessage").call(["text": "test"]) { (result, error) in
if let error = error as NSError? {
if error.domain == FunctionsErrorDomain {
let code = FunctionsErrorCode(rawValue: error.code)
let message = error.localizedDescription
let details = error.userInfo[FunctionsErrorDetailsKey]
}
// ...
}
if let text = (result?.data as? [String: Any])?["text"] as? String {
print(text) // WOULD EXPECT A PRINT OF THE CALLABLE FUNCTION HERE
}
}
这是可调用的 Cloud Function(已部署):
exports.addMessage = functions.https.onCall((data, context) => {
const text = data.text;
return {
firstNumber: 1,
secondNumber: 2,
operator: '+',
operationResult: 1 + 2,
};
});
截至目前,我在 XCode 控制台中看不到任何打印内容,除了可调用函数。谢谢!
最佳答案
听起来您可能正在使用 HTTP 请求 Cloud Functions。 HTTP callable Cloud Functions s 与 HTTP request Cloud Functions 不同.
注意 HTTP 可调用 Cloud Functions 的签名:
exports.addMessage = functions.https.onCall((data, context) => {
// ...
});
与 HTTP 请求 Cloud Functions 相比:
exports.date = functions.https.onRequest((req, res) => {
// ...
});
如果您正在使用onRequest
,您将必须生成一个HTTP request。来自客户。如果您使用的是可调用函数,那么您只需传递函数名称和数据,如示例中所示。从您显示的链接来看,它类似于
functions.httpsCallable("testFunction").call(["foo": "bar"]) { (result, error) in
//...
}
关于ios - Firebase HTTPs 可调用 iOS Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51159634/