How to run Node JS code from npm inside of Swift
That's a great question. I will try to give you an overview of how these different systems work and a theoretical, but rather painful solution. Swift runs on the mobile end and manages its resources on a more low level approach.
Javascript on the other hand is a general purpose language that requires a Virtual Machine(VM) that "understands" javascript, to be embedded into the system you are targeting. Thankfully, iOS has already done that with javascriptcore interface. With it, you can load up a Javascript bundle and evaluate any function from it.
In theory, what you are suggesting would work like this:
Find a bundled version of the package you want to run
Load it up on the javascript VM using the JavaScriptCore interface
Evaluate the function of the package
Check out this great guide
The great caveat of this approach is the platform specific calls of the npm bundle (for example Node.js' fs
package does not exist).
If that is the case then you should use a remote service that runs the code for your as cenk suggested.
Can I run JavaScript inside Swift code?
Last tested with Swift 5.1
Here is an example you can run in Playground to get you started:
import JavaScriptCore
let jsSource = "var testFunct = function(message) { return \"Test Message: \" + message;}"
var context = JSContext()
context?.evaluateScript(jsSource)
let testFunction = context?.objectForKeyedSubscript("testFunct")
let result = testFunction?.call(withArguments: ["the message"])
result
would be Test Message: the message
.
You also can run JavaScript code within a WKWebView calling evaluateJavaScript(_:completionHandler:).
You can also run JavaScript within a UIWebView by calling stringByEvaluatingJavaScript(from:), but note that that method has been deprecated and is marked as iOS 2.0–12.0.
run nodejs script in xcode run script
instead of node /path/
i had to use the path to the node binary e.g. /usr/local/bin/node "$SRC_DIR/deployment/deploy.js"
Swift encryption and NodeJS decryption producing inconsistent results
Look for cross plate-form AES encryption
Connect a Cloud Function to an iOS app to send mass push notifications at once
How do I connect with Cloud Functions from within an iOS app?
You can call Cloud Functions by doing the same thing that you would do calling any API, you will have to design your Cloud Functions to be Callable Functions, you can find more details on them and how to set it all up, in Swift and other languages, in this Documentation.
No matter what I have to get each follower from the "followers" ref and then I have to get each follower's deviceToken from within their "users" ref, I'm not sure where to start here and how do I actually send a push notification code once inside Cloud Functions? I found this answer but it's in javascript. I don't know javascript but I do know a tad bit of Node.js
These 2 questions can be answered together by this community answer that states that you should integrate Firebase Cloud Messaging into your iOS app, with a link to the full documentation on that subject. Also, you can find in this Documentation how you can send notifications to multiple devices, in there you will also find an example of the code that you will need to use in the Cloud Function itself using the Admin SDK.
NOTE: Cloud Functions can be written in Node.js, Go, Java and Python, and all the sample codes for the Cloud functions are in those languages.
Related Topics
Reading JavaScript Variable into Shiny/R on App Load
Js Replace Not Working on String
Rails 5 - Turbolinks 5,Some Js Not Loaded on Page Render
Sort an Array of Object by a Property (With Custom Order, Not Alphabetically)
How to Save a Leaflet Map with Drawn Shapes/Points on It in Shiny
Set Default Value of JavaScript Object Attributes
Youtube Iframe Player API - Onstatechange Not Firing
How to Implement Inheritance in Js Revealing Prototype Pattern
How to Split Comma Separated String Using JavaScript
Change Second Select List Based on First Select List Value in Rails
Dt: Link Binding Is Lost After Re-Rendering the Table
Applying Classname/Onclick/Onchange Not Working on Custom React Component
How to Use JavaScript in Ruby on Rails
What Does It Mean That JavaScript Is a Prototype Based Language
What Is Wkerrordomain Error 4 from Wkwebview
Can Jquery .Keypress() Detect More Than One Key at the Same Time
How to Pre-Populate a Jquery Datepicker Textbox with Today's Date