Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

317
Visualizações
How to run JS script which has import from npm-library on iPhone by Content View?

I have this code and node_modules in shared folder in x code project. It have to show me text key generated from js-code. js-code works outside by node.js, but it shows me 'undefined' in Content View. Can I import js modules in x code project? Will it work on iPhone?

//  ContentView.swift
//  Shared

import Foundation
import SwiftUI
import JavaScriptCore

class Cube {
    func doJS(text: String) -> String? {
        let jsSource = "var testFunct = function test(message) { const NodeRSA = require('node-rsa'); const key = new NodeRSA(); var k = key.generateKeyPair(); var p = k.exportKey('pkcs1'); return p};"

        let context = JSContext()
        context?.evaluateScript(jsSource)

        let testFunction = context?.objectForKeyedSubscript("testFunct")
        return testFunction?.call(withArguments: [text]).toString()
    }
}

struct ContentView: View {
    var body: some View {
        Text(Cube().doJS(text: "Hello, world!") ?? "No result")
            .fontWeight(.black)
            .foregroundColor(Color.green)
            .padding()
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

My js code:

// npm install node-rsa 
// https://www.npmjs.com/package/node-rsa

var testFunct = function test(message) { 
    const NodeRSA = require('node-rsa');
    const key = new NodeRSA(); 
    var k = key.generateKeyPair(); 
    var p = k.exportKey('pkcs1'); 
    return p};
console.log(testFunct())

enter image description here

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda