3
votes

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!

4
I don't understand - the URL for the callable function is determined and invoked by the client SDK. You don't need to know it. Maybe you should post both your function and client code in case there is some confusion.Doug Stevenson
@DougStevenson - added Function code. Using the SDK then, how does it make the call to the function named 'testFunction'jKraut
It's like Jen said in her answer. There is a difference between "callable" functions and regular HTTPS functions. You have to define a special callable function on the server in order to make it easily callable like the client code you're showing. If you write an HTTPS function on the server (as you've shown in your question), you should invoke it with your own HTTP client library instead.Doug Stevenson
@DougStevenson - makes sense now, I updated my Firebase function to callable (see update above). As of now, not returning the result as I would expect.jKraut

4 Answers

3
votes

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
//...
}
0
votes

I figured about the problem. I had to update my Cloud Function return key to match my Swift function. Here is TypeScript code:

exports.addMessage = functions.https.onCall((data, context) => {
const text = data.text;
console.log(text)

return {
    text: "100"
  };
0
votes

Hope this works. Make sure to add a 'text' element on the return part of your callable Cloud Function, for example:

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

In your code, you're returning variables which you're not using, such as 'firstNumber', 'secondNumber', 'operator', and 'operationResult', and your forgetting to add the important variable, which is 'text'.

0
votes

instead of (result?.data as? [String: Any])?["text"] as? String use result?.data

finally it look something like this

if let text = (result?.data) {
    print(text)  // WOULD EXPECT A PRINT OF THE CALLABLE FUNCTION HERE
}