Search code examples
swifturlrequestthemoviedb-api

URL request using Swift


I have access the "dictionary" moviedb for example : https://www.themoviedb.org/search/remote/multi?query=exterminador%20do%20futuro&language=en

How can i catch only the film's name and poster from this page to my project in Swift ?


Solution

  • It's answer :)

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            reload()
        }
    
        private func reload() {
            let requestUrl = "https://www.themoviedb.org/search/remote/multi?query=exterminador%20do%20futuro&language=en"
    
            let config = NSURLSessionConfiguration.defaultSessionConfiguration()
            let session = NSURLSession(configuration: config)
            let request = NSURLRequest(URL: NSURL(string: requestUrl)!)
    
            let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
                if let error = error {
                    println("###### error ######")
                }
                else {
                    if let JSON = NSJSONSerialization.JSONObjectWithData(data,
                        options: .AllowFragments,
                        error: nil) as? [NSDictionary] {
    
                            for movie in JSON {
                                let name = movie["name"] as! String
                                let posterPath = movie["poster_path"] as! String
    
                                println(name)        // "Terminator Genisys"
                                println(posterPath)  // "/5JU9ytZJyR3zmClGmVm9q4Geqbd.jpg"
                            }
                    }
                }
            })
    
            task.resume()
        }
    }