Firebase HTTPs可调用的iOS Swift [英] Firebase HTTPs Callable iOS Swift

查看:91
本文介绍了Firebase HTTPs可调用的iOS Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Firebase创建了可行的Cloud Function,并且可以在我的浏览器中正常工作.现在,我正在使用我的iOS Swift代码,并且已经成功安装了所有依赖项.

I've create a workable Cloud Function using Firebase in which works using my browser. Now, I'm working with my iOS Swift code, and have successfully installed all dependencies.

但是,我是iOS/Swift的新手,试图找出从Cloud Function调用URL的位置?这是Firebase提供的从iOS应用程序内调用的代码:

However, I'm new to iOS/Swift and try to figure out where to call the URL from the Cloud Function? Here is the code Firebase provides to call from within an iOS App:

  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
  }
}

这是可调用的云功能(已部署):

Here's the callable Cloud Function (which is deployed):

    exports.addMessage = functions.https.onCall((data, context) => {
const text = data.text;
return {
    firstNumber: 1,
    secondNumber: 2,
    operator: '+',
    operationResult: 1 + 2,
  };
  });

到目前为止,我在XCode控制台中看不到任何内容,希望可以调用该函数.谢谢!

As of now, I see nothing printed in my XCode console, expect the callable function. Thank you!

推荐答案

听起来您可能正在使用HTTP请求Cloud Function. HTTP可调用的云函数

It sounds like you may be using an HTTP request Cloud Function. HTTP callable Cloud Functionss are not the same thing as HTTP request Cloud Functions.

请注意HTTP可调用云功能的签名:

Notice the signature of HTTP callable Cloud Functions:

exports.addMessage = functions.https.onCall((data, context) => {
  // ...
});

相对于HTTP请求云功能:

versus HTTP request Cloud Functions:

exports.date = functions.https.onRequest((req, res) => {
  // ...
});

如果您使用的是onRequest,则必须创建

If you're using onRequest, you will have to make an HTTP request from the client. If you're using a callable function, then you just pass the function name and data as shown in the sample. Judging from the link you showed, it would be something like

  functions.httpsCallable("testFunction").call(["foo": "bar"]) { (result, error) in
//...
}

这篇关于Firebase HTTPs可调用的iOS Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