BitcoinSwift 是一个用于与 BTC 网络交互的 iOS 工具包。
有关更多具体信息,请参阅 示例
将此内容添加到您的 Podfile 并运行 pod install
以进行安装
pod 'BitcoinSwift', '~> 1.0.0'
Swift 包管理器是一个用于自动分发 Swift 代码的工具,它集成到 Swift 编译器中。
一旦您设置了 Swift 包,添加 TronWeb 作为依赖项就如同将它添加到 Package.swift 的依赖项值一样简单。
dependencies: [
.package(url: "https://github.com/Marcos-cmyk/Bitcoin.git", .upToNextMajor(from: "1.0.0"))
]
import BitcoinSwift
import BitcoinSwift
let bitcoin = Bitcoin_V1()
if bitcoin.isSuccess {
//To do...
} else {
bitcoin.setup(showLog: true) { [weak self] _ in
guard let self = self else { return }
//To do...
}
}
bitcoin.createAccount() { [weak self] state, address, privateKey, mnemonic,error in
guard let self = self else { return }
self.createAccountBtn.isEnabled = true
tipLabel.text = "create finished."
if state {
let text =
"address: " + address + "\n\n" +
"mnemonic: " + mnemonic + "\n\n" +
"privateKey: " + privateKey
accountDetailTextView.text = text
} else {
accountDetailTextView.text = error
}
}
guard let mnemonic = mnemonicTextView.text else{return}
bitcoin.importAccountFromMnemonic (mnemonic: mnemonic){ [weak self] state, address, privateKey, mnemonic, error in
guard let self = self else { return }
self.importAccountFromMnemonicBtn.isEnabled = true
tipLabel.text = "import finished."
if state {
let text =
"address: " + address + "\n\n" +
"privateKey: " + privateKey + "\n\n" +
"mnemonic: " + mnemonic
walletDetailTextView.text = text
} else {
walletDetailTextView.text = error
}
}
guard let privateKey = privateKeyTextView.text else{return}
bitcoin.importAccountFromPrivateKey(privateKey: privateKey){ [weak self] state, address, privateKey,error in
guard let self = self else { return }
self.importAccountFromPrivateKeyBtn.isEnabled = true
tipLabel.text = "import finished."
if state {
let text =
"address: " + address + "\n\n" +
"privateKey: " + privateKey
walletDetailTextView.text = text
} else {
walletDetailTextView.text = error
}
}
bitcoin.getBTCBalance(address: address) { [weak self] state, balance,error in
guard let self = self else { return }
self.getBalanceBtn.isEnabled = true
if state {
let title = "BTC Balance: "
self.balanceLabel.text = title + balance
} else {
self.balanceLabel.text = error
}
}
// If you have n input addresses and m output addresses, then your inputCount is n, and your outputCount is m.
let inputCount = 1
let outputCount = 2
bitcoin.estimateBtcTransferFee(inputsCount: inputCount,outputsCount: outputCount){ [weak self] state, high,medium,low,error in
guard let self = self else { return }
print("Estimate fee finised.")
if (state) {
let highFormatted = String(format: "%.2f", high)
let mediumFormatted = String(format: "%.2f", medium)
let lowFormatted = String(format: "%.2f", low)
self.estimatedFeetLabel.text = "Send BTC hava three estimated fee. \n high:\(highFormatted) Satoshis. \n medium:\(mediumFormatted) Satoshis. \n low:\(lowFormatted) Satoshis"
} else {
self.estimatedFeetLabel.text = error
}
}
// Support a Bitcoin address sending to multiple Bitcoin addresses simultaneously.
guard let privateKey = privateKeyTextView.text,
let reviceAddress1 = reviceAddressField.text,
let amount1 = amountTextField.text else { return }
var outputs = [[String: String]]()
let output1: [String: String] = ["address":reviceAddress1,"amount":amount1]
let output2: [String: String] = ["address":"address2","amount":"0.00001"]
outputs.append(output1)
outputs.append(output2)
let fee = 3000.0
bitcoin.transfer(privateKey: privateKey, outputs: outputs, fee: fee){ [weak self] state, hash,error in
guard let self = self else { return }
print("state = \(state)")
print("hash = \(hash)")
if (state) {
self.hashLabel.text = hash
} else {
self.hashLabel.text = error
}
}
有关更多具体信息,请参阅 示例
TronWeb 采用 MIT 许可发布。有关详细信息,请参阅 LICENSE