Guest User

Untitled

a guest
Apr 24th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. let apiKey = ""
  2.  
  3. let bundleId = ""
  4.  
  5. let SearchEngineId = ""
  6.  
  7. let serverAddress = String(format: "https://www.googleapis.com/customsearch/v1?
  8. q=%@&cx=%@&key=%@",KeyWord,SearchEngineId, apiKey)
  9.  
  10.  
  11. let url = serverAddress.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  12.  
  13. let finalUrl = URL(string: url!)
  14.  
  15. let request = NSMutableURLRequest(url: finalUrl!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10)
  16.  
  17. request.httpMethod = "GET"
  18.  
  19. request.setValue(bundleId, forHTTPHeaderField: "X-Ios-Bundle- Identifier")
  20.  
  21. let session = URLSession.shared
  22.  
  23. let datatask = session.dataTask(with: request as URLRequest) {
  24. (data, response, error) in
  25. do{
  26. if let jsonResult = try JSONSerialization.jsonObject(with:
  27. data!, options: []) as? NSDictionary{
  28. print("asyncResult(jsonResult)")
  29. }
  30.  
  31. }
  32. catch let error as NSError{
  33. print(error.localizedDescription)
  34. }
  35. }
  36. datatask.resume()
Add Comment
Please, Sign In to add comment