Firebase Cloud Functions 不会从 iOS 应用程序触发

Jam*_*ley 6 firebase swift google-cloud-functions

我正在使用 Firebase 作为后端构建一个 iOS 应用程序,并且需要使用 Firebase Callable Cloud Functions。我已经按照文档设置了所有内容,并且功能在从浏览器或 curl 启动时会触发并按预期运行,但我无法让它们从我的应用程序中触发。

我已经设置了一个仅使用这些功能的测试应用程序,但我也无法从 iOS 应用程序中触发它。甚至没有一个基本的 hello world。什么都没有通过。

已完成以下所有操作:

Firebase 方面:

  1. Firebase 已成功连接到应用
  2. 在本地安装/初始化 Firebase 函数。
  3. 在 VSCode 中尝试 Javascript 和 TypeScript 的书面函数代码
  4. 已成功部署到 Firebase 并可以在控制台中看到功能
  5. 通过命令行和浏览器在本地服务器上测试的功能都按预期工作

iOS端:

  1. Pod 安装的 Firebase 函数
  2. 将 Firebase 函数导入 ViewController
  3. 使用 Firebase SDK 在 Firebase 上调用与函数名称匹配的函数 - 附加到按钮触发器

我什么也没得到......按钮上的打印语句有效,但该功能不会触发,并且没有任何内容记录到 Firebase 日志中。

我在这里错过了什么或做错了什么?

使用新的 Firebase 实例尝试了新项目。直接从 Firebase 文档上的示例复制代码,并一步一步地遵循所有内容

火源代码

const functions = require('firebase-functions');
const admin = require('firebase-admin')

exports.helloWorld = functions.https.onCall((data, context) => {

    const text = data.text;
    console.log("Text: " + text);
    const uid = context.uid;
    console.log("UID: " + uid);
    const name = context.name;
    console.log("Name: " + name);


    console.log('Hello world fucking worked baby!');
    return {
        message: text
    }
});
Run Code Online (Sandbox Code Playgroud)

SWIFT代码

import UIKit
import FirebaseFunctions

class ViewController: UIViewController {

    lazy var functions = Functions.functions()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func fireFunction(_ sender: Any) {
        print("Button Fire")

        let data = ["text": "hello!"]

        functions.httpsCallable("helloWorld").call(data) { (result, error) in
            print("Function returned")
            if let err = error {
                print(err)
            }

            if let res = result {
                print(res)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

And*_*nir -1

Google Cloud Function如果从浏览器调用它,它似乎工作正常。您还可以查看云功能的LOG。只需转到该Cloud Functions页面并单击您的功能即可。单击该VIEW LOGS按钮并尝试再次调用该 URL,如果 Swift 调用方式有错误,则会记录在那里。

我没有经常使用 iOS Swift,但由于您尝试通过互联网调用该函数,我认为您可能需要授予应用程序访问互联网的权限。看起来该调用从未在云功能中发起,这就是它没有被触发的原因。

我假设云函数是 HTTP 触发的。因此,作为解决方法,您可以调用从应用程序发起 HTTP 请求的函数。同样,除非您确保您的应用程序有权使用互联网,否则这将不起作用。