gpt4 book ai didi

swift - Swift 中的 Firebase 函数调用返回值

转载 作者:行者123 更新时间:2023-11-28 13:27:58 28 4
gpt4 key购买 nike

我正尝试像使用 Redux JS 一样使用 Swift 访问从 Firebase 函数 twilioToken 返回的 token 。我附上了我用于 JS 的代码,这样我就可以用 Swift 模拟它,但不确定如何从 firebase 函数调用中访问 result.token。我在这里错过了什么吗?我是否需要以不同方式从 https 请求中获取值,或者我是否使用当前代码关闭?让我知道是否需要详细说明,谢谢!

output.token 的当前错误是Value of tuple type 'Void' has no member 'token'

尝试的 Swift 代码:

import UIKit
import MBProgressHUD
import FirebaseFunctions

class CallRoomVC: UIViewController {
private var appDelegate: AppDelegate!
private var userSession: UserSession = FirebaseUserSession.shared

lazy var functions = Functions.functions()

override func viewDidLoad() {
super.viewDidLoad()
appDelegate = UIApplication.shared.delegate as? AppDelegate

}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

guard let user = userSession.user else {
fatalError("User instance must be created")
}

var output = functions.httpsCallable("twilioToken").call(["uid": user.id]) { (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]
}
// ...
// or
// print("Result: \(result.token)")
}

}
if (output != nil) {
print("Result: \(output.token)")
}
}
}

Firebase 函数:

"use strict";
const functions = require("firebase-functions");
const admin = require("firebase-admin");
// eslint-disable-next-line import/no-extraneous-dependencies
const google_cloud_logging = require("@google-cloud/logging");
const twilio = require("twilio");
const cors = require("cors")({
origin: true
});
// eslint-disable-next-line import/no-extraneous-dependencies
admin.initializeApp(functions.config().firebase);

exports.twilioToken = functions.https.onRequest((req, res) => {
return cors(req, res, () => {
const token = new twilio.jwt.AccessToken(
"xxxxxxxxxxxxxx", // Account ID
"xxxxxxxxxxxxxx", // API Key SID
"xxxxxxxxxxxxxx" // API Key Secret
);
token.identity = req.query.uid;
token.addGrant(new twilio.jwt.AccessToken.VideoGrant());
console.log("Sending token: ", token);
res.status(200).send({ token: token.toJwt() });
});
});

JS Redux 代码:

function* getTokenSaga(action) {
const token = yield call(
rsf.functions.call,
"twilioToken",
{
uid: action.uid
},
{
method: "GET"
}
);

yield put(retrievedToken(token.token));
}

export function* twilioRootSaga() {
yield all([takeEvery(types.TOKEN.GET, getTokenSaga)]);
}

最佳答案

此处为 Twilio 开发人员布道师。

您对 Firebase 函数的调用是异步的,因为它正在发出 HTTP 请求。结果不会返回到您的 output 变量,但可以在回调中作为 result 对象使用。你需要使用那个 result 来代替,像这样:

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

guard let user = userSession.user else {
fatalError("User instance must be created")
}

functions.httpsCallable("twilioToken").call(["uid": user.id]) { (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 token = (result?.data as? [String: Any])?["token"] as? String {
print("Result: \(token)")
}
}

}

此示例改编自 the Firebase documentation here .

如果有帮助,请告诉我。

关于swift - Swift 中的 Firebase 函数调用返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58018002/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com