Javascript Ojbc调用js错误:Can';找不到需要的变量
我对JS完全陌生。我在ios上开发,我们需要在我们的程序中使用Web3js。可以在obj-c中调用js函数。但是,我使用'require'导入Web3js模块,它抛出'ReferenceError:找不到变量:require'。会发生什么?我错过什么了吗? 有人能帮忙吗??非常感谢 更新: 如果“require”不可用,我如何使用obj-c调用的js中的其他模块 这是我的密码 obj-c代码:Javascript Ojbc调用js错误:Can';找不到需要的变量,javascript,objective-c,web3js,Javascript,Objective C,Web3js,我对JS完全陌生。我在ios上开发,我们需要在我们的程序中使用Web3js。可以在obj-c中调用js函数。但是,我使用'require'导入Web3js模块,它抛出'ReferenceError:找不到变量:require'。会发生什么?我错过什么了吗? 有人能帮忙吗??非常感谢 更新: 如果“require”不可用,我如何使用obj-c调用的js中的其他模块 这是我的密码 obj-c代码: NSString* path = [[NSBundle mainBundle] pathForReso
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:
我使用Browserify打包所有模块,然后使用webView调用js函数 这是我的js文件index.js
"use strict"
let Web3 = require('web3.js');
window.Web3 = Web3;
然后用Broswerify打包
browserify index.js -o web3.js
我的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>
函数getAddress(prvkey)
{
尝试
{
var web3=新的web3();
var account=web3.eth.accounts.privateKeyToAccount(prvkey);
返回帐户地址;
}
捕获(e)
{
返回e;
}
}
你能发布你的代码吗?我猜你正在objective-c中使用一个直接的javascript桥,但是
require
只是Node.js运行时中的一个标准函数。您可能需要以其他方式导入Web3js模块。但我肯定需要看看你的代码,并让你解释你的设置,以了解你想做什么。我已经更新了我的代码。我正试图为我们的私人连锁店开发一个以太坊图书馆。它包括创建原始事务、编码ABI和从私钥获取地址。
<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>