Quick Links

Wondering where to get started?

I’ve set up the following tutorials which will take you from writing a simple backend in Java (Spring Boot) where you’ll develop your first REST API, all the way to consuming these API’s in a simple iOS application using Swift 3.

With that being said, if you have no interest in developing a backend and just wanted some code for HTTP Requests written in Swift 3, feel free to skip through and check out those tutorials as they can be easily adapted to consume any API.  

Latest Video

Each video will correspond to a part of one of the following tutorials (for those who prefer written format).

Java/Spring Boot Tutorials

Swift/iOS Tutorials

Part 1: Setting up your MySQL Database Part 1: Setting up our XCode/Swift 3 Project (To Consume our REST API)
Part 2: Setting Up Spring Boot and Hibernate in Eclipse Part 2: GET Request with Swift 3 (wihout Alamofire)
Part 3: Configuring Your Database to Work With Spring Boot Part 3: Display JSON Data Inside of a UITableView
Part 4: Querying a Database From Your Spring Boot Application Part 4: POST Request in Swift 3 (without Alamorfire)
Part 5: Creating Some Service Layer Functionality
Part 6: Creating Your First REST API
Part 7: Adding Additional Functionality to our Spring Boot Application

 

Library of the Month

I’ll be putting up a new library that I think is worth mentioning every month alongside some code examples to get you up and running.  If you have any suggestions, feel free to send those along.

Chameleon

 

Blog Categories

Here are a few blog categories I’ll try to keep consistent.

 

Tips All related coding tips and personal experiences.
Library Of The Month
Past libraries of the month.

 

This has received a lot more interest than I thought which is encouraging!  If you have any additional suggestions or maybe there is a topic you’d like to learn more about or if there was a section in one of the tutorials I skipped over too quickly please leave a comment below – I’d love to help out!

Querying a MySQL Database with Swift 3 (PerfectMySQL)

In the last tutorial we set up our server side Swift project to connect with the MySQL database we set up here.  In this tutorial, we’re going to learn how to run queries against our database using Swift & the Perfect framework.

 

Writing our First Statement

In the last tutorial we created a new Database.swift file that will handle all of the related database functionality of our project.  What we’re going to do is add in another function to this file that will allow us to insert a new row into the game table of our database.

To do so, I’m going to add in a function called insertGame which looks like this…

Calling our Insert Function

Awesome, so now that we have our first function created that will insert a game into our database, let’s call this from our main.swift file.  Here’s what we’re going to do:

  1. Create an instance of our DB class (inside of our Database.swift file) that will allow us to make calls to the public functions we’ve put inside of this class.
  2. Call our insertGame function!

Here’s what my main.swift file looks like now…

Now when we go ahead and run our application, our insertGame function will execute which will insert a new row into the game table of our database.

I’ve gone ahead and performed a quick query of the database just to show the additional row that has been added.

Screen Shot 2017-12-02 at 11.54.34 AM.png

Great, so we’ve written our first SQL statement that will insert game rows into our database using Swift!  As promised, I’ll be keeping these tutorials as short as possible, so I’ll end this one right here.  If you’re interested in learning writing server side Swift, in the next tutorial I’ll be showing you how to write your first API.

If you’re having some trouble getting your project to this stage, leave a comment below and I’ll help you out.  Have any other unrelated questions or suggestions?  I’d like to hear those too! 

 

Setting up our Swift web-server to connect to MySQL

This series is going to show you how to write a basic backend using Swift.  This is for those who feel more comfortable using Swift throughout their application rather than switching between languages (ex: Java/Spring Boot backend which can also be started out here).  If this is something you’re interested in, I’ll be breaking this down into small parts so it doesn’t become too frightening!

Last time we focused on setting up our Swift web-service project which can be found here.  This time we’re going to be doing some configuration within this project to set up communication with MySQL.

Connecting to MySQL

The first thing we’re going to do is configure our project to connect with MySQL.  Before we add in the MySQL Perfect dependency, we have to do a little bit of setting up first on our machine.  Open up Terminal and type the following…

brew update

And once that has finished updating type

brew install mysql

Potential Errors (Follow these steps if you receive the following error)

Screen Shot 2017-11-26 at 10.53.21 AM

