Ios 获取firebase服务器时间云函数
我正在尝试编写一个函数,我可以在iOS应用程序上调用该函数,以返回我当前(或近似)的firebase服务器时间 以下几次尝试都没有成功。我做错什么了吗 Obs.:第二个返回类型为FirHttpCallableResult的对象,我找不到解析它的方法,也无法查看其中的内容以确定它是否有效Ios 获取firebase服务器时间云函数,ios,swift,google-cloud-functions,Ios,Swift,Google Cloud Functions,我正在尝试编写一个函数,我可以在iOS应用程序上调用该函数,以返回我当前(或近似)的firebase服务器时间 以下几次尝试都没有成功。我做错什么了吗 Obs.:第二个返回类型为FirHttpCallableResult的对象,我找不到解析它的方法,也无法查看其中的内容以确定它是否有效 exports.currentTime = functions.https.onRequest((req, res) => { res.send({"timestamp":new Date().ge
exports.currentTime = functions.https.onRequest((req, res) => {
res.send({"timestamp":new Date().getTime()})
});
exports.currentTimeTwo = functions.https.onCall((data, context) => {
return {"timestamp":new Date().getTime()};
});
exports.currentTimeThree = functions.https.onRequest((req, res) => {
const data = {"timestamp":new Date().getTime()}
res.send(data)
});
iOS代码:
static func getServerTime(){
Functions.functions().httpsCallable("currentTimeTwo").call { (result, error) in
if let error = error as NSError? {
print("error \(error)")
if error.domain == FunctionsErrorDomain {
print("error domain: \(error.domain)")
let code = FunctionsErrorCode(rawValue: error.code)
print("error code: \(code)")
let message = error.localizedDescription
print("error message: \(message)")
let details = error.userInfo[FunctionsErrorDetailsKey]
print("details: \(details)")
}
}
print(result)
}
}
上面的函数是正确的。正如Frank在我的问题评论中所告知的,我无法从swift代码结果中访问财产数据
新Swift代码:
static func getServerTime(){
Functions.functions().httpsCallable("currentTimeTwo").call { (result, error) in
if let error = error as NSError? {
print("error \(error)")
if error.domain == FunctionsErrorDomain {
print("error domain: \(error.domain)")
let code = FunctionsErrorCode(rawValue: error.code)
print("error code: \(code)")
let message = error.localizedDescription
print("error message: \(message)")
let details = error.userInfo[FunctionsErrorDetailsKey]
print("details: \(details)")
}
}
let resultFireBase = result as! HTTPSCallableResult
print(result?.data)
}
}
}
云函数代码中只有
functions.https.onCall
与Swift代码中的functions.functions().httpscalable
匹配,因此其他的在这里没有意义
当您调用
httpscalable
时,您会得到一个属性,它的数据
属性包含您的云函数返回的任何内容,只要它是有效的JSON类型,您的新日期().getTime()
似乎只有函数.https.onCall
与函数.functions()匹配.httpscalable
在您的Swift代码中,因此其他代码在这里没有意义。调用该函数时,返回的result
和error
的值是什么?请注意,所有包含的都是data
属性,该属性包含云函数返回的任何内容,只要它是有效的JSON类型,您的new Date().getTime()
似乎就是这样。谢谢Frank,我不知道应该使用data属性来访问结果的内容。现在它可以与for functions.https.onCall一起正常工作
static func getServerTime(){
Functions.functions().httpsCallable("currentTimeTwo").call { (result, error) in
if let error = error as NSError? {
print("error \(error)")
if error.domain == FunctionsErrorDomain {
print("error domain: \(error.domain)")
let code = FunctionsErrorCode(rawValue: error.code)
print("error code: \(code)")
let message = error.localizedDescription
print("error message: \(message)")
let details = error.userInfo[FunctionsErrorDetailsKey]
print("details: \(details)")
}
}
let resultFireBase = result as! HTTPSCallableResult
print(result?.data)
}
}
}