jKraut
jKraut

Reputation: 2487

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!

Upvotes: 4

Views: 4980

Answers (4)

Shyam
Shyam

Reputation: 317

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
}

Upvotes: 0

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'.

Upvotes: 0

jKraut
jKraut

Reputation: 2487

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

Upvotes: 1

Jen Person
Jen Person

Reputation: 7546

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

Upvotes: 3

Related Questions