API Calls: iPhone Development

iOS Tutorials: APICalling Results

API Callings

What is an API?

API stands for “Application program interface”, well that doesn’t explain much; so, an API is a set of protocols, procedures, tools that helps in building of software applications. This is actually a simple URL, which is provided to the developer so that data can be migrated from client to server & server to client.

If we talk about types of API then there is REST, SOAP, and RPC etc.

API follows a protocol that is the HTTP protocol.

The 2 most widely used methods by developers are:

  • Get method.
    • This method will request data from a specified source.
  • Post method.
    • It submits data to be processed to a source.

If we simply say, that as the name suggests get method is something which will help us in getting some data from server, when it comes to post; then we will try and push some data into the server, and some processing is to be done on that basis.

So, how will the mobile application call this API’s and get data, which is on the server. In this section we will try and call API’s and get/post data as desired.

We will be trying to get data from the following API:

https://api.myjson.com/bins/1f9nyj

API Project Setup

We have created a simple swift file, which has a singleton class that will help in the parsing of the API’s (Get Method Api’s).

You can download the required files from the link here.

So now we will create a simple application or you can download our starter Application from the link.

If you run the Sample Project downloaded from the above link, it will look something like this.

iOS Tutorials: Hello World.
iOS Tutorials: Hello World.
  • First of all delete the “Hello World” label

AS, you have downloaded the zip file; extract the zip file and there you can see 3 files.

iOS Tutorials: API Calling Downloaded files
APICalling Downloaded files
  • Add all this files, into the project.

 

iOS Tutorials: API Calling Add New files to Application
APICalling Add New files to Application

 

iOS Tutorials: API Calling Add downloaded files
APICalling Add downloaded files
  • Add a method/function on your ViewController.swift file that will have the code for make a Web service request.
    • Add the below code in the ViewController.swift file.

// MARK: – API Call Methods

    func callGetApi() {

        APIHelper.sharedInstance.callGetApiWithMethod(withMethod: “”, successHandler: { (dictData) in

            print(dictData)

        }) { (strMessage) in

            print(strMessage)       

        }

    }

Note: Add this method in the viewDidLoad method

        callGetApi()

iOS Tutorials: APICalling Add and Call Method
APICalling Add and Call Method

Note: We are done with the coding part, but still there are some configurations to be done.

After the release of iOS-9 Apple has made mandatory that all the API’s, which are being used, should follow the HTTPS protocol and there are some keys which needed to be added in the info.plist file, other wise the API calling cannot be possible.

  • Navigate to info.plist –> Click on the + icon as mentioned in the image.

 

iOS Tutorials: APICalling App Configuration
APICalling App Configuration
  • Once a new row is added, enter the key as “App Transport Security Settings”.
    • It automatically takes the type as “Dictionary”.
  • Now we will add the values into the Dictionary.
iOS Tutorials: APICalling App Configurations
APICalling App Configurations

 

iOS Tutorials: APICalling App Transport Security Settings
APICalling App Transport Security Settings
  • Add the value as “Allow Arbitrary Loads
    • This is a Boolean value, which needs to be as “yes”.
iOS Tutorials: APICalling Allow Arbitrary Loads
APICalling Allow Arbitrary Loads

 

iOS Tutorials: APICalling iOS Tutorials: APICalling Allow Arbitrary Loads - YES
APICalling iOS Tutorials: APICalling Allow Arbitrary Loads – YES

Once this is done, try and run the project. (Command + R), and look at the console for results.

iOS Tutorials: APICalling Results
APICalling Results

You can download the whole source code from the link here.

Wanna see how POST method api’s work, please browse our next article.

Author: Chandra.rao

iOS Developer. #Life of an developer.... Learn New technology, Code, Learn New Technology, Code, Death.