Tuesday 4 April 2017

Swift : Json to get all data



Step - 1 : Add security to app in info.plist file
Open info.plist --> Bundle Version --> click on (+) --> Select 'App Transport Security Setting' --> click on (+) --> Select 'Allow Arbitrary Load' --> change value to 'YES'.

Step - 2 : Open ViewController.swift and place below code

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
    let url = URL(string: "http://api.androidhive.info/contacts/")
        let task = URLSession.shared.dataTask(with: url!){
            (data, response, error) in
            
            if error != nil{
                print("Error")
            }else{
                if let content = data
                {
                    do{
                        let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                        print(myJson)
                        
                    }catch
                    {
                        
                    }
                }
            }
            
        }
        task.resume()
        
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Step - 3 : Now run the app

Note :  To see the result,  Click on hide or Show Debug area --> Show Console
For help, See Image


Choose


Now Click Show Console to get Json Result





No comments:

Post a Comment