Blockchain Programming in iOS

Mohammad Azam
3 min readDec 22, 2017
https://www.crypto-economy.net/wp-content/uploads/2017/12/Blockchain-1.jpg

UPDATE: I am proud to announce that my new course “Blockchain Programming in iOS Using Swift” is now available. Enroll now for only $10.99.

Blockchain is the technology behind popular cryptocurrencies including Bitcoin. The main concept behind Blockchain is to provide a distributed ledger which is not controlled by one central party. In this post I will show you how you can use iOS/MacOS to create a very basic Blockchain in Swift language.

NOTE: This post does not cover nodes/peers, validation, rewards etc.

The quickest and the easiest way to to get started is by using Swift Playgrounds. For this particular demo, I used macOS Playgrounds since it has some useful functions for generate SHA hash.

Implementing the Block Class

The first step is to implement the Block class which will represent a single block in a .. blockchain. The implementation is shown below:

The details of the Block class is shown below:

  • index — The position of block in the blockchain. Index of 0 means that the block is the first block in the blockchain. Index of 1 means it is the second block in the blockchain.. you get the idea right!
  • dateCreated — The date when the block was created
  • previousHash — The hash value of the previous block
  • hash — The current hash of the block
  • nonce — Auto incremented number which plays an important role for mining the hash
  • data — Any valuable asset. This can be money, medical information, real estate information etc
  • key — This is a computed property which is passed to the hashed function

Implementing the Blockchain Class

The Blockchain class requires an instance of a block to initialize itself. This block is also known as genesis block, since it is the first block in the block chain. The implementation of Blockchain class…

Mohammad Azam

iOS Developer, speaker and educator. Top Udemy and LinkedIn instructor. Lead instructor at DigitalCrafts. https://www.udemy.com/user/mohammad-azam-2/