私はJSがまったく初めてです。私はiosで開発しており、プログラムでWeb3jsを使用する必要があります。obj-c で js 関数を呼び出しても問題ありません。ただし、「require」を使用して Web3js モジュールをインポートすると、「ReferenceError: Can't find variable: require」がスローされます。何が起こるのですか?何か見逃しましたか?誰でも助けてくれますか?? どうもありがとう。
アップデート:
「require」が使用できない場合、obj-c によって呼び出された js から他のモジュールを使用するにはどうすればよいですか?
これが私のコードです。
obj-c コード:
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;
}
}