- ConSense Account
- Xcode
- Google IMA SDK 3.5.1+
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()
}
}