ICON Swift iOS Tutorial: Part 1

This tutorial series covers how to integrate ICON blockchain functionality into an iOS App using the ICON Swift SDK. We begin in Part 1 with the setup and integration of the ICON Swift SDK into Xcode. This portion of the tutorial covers setting up the baseline ICON functionality and shows examples such as how to connect to the ICON network, create a wallet, and perform a simple ICON query, such as checking the balance of an ICON wallet. Part 2 expands development into a custom, secure, and useful DApp to quickly and conveniently load images with immutable timestamps onto the ICON blockchain.


Software Requirements:

This tutorial uses Xcode 11.0, Swift 5, and ICONKit 0.4.1

The ICON Swift SDK documentation can be found at https://www.icondev.io/docs/swift-sdk


In Part 1, we will:

  1. Create a single view iOS app
  2. Integrate the ICON SDK
  3. Create a wallet (or load a wallet if one is already created)
  4. Check the balance of the wallet
  5. Show the wallet address and balance on the screen

First, go ahead and open XCode and create a new single view app. Run the app, you will see the common “Hello World” as shown in Fig. 1.

Figure 1. Hello World

We will use CocoaPods, a dependency manager for Swift, to install the ICON Swift SDK (labeled ICONKit) and dependencies. If you do not have CocoaPods installed, go ahead and install (instructions can be found at: https://cocoapods.org). Then create a podfile by running ‘pod init’ in the main project directory (or alternatively, you can just create a file called ‘Podfile.’ We now populate this file to include the latest version of ICONKit (version 0.4.1) and then run the command ‘pod install’ to install and integrate ICONKit and its dependencies into our project. Your Podfile should look similar to the below (our project name is ‘ICON_Swift_Tutorial’):

target 'ICON_Swift_Tutorial' do
use_frameworks!
pod 'ICONKit', '~> 0.4.1'
end

After running ‘pod install,’ you’ll see ICONKit and dependencies installed. Open the ‘app.xcworkspace’ file to load the project with associated software dependencies in XCode. Notice the file structure shows your project, as well as the Pods section. It should look similar to what is shown in Fig. 2.

Figure 2. XCode Project File Structure (With Pods)

We will first focus on the AppDelegate, which is the root of the project. It sets shared behaviors and initial actions upon loading. We will add ‘import ICONKit’ to utilize ICONKit. Next, we will add functionality to the application function (we use the argument with the ‘didFinishLaunchingWithOptions’).

The relevant code is below. In this code, we determine if there is a keystore file, named “iconkeystore.” If this exists, then we create a shared object called IconServices.shared (which I’ll describe later). If not, then we create a keystore with the password “iconpassword.” You’ll notice print lines for each of the flow outcomes. Since this tutorial is focused on setup of the environment, we provided these in the code to ensure you can see the execution and learn which path is being taken. In addition, clearly the filename and password should be customized for real-world use.

Now, let us take a look at the IconServices class. This is contained in a separate Swift file, appropriately named IconServices.swift. This class connects to the wallet (via the keystore / password combination), checks the balance of the wallet, and then checks the address. We demonstrate connectivity to the ICON blockchain, through a wallet, with this basic functionality. We will then use these values to show the user the status of their wallet. Notice that you can connect to either the TestNet or the MainNet, based on the value provided to the ICONService object. The code is shown below.


Now that we have the basic functionality to connect to the ICON network, we will show visualization of the wallet address and balance. We setup a simple UIViewController to show this information. We will not go through this setup, as it is common to almost all Swift applications and there are countless tutorials on different setups. Once we have our chosen GUI setup, we can easily access the address and balance for the wallet through our IconServices class via:

IconServices.shared.myAddress
IconServices.shared.myBalance

We design a very simple interface, showing these details.

Figure 3. ICON wallet address and balance

Now, let us ensure we have properly connected to the ICON network, and can dynamically update our information, based on changes. We send 1 ICX to this wallet address. The new balance of 1 ICX is shown in the tracker.

Figure 4. ICON wallet with 1 ICX

Now we reload our Swift App and see that the balance had updated. We have made the GUI a bit more aesthetically pleasing, however, the purpose of this Part 1 is just to setup the integration of the ICON SDK and baseline functionality into a Swift App. In Part 2, we will focus on a real-world application.

Figure 3. ICON wallet address and balance update

Please stay tuned for Part 2 of this tutorial, where we continue development into a custom, secure, and useful DApp to quickly and conveniently load images with immutable timestamps onto the ICON blockchain.

Russell (thelionshire) — Ubik Capital

Written by

Not financial advice. Opinions are my own.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade