Swift PHP MySQL Tutorial – Connecting iOS App to MySQL Database

Today we will learn Swift PHP MySQL. As while developing applications for mobile we need a server for our database. And for this PHP and MySQL is the best option available as it is easy to use and get. So in this Swift PHP MySQL Tutorial we will learn how we can connect our Xcode Project to an external MySQL Database. If you don’t want to do additional server side scripting for your application’s backend you can go through the Firebase iOS Tutorial, where instead of a server you can use Firebase as you appliation’s backend.
If you are looking for a User Registration using PHP and MySQL Tutorial you can visit it from below link.
In this post we will create a simple Single View App to store some data in our MySQL Database. I will be using the following tools.
  • Xcode 7
  • PHP Storm
  • Xampp (for PHP and MySQL)
The first thing we need is our database so lets create the database.

Swift PHP MySQL Tutorial – Creating Web Service

We need a web service often called REST API, as a medium of communication between our webserver and iPhone application. So first we will create our Web Service that will handle the database insertion. As the below demonstrated method is not recommended, you should check PHP REST API Tutorial instead. But I wanted to make this tutorial as simple as possible so we are creating the web service in simplest way.

Creating MySQL Database

First we will create the MySQL Database. I have created my database model. Below you can see my database model.
database model
This is our database. Its very small and simple as I wanna make this tutorial as simple as possible. To create the above database follow these steps.
  • Go to phpmyadmin (localhost/phpmyadmin) and create a new database.
creating database
  • Now select the database and execute the following SQL statement.
  • And then you will have a database table like the following.
database
  • Now we have our database. We will store the values in this table.
But the values to be stored here will be sent from our iPhone App.  And hence we need a communication medium between this database and our iPhone Application. PHP will do this thing for us. So now we will create our web service using PHP.

Creating PHP Scripts

I am going to use PHP Storm IDE, as I love this IDE.  So open PHP Storm (You can also use notepad++ or any other text editor).
  • Create a new project in the xampp’s htdocs folder. I have created MyWebService.
swift php tutorial
  • Create two more directories inside your project, as shown in the below image.
swift php
  • Inside directory includes create a file named Config.php and write the following code. The below code contains important constant that will be used to connect to our database.
  • Now in the same directory (includes) again create a new file named DbConnect.php and write the following code. This file will create the database connection.
  • We need one more file here for the database operation, create a file DbOperation.php and write the following code.
  • Now inside api folder create a file named createteam.php and write the following code. This file will actually insert the values to database.
  • Yeah our web service is ready.
  • Now we need to know the IP of your xampp server you can easily get it by using ifconfig command. Open terminal and write ifconfig and hit enter.
swift php mysql
  • So we have the IP, now we can write the URL of the webservice. The IP we got points to the htdocs folder  so my Web Service URL to create a new team is.

Testing our Web Service

Before moving to iOS Application Development we should first test the web service we created. For testing we can use any REST Client. I am using POSTMAN for chrome.
  • Open POSTMAN and send a POST request with the required parameters to your service URL. You can see the below image for help.
xcode php mysql
  • As you can see we got the success message. Now you can check you database table.
swift php mysql tutorial
  • Yes our URL is working fine. Now we will send a post request to the URL from our iPhone App to insert values. So lets move ahead to xcode side part.

Swift PHP MySQL Tutorial – Xcode Project

Creating a New Project

We have our web service now, lets start creating our iPhone App project in Xcode.
  • Create a Single View App for iPhone in Xcode.
swift php mysql

Adding Views

  • Now go to your Main.storyboard and drag two Text Fields for name and member count values. You also need to add a Button where we will click to save the added values.
Swift PHP MySQL

Connecting Views to ViewController

  • Now connect the Views to your ViewController.swift file. So the code for your ViewController.swift will be.
  • Now we will make a post request to your web service using NSMutableURLRequest.

Making POST Request to our Web Service

  • Now inside the Button action function we will send a POST Request to the URL. For this we will use NSMutableURLRequest.
  • So here is the code, I have written the comments to explain the code.

Making changes in Info.plist file

  • Now the last important thing is you need to do some changes in your Info.plist file, because we are using Xampp and it is not a secure connection. And by default iPhone App will not allow you to make request on http URLs. So to make it work on http open Info.plist file and add the following at the end. Just before the </dict> tag.
  • Now try running your application.

Testing Your Application

  • Now the coding part is done. Just run your application in Simulator.
swift php mysql
Swift PHP MySQL Tutorial
  • Fill the values and click on Save Team. And then check Xcode.
swift php mysql tutorial
Swift PHP MySQL Tutorial
  • Yeah we got the success message. Now lets check the database.
Swift PHP MySQL Tutorial
Swift PHP MySQL Tutorial
Bingo! Its working absolutely fine. If you are having troubles you can get my source code from the link given below.

Comments

Post a Comment

Popular posts from this blog

How to Install Mac OS X El Capitan on VMware on PC

iOS Registration Form Example using PHP and MySQL