Monday, September 22, 2014

Webservice call in swift iOS

In this article we call webservice using the swift programming language in iPhone.

To make webservice call using swift language first of create NSURL with your webservice url and create NSURLRequest using NSURL.

        let nsurl:NSURL = NSURL(string:"PUT YOUR WEB_SERVICE URL HERE")
        let request:NSURLRequest = NSURLRequest(URL: nsurl)
Now create connection with server using NSURLConnection using Asynchronous.
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) in
            println(NSString(data: data, encoding: NSUTF8StringEncoding))
          
            var nsdata:NSData = NSData(data: data)
            var response:NSString = NSString(data: data, encoding:                                                       NSUTF8StringEncoding)
            println("This is the final response \(response)")
In this article I parse the JSON response. To parse the json response create below method for parse the JSON response.

    func parseJSON(inputData: NSData){
        var error: NSError?
        var boardsDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary

For get any string from the JSON response use below line of code.
      var status:NSString = boardsDictionary["YOUR KEY OF JSONOBJECT"] as NSString

For get JSON array from JSON response use below code 
        var tableList:NSArray = boardsDictionary["YOUR KEY FOR JSONARRAY"] as NSArray
       } 

That's it...

Full code for your UIViewController.

import UIKit

class getWebserviceCall: UIViewController {
  
    override func viewDidLoad() {
        super.viewDidLoad()
       
        let nsurl:NSURL = NSURL(string:"YOUR WEBSERVICE URL")
        let request:NSURLRequest = NSURLRequest(URL: nsurl)
       
      
        let indicator:UIActivityIndicatorView = UIActivityIndicatorView (activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        indicator.color = UIColor .magentaColor()
        indicator.frame = CGRectMake(0.0, 0.0, 10.0, 10.0)
        indicator.center = self.view.center
        self.view.addSubview(indicator)
        indicator.bringSubviewToFront(self.view)
       
        indicator.startAnimating()
       
        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) in
            println(NSString(data: data, encoding: NSUTF8StringEncoding))
          
            var nsdata:NSData = NSData(data: data)
            var response:NSString = NSString(data: data, encoding: NSUTF8StringEncoding)
            println("This is the final response \(response)")
            indicator.stopAnimating()
           
                self.parseJSON(nsdata)
            })
       
    }
   
    func parseJSON(inputData: NSData){
        var error: NSError?
        var boardsDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary
       
        var status:NSString = boardsDictionary["YOUR JSONOBJECT KEY"] as NSString
        var tableList:NSArray = boardsDictionary["YOUR JSONARRAY KEY"] as NSArray
        
      
    }
}

Hope this post is helpful for you..






No comments:

Post a Comment