Skip to content

Instantly share code, notes, and snippets.

@boska
Last active May 8, 2017 03:48
Show Gist options
  • Select an option

  • Save boska/ad51281f2a5b5f9c328930f34abf9a12 to your computer and use it in GitHub Desktop.

Select an option

Save boska/ad51281f2a5b5f9c328930f34abf9a12 to your computer and use it in GitHub Desktop.
iOS example

iOS ConSense API SAMPLE

Prerequisites

  • ConSense Account
  • Xcode
  • Google IMA SDK 3.5.1+

Code Snippet

let videoURL = "your encoded video url"
let apiKey = "your api key"
    let requestURL = URL(string: "https://vsp.viscovery.com/api/vmap?api_key=\(apiKey)&video_url=\(videoURL.toBase64)&platform=mobile")!
let request = URLRequest(url: requestURL)
let task = URLSession.shared.dataTask(with: request) {
  (data, response, error) -> Void in
  let httpResponse = response as! HTTPURLResponse
  let statusCode = httpResponse.statusCode
  if statusCode == 200 {
    do {
      let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String: AnyObject]
      
      let vmap = json["context"] as? String ?? "" // vmap xml string
      
      // construct ads request with static string
      let request = IMAAdsRequest(adsResponse: vmap, adDisplayContainer: IMAAdDisplayContainer(adContainer: self.contentVideoView, companionSlots: nil), contentPlayhead: self.contentPlayhead, userContext: nil)
      self.adsLoader.requestAds(with: request) // IMAAdsLoader
      
    } catch {
      self.contentPlayer.play()
      print("Error with Json: \(error)")
    }
    
  } else {
    self.contentPlayer.play()
  }
}
task.resume()

Extension for base64 string

extension String {
  var toBase64: String {
    return Data(self.utf8).base64EncodedString()
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment