-
Notifications
You must be signed in to change notification settings - Fork 6
Get started with WavesSDK for iOS
It's easy to start using Waves iOS SDK. This page contains a QuickStart tutorial that will help you to build your first app with the SDK integrated.
The source code of the application that's used as an example is available in the examples directory. You can run it via XCode after installing the WavesSDK using pods: run pod install
in Terminal.
We use cocoapods for the dependency linking. To use Waves SDK in your Xcode workspace please add the "WavesSDK" pod into your Podfile:
platform :ios, '10.0'
use_frameworks!(true)
target '<ProjectName>' do
pod 'WavesSDK'
end
Check this page to learn more about cocoapods.
Please use this instruction to start using Podfile.
Add library initialization to application() method in your AppDelegate class by editing 'AppDelegate.swift' file.
After that you will be able to use Waves SDK features in the app.
import UIKit
import WavesSDK
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
WavesSDK.initialization(servicesPlugins: .init(data: [],
node: [],
matcher: []),
enviroment: .init(server: .testNet, timestampServerDiff: 0))
return true
}
}
Now the application is ready to work with Waves Platform. Let's test it by implementing WavesCrypto functionality. For example, we could try creating a new seed-phrase and getting the blockchain address for it.
// importing WavesCrypto part of SDK
import WavesSDKCrypto
// creating a WavesCrypto object
let crypto = WavesCrypto()
// generating the seed-phrase
let newSeed: Seed = crypto.randomSeed()
// getting address of this seed-phrase
// provide chosen network byte in the chainId field ("T" for the Testent, "W" for the Mainnet)
let address: String = crypto.address(seed: newSeed, chainId: "T")!
You can check the full list of WavesCrypto methods on this page.
Now let's see how iOS SDK can be associated with the blockchain.
A real case solution is accessible in the example directory and here are the important code parts of it:
// getting current account balances using its address
WavesSDK.shared.services
.nodeServices
.assetsNodeService
.assetsBalances(address: "SomeAddressInBase58")
.observeOn(MainScheduler.asyncInstance)
.subscribe(onNext: { [weak self] (balances) in
self?.balances = balances
self?.tableView.reloadData()
})
.disposed(by: disposeBag)
}
}
// signing and sending a transaction using account's seed
func sendTransaction() {
let seed = "My seed"
guard let chainId = WavesSDK.shared.enviroment.scheme else { return }
guard let address = WavesCrypto.shared.address(seed: seed, chainId: chainId) else { return }
guard let senderPublicKey = WavesCrypto.shared.publicKey(seed: seed) else { return }
let recipient = "someAddressOrAlias"
let fee: Int64 = 100000
let amount: Int64 = 100000
let feeAssetId = ""
let assetId = ""
let attachment = "Some comment to transaction"
let timestamp = Int64(Date().timeIntervalSince1970) * 1000
var queryModel = NodeService.Query.Broadcast.Transfer(recipient: recipient,
assetId: assetId,
amount: amount,
fee: fee,
attachment: attachment,
feeAssetId: feeAssetId,
timestamp: timestamp,
senderPublicKey: senderPublicKey)
// sign transfer transaction using seed
queryModel.signing(seed: seed, chainId: chainId)
let send = NodeService.Query.Broadcast.transfer(queryModel)
WavesSDK.shared.services
.nodeServices // You can choose different Waves services: node, matcher and data service
.transactionNodeService // Here methods of service
.broadcast(query: send)
.observeOn(MainScheduler.asyncInstance)
.subscribe(onNext: { [weak self] (tx) in
print(tx) // Do something on success, now we have wavesBalance.balance in satoshi in Long
})
.disposed(by: disposeBag)
}
Other transactions examples are available in tests.
In this tutorial there is just a simple demonstration of Node Service features. In your cases you can also use Data, Matcher and Node services.
To run your application you will need a Waves Testnet account. Please create it in a Waves Keeper. After creating you have to add tokens to your Testnet account's balance using Faucet: just insert account's address and get your 10 Waves 💰
After that you can insert the account data to the code everywhere it's needed and run the app.
Each transaction has an ID ("txId" property). So you can check if your transaction was successfully sent in the Explorer by inserting txId value into the search field on the top of the screen.
Now you have your first iOS application associated with Waves platform 😎
To make it more powerful learn more about transactions, Node-Service, Matcher-Service and Data-Service methods!
Have fun developing applications with Waves features!