To fix this, type

xcode-select --install

And then go ahead and attempt to perform the previous step by typing

brew install mysql

Now we can update our Package.swift file to include the Perfect MySQL dependency.  After doing so, your file will look as follows…

Let’s update our project to include this dependency.  Open up Terminal and type…

swift package update

And once that has finished updating, type in the following command

swift package generate-xcodeproj

Awesome.  So we have the MySQL dependency added, now we can write some code to configure our project to communicate with our MySQL database that we set up in a previous tutorial here (you’ll need to do this first, unless you have a MySQL database already setup and running on your machine).

I’m going to create a new Swift file called Database.swift which is where we’ll put all of our database related functionality.  To do this…

  1. Right click on our project directory, swift-server
  2. Select New File… and in the popup window select Swift File
  3. Give your file a name – I’ve called mine Database and then go ahead and click save.

After creating this new Swift file, my project directory looks as follows…

Screen Shot 2017-12-02 at 9.51.00 AM

Now, there are a few other things we’re going to do before we make the call to connect to our database.  Perform the following…

  1. Right click on your new Database.swift file > Show File Inspector

On the right hand side of Xcode, you’ll notice a panel appear.  Make sure in the Target Membership that you have the following selected…

Screen Shot 2017-12-02 at 10.27.46 AM.png

We’re ready to start coding.  I’m going to create a new function in the Database.swift that will connect to our database.  It will look like this…

Keep in mind that you’ll have to change the values for user, password, and database depending on how you’ve set up MySQL on your computer.


As promised, I will break down these tutorials to keep them short and easy to follow.  I will end this one right here as I know it might take a bit of time to get this setup – please leave me a comment below if you run into any errors while trying to get your project to this point.

If you’re interested in this series, next time I’ll be showing you how to query our MySQL database that we connected in this tutorial from our Swift web-server project!

Creating our Swift Web Server Project

You may have followed the previous Spring Boot tutorials where we developed our first REST API.  We’re going to do something similar here except we’re going to create our web-server using Swift instead.

Once again, I’m going to break these tutorials down into small sections so it isn’t too daunting especially if this is your first time creating a REST service.  With that being said, today we’re just going to be creating our project.

For the web-server, I’m going to be using Perfect which is a toolkit used to build REST services.

Things you’ll need

OS X

  1. Swift 3.0
    • If you have Xcode installed, you should already have this.  But just to confirm, go ahead and open up Terminal and type swift –version if you’re unsure.

That’s it.  Awesome.

Ubuntu Linux (taken from the Perfect documentation)

Perfect runs in Ubuntu Linux 14.04, 15.10 and 16.04 environments. Perfect relies on OpenSSL, libssl-dev, and uuid-dev. To install these, in the terminal, type:

sudo apt-get install openssl libssl-dev uuid-dev

When building on Linux, OpenSSL 1.0.2+ is required for this package. On Ubuntu 14 or some Debian distributions you will need to update your OpenSSL before this package will build.

Building Our Starter Project

Open up Terminal and enter in the following commands…

mkdir swift-server 
cd swift-server

This is the directory that I’ll put my project into.  (I’ve put this on my desktop, but feel free to put yours anywhere).

Now that we’re inside of our new directory, type the following commands

swift package init --type executable
swift package generate-xcodeproj

And with our new Xcode project generated, let’s go ahead and open it.

open ./swift-server.xcodeproj

Maybe you like screenshots, so here is what my Terminal looks like after entering the above commands…

Screen Shot 2017-11-25 at 10.37.20 AM

Alright, let’s make sure that we have this set up.  After opening your project from Terminal, click on the active scheme

Screen Shot 2017-11-25 at 10.51.23 AM.png

It might already look like it has been selected, but make sure to set the active scheme as follows…

Screen Shot 2017-11-25 at 10.55.01 AM.png

Then we’re going to run our application by selecting the run button.

screen-shot-2017-11-25-at-10-51-23-am1.png

After doing so, you should notice the following “Hello World” text in the Xcode console as follows…

Screen Shot 2017-11-25 at 11.00.49 AM.png

If you don’t see this, make sure that you have selected the correct active scheme from the previous step!

