Created
November 21, 2017 19:58
-
-
Save agermanidis/d3a55b1915a84a1b59e7a2e1a72f5c90 to your computer and use it in GitHub Desktop.
Microsoft Image Search Request example - Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func microsoftImageSearch(_ query : String, completion: @escaping (_ result: [String])->()) { | |
let url = URL(string: "https://api.cognitive.microsoft.com/bing/v7.0/images/search?q=\(query)") | |
var request = URLRequest(url: url!) | |
request.addValue(MICROSOFT_API_KEY, forHTTPHeaderField: "Ocp-Apim-Subscription-Key") | |
URLSession.shared.dataTask(with:request, completionHandler: {(data, response, error) in | |
guard let data = data, error == nil else { return } | |
do { | |
var results : [String] = [] | |
let json = try JSONSerialization.jsonObject(with: data, options :JSONSerialization.ReadingOptions()) as! [String:Any] | |
let value = json["value"] as! [Any] | |
for item in value { | |
let obj = item as! [String: Any] | |
results.append(obj["contentUrl"] as! String) | |
} | |
completion(results) | |
} catch let error as NSError { | |
print(error) | |
} | |
}).resume() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
does this work for you? I'm having some issues to let it work :(