Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase HTTPs Callable 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.

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,
  };
  });

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

like image 919
jKraut Avatar asked Oct 23 '25 14:10

jKraut


1 Answers

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.

Notice the signature of HTTP callable Cloud Functions:

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

versus HTTP request Cloud Functions:

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

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
//...
}
like image 129
Jen Person Avatar answered Oct 26 '25 04:10

Jen Person



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!