Executing native code in Electron app (Node.JS / Javascript)
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

In recent MacOS and iOS apps, Apple gave developers an easy way to just "ask for a review" using a simple requestReview() API call.

To give users an easy way to provide feedback on the App Store or the Mac App Store, use the SKStoreReviewController API. It's a simple API call with not much back and forth needed. We need this implemented in an Electron app.

You can read more about the SKStoreReviewController API here:

https://developer.apple.com/app-store/ratings-and-reviews/

The requestReview() API is a very simple API -- MacOS does the heavy lifting once the API is called. For the app developer, it's just a native macos API call, which we need to call from within an Electron app, which is a node.js Javascript app. We don't expect this to be a very involved solution.

Getting the bounty:

A proper solution will be source code that we can add to an Electron app that allows us to call the MacOS requestReview() API from within the javascript source, and get a response from the API call to see if it executed successfully or not. Plus simple instructions on how to use it.


As one possible solution to get you started, here's a Cordova plugin that does this for requestReview: https://github.com/kenmickles/cordova-plugin-request-review . There are two main files, one javascript and another in Swift.

cordova-plugin-request-review/www/RequestReview.js:

var exec = require('cordova/exec');

exports.requestReview = function(arg0, success, error) {
    exec(success, error, "RequestReview", "requestReview", [arg0]);
};

cordova-plugin-request-review/src/ios/RequestReview.swift:

import StoreKit

@objc(RequestReview) class RequestReview : CDVPlugin {
    @objc(requestReview:)
    func requestReview(command: CDVInvokedUrlCommand) {
        var pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR)

        if #available(iOS 10.3, *) {
            SKStoreReviewController.requestReview()
        }

        pluginResult = CDVPluginResult(status: CDVCommandStatus_OK)
        self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
    }
}

Via cordova-plugin-request-review/plugin.xml, The requestReview function in cordova-plugin-request-review/www/RequestReview.js is exposed to the client as function SKStoreReviewController.requestReview. So the client just needs to call SKStoreReviewController.requestReview() in order to execute the native requestReview() function.

Although this is taken from a cordova app meant for iOS, that same exact Swift code should work on MacOS with a small modification to check for MacOS version instead of iOS version, so you shouldn't need to write any new Swift code for this to work on desktop. You may just need to find an electron / node.js alternative to cordova's "exec" command that can execute the pre-written Swift file, and write the wrapper javascript for that. Maybe this is something that can be easily done using node's child_process.execFile. The swift code could be stored inside the electron app as a separate file, just like in the cordova example, and called from within the app using node's child process.

The first working solution gets the bounty.

Hi! I saw that you have trouble finding a solution to this problem. Unfortunately, I don't have a MacOS so I can't write code for this but it shouldn't be hard with this Node.js package https://github.com/noppoMan/node-native-extension-in-swift We name our function with @_cdecl call and after that statement, we define a normal Swift function which we register in C++. In our JavaScript file, a simple require gets the job done.
VladimirMikulic 25 days ago
Will take a look. There were some other packages dating back a few years ago, but they became abandonware and users reported they were dysfunctional.
LDubya 25 days ago
awarded to drakmail

Crowdsource coding tasks.

1 Solution

Winning solution

Hello,

you may see the complete solution at my GitHub repo: https://github.com/drakmail/electron-native-code . Tested on node.js v12 and electron 8.2.5 (should run on any electron version which uses node.js 12+).

It has dependency on https://github.com/nsofnetworks/objc package.

After installing native dependencies you need to run ./node_modules/.bin/electron-rebuild

Thanks drakmail, I will implement this and review. A couple appointments tomorrow so hopefully by Saturday
LDubya 16 days ago
Awesome! If you use earlier versions of electron you may change objc package to https://github.com/lukaskollmer/objc
drakmail 16 days ago
Any news?
drakmail 12 days ago
@drakmail thanks for the reminder. Although we haven’t gotten time to test it in-app, it is the best solution that we have so you’ll get the bounty, but is there a way to contact you in case we have a quick question or would like to commission further work?
LDubya 12 days ago
sure, you can reach me by email alex at maslov.dev
drakmail 12 days ago