1

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 ?

Mercenario
  • 45
  • 1
  • 6

2 Answers2

1

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()
    }
}
  • I used the example of the movie: Terminator but In the case of my project, the user will do the search for any movie. so this link : "https://www.themoviedb.org/search/remote/multi?query=exterminador%20do%20futuro&language=en" it will be broken . It's fixed : https://www.themoviedb.org/search/remote/multi?query=(the name that the user searched) if there is space between the words replaced by "%20 " and the final "&language=en" it's fixed. How can I do that in Swift ? – Mercenario Jul 29 '15 at 13:45
  • you can write this: `let query = "Terminator second"` `let encodedQuery = query.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())`. encodedQuery is "Terminator%20second". You make request url with `let requestUrl = "https://www.themoviedb.org/search/remote/multi?query=\(encodingQuery)&language=en"` – Ryosuke Hiramatsu Jul 29 '15 at 13:49
0

You need to include your api key along with the request. I'd just try something like this to see if it works or not. If it does, then you can go about using the api key in a different way to make it more secure. I wouldn't bother as it's not an api with much sensitive functionality.

let query = "Terminator+second"
let url = NSURL(string: "http://api.themoviedb.org/3/search/keyword?api_key=YOURAPIKEY&query=\(query)&language=β€Œβ€‹en")!
let request = NSMutableURLRequest(URL: url)
request.addValue("application/json", forHTTPHeaderField: "Accept")

let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { data, response, error in
    if let response = response, data = data {

    print(response)

    //DO THIS
    print(String(data: data, encoding: NSUTF8StringEncoding))

    //OR THIS
    if let o = NSJSONSerialization.JSONObjectWithData(data, options: nil, error:nil) as? NSDictionary {
        println(dict)
    } else {
        println("Could not read JSON dictionary")
    }
} else {
    print(error)
}
}

task.resume()

The response you'll get will have the full list of properties. You need the poster_path and title (or original_title) property of the returned item.

Septronic
  • 1,156
  • 13
  • 32