Network Development In Swift


Network development allows us to send and receive data from remote services and develop  applications that communicate over a network connection and connect to web based services like representational state transfer. It is a software architectural style of the world wide web and these services communicate over HTTP verbs. The classes are used to connect and retrieve information from Rest services. 
  • Default session configuration - It is similar to NSURLConnection on API
  • Background session configuration - It allows for uploads and downloads to be performed, when the application is running on the background.
  • Ephemeral session configuration - It is a default session configuration and it does not cache anything to disk.

HTTP get request

Rest standard is used to get request to retrieve data from a service by using a get request to create or update a data object. The below code is used to request and print the result to the console.
  1. public typealias dataFromURLcompletionClosure = (NSURLResponse!, NSData!) - > Void  
  2. public func sendGetRequest(handler: public func getConnect(handler: dataFromUrlCompletionClosure) {  
  3.             let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration();  
  4.             let urlString = ""  
  5.             if let encodeString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()),  
  6.                 url = NSURL(string: encodeString) {  
  7.                     let request = NSMutableURlRequest(URL: url)  
  8.                     request.HTTPMethod = "Get "  
  9.                     let urlSession = NSURLSession(configuration: sessionConfiguration, delegate: nil, delegateQueue: nil)  
  10.                     let sessionTask = urlSession.dataTaskWithRequest(request) {  
  11.                         (data, response, error) in handler(response, data)  
  12.                     }  
  13.                     sessionTask.resume()  
  14.                 }  
  15.         }   
The NSURL object represents the URL and it is used to represent the local file on the disk and to represent the URL of remote services. The NSMutableURLRequest is a subclass which represents a URL load request and is used to encapsulate the URL and request properties. It is mainly used to encapsulate the necessary information to make requests. The NSURLHTTPResponse is used to encapsulate the metadata associated with responses to URL requests. It provides methods for accessing specific information associated with the HTTP response.

Rest web services

Rest is used for stateless communication between the devices. Rest based services are to connect more devices to the internet and design network applications. Simple HTTP requests are used for communication among devices. The post request is used to create or update data and get request is used to read the data and the delete request is used to delete the data.

Http post request

To send or post data to the server the POST request is used and data takes the form of key pairs. The pairs are separated by an ampersand symbol and the keys are separated by the equals sign. The dataUsingEncoding method is used to GET request to properly encode the POST data. The below function loops through each key or value pair of Dictionary object and creates a string object that combines the key and value separated by equal sign. To join each item in array separated by specified string the joinWithSeparator function is used.
  1. func dictionaryToQueryString(dict: [String: String]) - > String {  
  2.     var parts = [String]()  
  3.     for (key value) in dict {  
  4.         let part: String = key + "=" + value  
  5.         parts.append(part);  
  6.     }  
  7.     return parts.joinWithSeparator("&")  
  8. }   
The sendPostRequest function accepts one argument named handler, which is a closure that conforms to the DataFromURLCompletionClosure type. The application that communicates with other devices and services over the internet must have internet connection to make internet calls. If the application sends a large quantity of data, the enum will be used as return type for networkConnectionType (),
  1. public enum ConnectionType      
  2. {      
  3.  case NONETWORK      
  4.  case MOBILE3GMETWORK      
  5.  case WIFINETWORK      
  6. }     
Make a get request to the service with the RSURLRequest and provide the URL and parameters to send the service.


This API has a simple and easy interface to make a single GET request to the sevice