目前针对Web3j 只有开源的web3jswfit 库:https://github.com/BANKEX/web3swift
想要进行OC的钱包开发, 开源进行OC和swift 混编, https://www.jianshu.com/p/37f720df43ac
新建OC项目,swfit版本 改为4.2.
podfile 文件中 导入web3swift库
use_frameworks!
pod 'web3swift.pod'
post_installdo|installer|
installer.pods_project.targets.eachdo|target|
target.build_configurations.eachdo|config|
config.build_settings['ENABLE_BITCODE'] ='NO'
config.build_settings['SWIFT_VERSION'] ='4.0'
end
end
end
导入后 在oc代码中 就能应用web3swift库了
调用智能合约
NSDictionary *abi = [self readLocalFileWithName:kGoodCoinAbiKey]; // 加载本地的ABIjson文件
NSString*abistr = [selfdictionaryToJson:abi]; // 转化为字符串
// W3NetworkId * chainId =[W3NetworkId ropsten] ; /.测试环境
W3Web3 * infura =[[W3Web3alloc]initWithUrl:[NSURLURLWithString:@"http://172.18.0.166:7545"]]; // 本地测试地址
if(!infura) {
return nil;
}
//增加一个地址,通过私钥导入
[infura addAccountWithPrivateKey:[SecureData hexStringToData:[privekey hasPrefix:@"0x"]?privekey:[@"0x" stringByAppendingString:privekey]] password:user.passWord error:nil ];
//设置
W3Options*option = [[W3Optionsalloc]init];
option.from =[[W3Address alloc] initWithString:_user.address];;
option.to =[[W3Address alloc] initWithString:@"0xe8031716077B2c42365AD559830CEF1d977Dd536"];
// option.value = [[W3UInt alloc] initWithValue:@(0.1)];
// option.gasLimit = [[W3UInt alloc] initWithValue:@(60000)];
// option.gasPrice = [infura.eth getGasPriceAndReturnError:nil];
//初始化一个合约
W3Contract *contract = [[W3Contract alloc] init:abistr at:[[W3Address alloc] initWithString:@"0xe8031716077B2c42365AD559830CEF1d977Dd536"] error:nil];
//合约里的参数 必须和ABi里面对应, 否则会闪退
W3EthereumTransaction*contracttrans = [contractmethod:@"send" parameters:@[[[W3Address alloc] initWithString:@"0xe1f33a19E2E8b4d2f80A245d0E059f4B6352a547"],[[W3UInt alloc] initWithValue:@(100)], [dhexData]] extraData:nil options:option error:nil];
W3TransactionIntermediate *td = [[W3TransactionIntermediate alloc] initWithTransaction:contracttrans web3:infura contract:contract method:@"send" options:option];
//开始发送send方法 调用智能合约
[td sendAsyncWithPassword:_user.passWord options:option onBlock:@"latest" completion:^(W3TransactionSendingResult * _Nullable res, NSError * _Nullable err) {
}];
// 成功会返回hax,
开发过程中可能会有很大坑, 欢迎交流。 WX kabosly
网友评论