Importing Perfect into our Project

Now that our application has run successfully, let’s go ahead and import Perfect.  To do this, open up the Package.swift file that was generated when we created our project from Terminal.

Screen Shot 2017-11-25 at 11.06.20 AM.png

Add in the following dependency to your Package.swift file so that it will now look something like this…

Now that we’ve added in the Perfect dependency, we’re going to open up Terminal again and type in the following command

swift package update

And once that has finished updating, type in the following command

swift package generate-xcodeproj

Now we can switch back into Xcode and note the Perfect directories have been added to our Sources.

Screen Shot 2017-11-25 at 11.19.14 AM.png

To check that this is setup, go ahead and open the main.swift file located in Sources > swift-server > main.swift (shown in the above image).

We’re going to import the PerfectLib, PerfectHTTP, and PerfectHTTPServer.  Your main.swift file will now look as follows…

And once again, we’ll run our application by selecting  Screen Shot 2017-11-25 at 10.51.23 AM and if there are no errors we are good to go!


 

Alright, so we’ve set up our project and have successfully added in the Perfect dependency.  If you’d like to continue learning how to create a Swift REST service, next time we’ll be doing some configuration within our project to communicate with our MySQL database.

Have any questions, comments, suggestions?  Having issues getting your project to this stage?  Leave a comment and I’ll be happy to get back to you. 

 

Library of the Month: Chameleon

Having a hard time finding the right colour scheme for your iOS app?  Check out Chameleon – a powerful colour framework that removes the hassle and minimizes the time spent on finding the perfect colour combination.  Chameleon is lightweight and offers many useful functionalities such as global app theming, text and background contrast, and generation of gradient colours… just to name a few.

Installation

Swift

use_frameworks!

pod 'ChameleonFramework/Swift'

Objective-C

use_frameworks!

pod 'ChameleonFramework'

Storyboard Add-On (optional)

If you like, you can also download and install the Chameleon Palette.  After restarting Xcode you’ll find all of the colours from Chameleon in the Palette Colour Picker for those of you that prefer using Storyboard.

Screen Shot 2017-11-19 at 10.27.13 AM.png

Examples

Text and Background Contrast

I suppose it’s important that users are able to read the text in your application. With Chameleon you can do that with one line of code which takes away the impossible decision of picking a contrasting text colour (this is also extremely helpful when your background colour changes dynamically).  Check it out…

Screen Shot 2017-11-19 at 10.44.08 AM.png

Gradient Colours

Screen Shot 2017-11-19 at 10.55.40 AM.png

Colours From Images

Chameleon supports extraction of colour from images which allows for easy generation of flat or non-flat colour schemes.

Screen Shot 2017-11-19 at 11.18.23 AM.png

This just provides a few examples of the things you can do with the Chameleon framework, if you’re looking for some more tricks check out the documentation here.  I hope some of you might find this useful as it’s definitely something I’ll continue to use in my future projects.

If there are any other frameworks that you think are worth mentioning, leave me a comment below and I’d love to check them out. 

Using JDBC in a Spring Boot Application

Sometimes you might want to execute some more complicated SQL statements to run against your database from your Spring Boot application.  In this section, I’ll show you how to setup a JDBC Template to get started.

I’m going to be using a simple, existing project that you would have been following along with if you had done any of the previous Spring Boot tutorials.  The source code for this can be found right here.

I’m also going to be running this in Eclipse, but feel free to use any other IDE of choice.  If you’re having some trouble following along, don’t worry, I’ll add in the completed source code at the end of this post as well.

Creating our BaseJDBCRepository

There will be many times where you’ll want to create a new JDBCRepository class per each entity in your application.  With that in mind, to keep things tidy I’m going to create a parent class that each of these JDBCRepositories will extend just to reuse some existing code.  So, I’m going to create a new class called BaseJDBCRepository inside of the repository package and add in the following code…

Creating the GameJDBCRepository class

Remember before how I mentioned that we’re going to want to create a new JDBCRepository class per each entity in our application?  Now we’re going to create a GameJDBCRepository.java file inside of the repository package which will handle all functionality related to our Game.java object.  This new file will extend the BaseJDBCRepository class we created in the previous step so that we can access the jdbcTemplate.  This will allow us to perform our queries.  A basic skeleton of this class will look as follows…

