Connect your apps to iTunes Search API

In this tutorial, you will know how to connect to the iTunes search API using Swift, download the JSON results, and parse them to the dictionaries. This information will then be used to populate the table views. A click event will then be added to the table view for the sake of user interaction. On clicking this item, the iTunes store will be opened.

How to Connect the User Interface

We should begin by getting a reference to the tableview. In the ViewController.swift just outside other functions and after class definition, add the following code:

Swift

1

@IBOutlet varappTV:UITableView

The variable appTV will make it possible to connect to the Table View in the storyboard. Save the file above.

Open the storyboard. Drag from the Table View to the View controller object. This will link these objects.

How to Make the API Request

Since we have connected the user interface, we can make the API call. Create a new function, and give it a name of choice. I have called mine iTunesSearch(searchterm: String). After searching arbitrary terms, this will cause the request to happen. Write the code below:

We have started by fixing the search term that we pass in. The iTunes search APi recommends that terms should be of the form first+ second+ third+ words. This contradicts the form first%20second%20. This is why we have used an NSString method, that is, stringByReplacingOccurencesOfString.

The search term will be modified by replacing all the available spaces with a + symbol. You can also see the escape, which will make it possible to escape the search term if a symbol that cannot fit in the URL is found. We have also defined an NSURL, which can be used in iOS8’s network API as a Request URL. The lines below as used above are very important:

The first line has used the url that we created as the target URL to create an NSURLRequest object. The second line has done the main job by creating a connection which is to be used for sending the request. The purpose of setting the delegate to self is to be able to listen to the information that will come from the connection inside the view controller class. To begin the request, we have used ctn.start().

How to Prepare for the Response

Now that we have sent the Request, we need to prepare for the response that we will get. At the end of viewDidLoad, add the following code:

Swift

1

iTunesSearch("JP Software")

“JP Software” is our search term, so the function will search for any software in the iTunes store which contains that phrase. You can change the search term to what you want to look for from the iTunes store. A data object to contain the results is needed for us to get the results. Let us create an instance of NSMutableData as a class member. Inside the curly brace and just below the class definition, add the following line of code to achieve this:

Swift

1

2

3

vard:NSMutableData=NSMutableData()

vartData:NSArray=NSArray()

Now we are prepared to receive the response. It is time to receive it. Add the following chunk of code:

The NSURLconnection receives the response, and then it will call the receivedResponse method. A new and empty object will be in the line self.d= NSMutableData(). Once the connection is established, data will be received through the method receivedData(). The important information here is the argument passed in the method. Every chunk of data is important, and that is why we have used self.d to append it.

Note that we have used the ctnFinishedLoading() method in the final step. This is after the connection has been established and all the data has been received. We can then use the received data in our app. The NSJSONSerialization converts the received data into data dictionary objects which are useful. This is achieved by deserialization of the results from iTunes.

The final process involves validating the results so as to check on whether they are what we expected. The resulting data can then be set to the table data by self.tData object. The appsTableView reloads its content.

How to Update the Table View UI

Remember that the table view has two functions, a cell function for creation and modification of cells for each row and a count function whose purpose is to count the number of rows. We need to put the data we obtained from the web into the table. Consider the code below:

letcell:UITableViewCell=UITableViewCell(style:UITableViewCellStyle.Subtitle,reuseIdentifier:"Cell for Testing")

varrData:NSDictionary=self.tData[indexPath.row]asNSDictionary

cell.text=rData["trackName"]asString

// Grabbing the artworkUrl60 key for getting image URL for the thumbnail of the app

varuString:NSString=rData["artworkUrl60"]asNSString

variURL:NSURL=NSURL(string:uString)

// Download representation of the image as NSData at the URL

variData:NSData=NSData(contentsOfURL:iURL)

cell.image=UIImage(data:iData)

// obtain the formatted price string to be displayed on the subtitle

varfPrice:NSString=rData["fPrice"]asNSString

cell.detailTextLabel.text=fPrice

returncell

}

numberOfRowsInSection will return the number of resulting objects from the tableData member. This is set tothe ctnFinishedLoading method. The row number has been to grab three values, so we don’t return the row number but the three values. These three keys are then used to construct the title, the subtitle, and the image that will be put along the cell.