ברוב המקרים שבהם משתמשים ב-IMA SDK, צריך לנהל רק בקשה אחת להצגת מודעה בכל פעם. עם זאת, יש מקרים מיוחדים שבהם צריך לשלוח כמה בקשות בו-זמנית, למשל הטמעה של טעינה מראש של נתוני מודעות לפני שהמשתמש בוחר סרטון. בקשות להצגת מודעות מתבצעות באופן אסינכרוני, ולכן יכול להיות שייראה לכם קשה לוודא שמנהל המודעות הנכון משויך להקשר הנכון.
כדי לפשט את תהליך ההבחנה בין כמה פלטפורמות לניהול מודעות, IMA SDK ל-iOS מאפשר לבעלי תוכן דיגיטלי להעביר כל ערך או אובייקט לשדה UserContext של כל בקשה להצגת מודעה. אחר כך אפשר לאחזר את הערך או האובייקט האלה בפונקציית הנציג AdsLoader:AdsLoadedWithData, דרך המאפיין userContext של האובייקט IMAAdsLoadedData.
דוגמה
...
adsLoader = IMAAdsLoader(settings: nil)
adsLoader.delegate = self
let userContextA = {id: "Request A", element: videoElementA}
let userContextB = {id: "Request B", element: videoElementB}
let requestA = IMAAdsRequest(
adTagUrl: ViewController.AdTagURLString,
adDisplayContainer: adDisplayContainer,
contentPlayhead: contentPlayhead,
userContext: userContextA)
let requestB = IMAAdsRequest(
adTagUrl: ViewController.AdTagURLString,
adDisplayContainer: adDisplayContainer,
contentPlayhead: contentPlayhead,
userContext: userContextB)
adsLoader.requestAds(with: requestA)
adsLoader.requestAds(with: requestB)
...
// MARK: - IMAAdsLoaderDelegate
func adsLoader(_ loader: IMAAdsLoader!, adsLoadedWith adsLoadedData: IMAAdsLoadedData!) {
let userContext = adsLoadedData.userContext
print("Loaded ads for ID: " + userContext.id)
adsManager = adsLoadedData.adsManager
adsManager.initialize(with: nil)
}
func adsLoader(_ loader: IMAAdsLoader!, failedWith adErrorData: IMAAdLoadingErrorData!) {
let userContext = adsLoadingErrorData.userContext
print("Error loading ads for ID: " + userContext.id)
}
...