Firebase HTTPs Callable iOS Swift

最后都变了- 提交于 2020-01-15 03:24:11

问题


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!


回答1:


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



回答2:


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



回答3:


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




回答4:


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
}


来源:https://stackoverflow.com/questions/51159634/firebase-https-callable-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!