Implementing Google custom search API in iOS
The following provide implementation in Swift 4, of "GET"
request from google custom search engine
,
let apiKey = "Your api key here"
let bundleId = "com.Your uniqueBundleId here"
let searchEngineId = "Your searchEngine here"
let serverAddress = String(format: "https://www.googleapis.com/customsearch/v1?q=%@&cx=%@&key=%@","Your query here" ,searchEngineId, apiKey)
let url = serverAddress.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let finalUrl = URL(string: url!)
let request = NSMutableURLRequest(url: finalUrl!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10)
request.httpMethod = "GET"
request.setValue(bundleId, forHTTPHeaderField: "X-Ios-Bundle-Identifier")
let session = URLSession.shared
let datatask = session.dataTask(with: request as URLRequest) { (data, response, error) in
do{
if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary {
print("asyncResult\(jsonResult)")
}
}
catch let error as NSError {
print(error.localizedDescription)
}
}
datatask.resume()
Brief Step of the process:
- Create a Google account (ignore if you have one)
- You may found some piece of information related to pricing at the bottom of this page helpful(you can ignore this too)
- Create a project and generate the API key
- Go to google console and create a project
- After the project is created, click on it to go to its detail.
- On the left bar under the Auth&API segment, click on APIs.
- Now you'll find CustomSearchAPI link in the Browse APIs section (as it is not activated by default), turn it on by clicking on the button at right.
- Now click on Credentials, just below the APIs option
- On this page under "Public API Access" click on Create New Key Button, for now, choose the browser key(as at first we wanna test it on the browser), create it, and leave it, as it is for now.
- Create Custom Search Engine
- Now on the new tab, open Custom Search Engine page. On this page click on Create a custom search engine, button
- That will lead you to create a new search engine page, here give your domain name in the "Sites to search" field. (If you don't have one no worries, give anything, that has www. at the start and .com in the end)
- Fill in the name, if it hasn't pick one already, then click on create.
- So you got a jumping robot to congratulate you? ;) Yeah, that's it. On this page step up to "Modify your search engine", by clicking on, "Control Panel" button
- There you are, now turn on the Image Search, (if you want to)
- Also in the "Sites to search" section, select, "Search the entire web but emphasize on the included item", instead of, the default one, which is "Search only included site"
- That is it, at the bottom of this page click on update. And then come back to the middle of the page and under the "Detail" title, click on Search engine ID, copy the Id, paste it somewhere.
- Search, using get request:
- To make a get request use this request URL
- In it replace, {API_KEY} which you have created under "Create a project and generate API key" section
- And replace {SEARCH_ENGINE_KEY} with the Search engine Id you just copy-pasted Call it with some different value, at query string, then 'a', https://www.googleapis.com/customsearch/v1?**q=a**&key={API_KEY}&cx={SEARCH_ENGINE_KEY} change a with anything you wanna search you must have got the beautiful JSON of search result
- Other Stuff
- If you wanna see the request status, go back to your project page, that how many requests placed, how many of those failed, etc. Click on the overview and you will get the graph for that, love you google
- If you have trouble with JSON, here are some links at your service,
- What is JSON 1, 2?
- Use JSON in ios.
- Use JSON in android.