Implementing Google custom search API in iOS

蓝咒 提交于 2019-12-17 07:13:01

问题


I went through several links in order to find the proper steps to implement google customsearchapi in an ios application and spent about 6-7 hours in that process.

Links:

  • https://developers.google.com/custom-search/json-api/v1/introduction
  • http://developers.google.com/apis-explorer/#p/customsearch/v1/search.cse.list?q=a&_h=1&
  • https://productforums.google.com/forum/#!topic/customsearch/hT2fnfErVwo
  • Google Custom Search: 403 error in iOS
  • And Father of all

All these provide bits and peaces of formation. Is there any place to have a summarize, precise info that can help to implement the custom search in an iOS application?


回答1:


Brief Step of the process:

  1. Create a Google account (ignore if you have one)
  2. You may found some peace of information related to pricing at the bottom of this page helpful(you can ignore this too)
  3. Create project and generate API key
    1. Go to google consol and create a project
    2. After project created, click on it to go to it's detail.
    3. On the left bar under Auth&API segment, click on APIs.
    4. Now you'll find CustomSearchAPI link in Brows APIs section (as it is not activated by default), turn it on by clicking on the button at right.
    5. Now click on Credentials, just below APIs option
    6. On this page under "Public API Access" click on Create New Key Button, for now choose browser key(as at first we wanna a test it on browser), create it and leave it, as it is for now.
  4. Create Custom Search Engine
    1. Now on the new tab, open Custom Search Engine page. On this page click on Create a custom search engine, button
    2. That will lead you to create new search engine page, here give your domain name in "Sites to search" field. (If you don't have one no worries, give any thing, that have www. in the start and .com in the end)
    3. Fill name, if it haven't pick one already, then click on create.
    4. So you got a jumping robo to congratulate you? ;) Yeah that's it. In this page step up to "Modify your search engine", by clicking on, "Control Panel" button
    5. There you are, now turn on the Image Search, (if you want to)
    6. Also in "Sites to search" section, select, "Search the entire web but emphasize on included item", instead of, the default one, which is "Search only included site"
    7. That is it, at the bottom of this page click on update. And then come back to middle of the page and under the "Detail" title, click on Search engine ID, copy the Id, paste it somewhere.
  5. Make a search, using get request:
    1. To make a get request use this request URL
    2. In it replace, {API_KEY} which you have created under "Create project and generate API key" section
    3. And replace {SEARCH_ENGINE_KEY} with the Search engine Id you just copy pasted Call it with some different value, at query string, than 'a', https://www.googleapis.com/customsearch/v1?q=a&key={API_KEY}&cx={SEARCH_ENGINE_KEY} change a with any thing you wanna search you must have got the beautiful JSON of search result
  6. Other Stuff
    1. If you wanna see the request status, go back to your project page, that how may request placed, how many of those failed, ect. Click on the overview and you will get the graph for that, love you google
    2. If you have trouble with JSON, here are some links at your service,
      1. What is JSON 1, 2?
      2. Use JSON in ios.
      3. Use JSON in android.



回答2:


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()


来源:https://stackoverflow.com/questions/27039010/implementing-google-custom-search-api-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!