Now, for fun let’s add in a query that will find all games that have a particular word that we are looking for in their description.  Maybe we want to find all games that have the word “platform” in their description as we really like those kind of games.

There are a couple of things that we will need to set this up such as:

  1. Our SQL statement that we wish to execute.
  2. A custom RowMapper that will map the results returned from our query into a list of Game.java objects.

With that in mind, I will add these into our GameJDBCRepository class and it should now look as follows…

Great, so now that we have our RowMapper and our SQL statement, let’s create our searchForGamesByDescription method.  It will look something like this…

Awesome, and that is our first implementation using JDBC.

If you had some trouble following along, or if you wanted to just check out the source code I’ve uploaded it to github which can be found here.

Have any questions, comments, suggestions?  Want more JDBC examples?  Leave a comment and I’ll be happy to get back to you. 

 

 

POST Request in Swift 3 (without Alamofire)

Howdy folks!  In the last tutorial we spent some time going through how we can take the JSON data we retrieved from our GET request in Swift and display it inside of a UITableView.  We also learned how we can map the JSON representation of our Game object (from the database) into a Swift object so that we could easily access particular fields.

If you’re new to these tutorials, the API’s we will be calling to test our HTTP requests were developed in previous lessons using Spring Boot.  If you’re interested in Java development and wanted to create your own backend application, the beginning of these tutorials can be found here.  However, if you have no interest in learning how to create your own API’s, you can still follow along with this tutorial.

In this tutorial we’re going to code our POST request so that a user will be able to create comments on a Game.  We’ll add this code to our Request.swift file which is responsible for all of our HTTP functionalities.

Creating our Comment.swift Object

Because we’re going to be using a JSON content type when posting to our API, that means we need to pass through a JSON representation of a Comment object.  With that being said, the first thing we’re going to do is create a new Comment.swift file that mocks the fields of our comment table in the database.  It will look something like this…

Creating our POST request in Swift 3

Great, so we have our Comment.swift object created, now we’re ready to start coding our POST request.  If you’ve been following along, we’re going to put this function in our Request.swift file which is responsible for all of our HTTP functionality.  In order to make a POST request, we need to keep in mind that we’re going to be posting JSON content type to the server, which is where we can make use of our mapToDictionary function created in our Comment.swift file.

With that being said, our POST request will look as follows…

We’ll take in a POST url which contains the API that we will POST to.  We’ll also take in a body: [String: AnyObject] parameter which represents a JSON representation of, in this case, our Comment.swift object.

Testing our POST request

Sweet, so we’ve created our Comment.swift class and now we have our POST request set up using Swift 3.  Now, let’s see how we can actually use this function.

First, let’s set up a new constant in our Constants.swift file to store our create game comment API.  It should now look something like…

For now, I’m going to create a new view controller in which I’ll name CommentViewController.swift which will be responsible for showing all of the comments of a particular game.

Now, my project directory will look something like this…

Screen Shot 2017-10-27 at 6.41.47 PM.png

For testing purposes, I’m just going to create a dummy call with this function so that we can see how to call it and that it is working as we expect it to.  With that being said, in our viewDidLoad method of our new CommentViewController.swift file, I’m going to add in the following code…

  1. We’ll create a random Comment.swift object.
  2. Set up our POST url to create a comment on game with id = 1
  3. We’ll call our Request.post method.

For now, I’ll set this CommentViewController.swift class as my initial view controller, just for testing purposes.  When I run my application,  I print out the data to the console which looks as follows…

Screen Shot 2017-10-27 at 7.11.11 PM.png

Awesome, this tells us that our POST request is working as expected and that comments are being created on the game corresponding to the game.id we pass through in our API call.

So, we’ve created our Comment.swift object, wrote our POST request in Swift, and finally we have done a quick test to see that it is working as expected.

This tutorial was primarily created to develop our actual POST request, however, in the next tutorial we’ll set up a basic UI that will allow a user to comment on a particular game, and from this input we will POST it to the server.

Have any questions, comments, suggestions?  Having issues getting your project to this stage?  Leave a comment and I’ll be happy to get back to you.