javascript - Ojbc invoke js error: Can't find variable require -


i'm totally new js. i'm developing on ios , need use web3js in our program. it's ok call js function in obj-c. however, use 'require' import web3js modules, throws 'referenceerror: can't find variable: require'. happens? did miss anything? can help?? many thanks.

update:

if 'require' not usable, how use other modules js invoked obj-c?

here code.

obj-c code:

nsstring* path = [[nsbundle mainbundle] pathforresource:@"bridge/src/index" oftype:@"js"]; jsfunc = [nsstring stringwithcontentsoffile:path encoding:nsutf8stringencoding error:nil];  jscontext* jscontext = [[jscontext alloc] init]; [jscontext evaluatescript:jsfunc]; jsvalue* func = jscontext[@"getaddress"]; jsvalue* = [func2 callwitharguments:@[@"0x8730f6ad1c0d387644590966bdbfedba44fe250118df0f97f11190ad5727b312"]];     

js:

function getaddress(prvkey) {   try{       var web3 = require('../../3rd_party/web3.js-1.0.0');            var web3 = new web3();       return web3.eth.accounts.privatekeytoaccount(prvkey);   }   catch (e)   {       return e;   } } 

i used browserify package modules , used webview call js functions.

here js file, index.js

"use strict" let web3 = require('web3.js'); window.web3 = web3; 

and use broswerify pack

browserify index.js -o web3.js 

my html,

<html> <head>     <script type="text/javascript" src="web3.js"></script>    </head> <body>     <script type="text/javascript">         function getaddress(prvkey)         {             try             {                 var web3 = new web3();                 var account  = web3.eth.accounts.privatekeytoaccount(prvkey);                 return account.address;             }             catch (e)             {                 return e;             }         }     </script> </body> 


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -