Search code examples
iosswiftsearchgoogle-search

How to retrieve Google Search Results in IOS


I set up a Custom Google Search per this post: What are the alternatives now that the Google web search API has been deprecated?.

Now I need to implement it in my app, and I want to fire off a request to search my custom search and I need to work with the results I get back (the JSON I believe).

I would imagine I need to use NSURLSession to make the request, but I can't quite figure out the proper syntax.

This is the custom search's URL: https://cse.google.com/cse/publicurl?cx=XXXXXXXXX:XXXXXXXXXX


Solution

  • Modify this to fit your needs:

    googleUrl = "https://cse.google.com/cse/publicurl?cx=XXXXXXXXX:XXXXXXXXXX"
    guard url = NSURL(string: googleUrl) else { return }
    NSURLSession.sharedSession().dataTaskWithURL(url) {
        (data, response, error) in
        // deal with error etc accordingly       
    }