API Reference
Complete API documentation for all ad formats supported by the OCM AdNetwork SDK.
SDK Configuration
Prebid Config
Use .prebid(accountId:configId:) when initializing the SDK with a local configuration:
let config = OcmConfigBuilder()
.adUnit(
id: "home_top_banner",
format: "banner",
size: "300x250"
)
.gam(
networkCode: "75351959",
adUnitPath: "/75351959/testadunit/test_app_300x250"
)
.prebid(
accountId: "your-prebid-account-id",
configId: "your-prebid-config-id"
)
.privacyFromSdk(
gdpr: 1,
ccpa: "",
coppa: 0
)
.build()
| Parameter | Type | Required | Description |
|---|---|---|---|
accountId | String | Yes | Prebid Server account ID provided by OCM |
configId | String | Yes | Prebid ad unit configuration ID provided by OCM |
🖼️ Banner Ads
Rendering mode
OcmConfigBuilder.adUnit(id:format:size:position:refresh:renderMode:) accepts OcmRenderMode.prebidGam (default) or OcmRenderMode.prebid. The direct Prebid mode requires the ad unit id to be its Prebid configuration ID. See Banner Render Modes.
OcmAdNetworkSDK.setPbsEventLogging(true) logs raw PBS auction requests and responses with the [OCM-PBS] prefix. Disable it with setPbsEventLogging(false). setPbsDebug(_:) separately controls PBS debug mode.
OcmBannerView
Display standard banner ads in your app.
Storyboard Integration
- Drag a
UIViewinto your ViewController in Interface Builder - Open Identity Inspector (⌥⌘3)
- Set Custom Class to
OcmBannerView - Connect via
@IBOutletif needed:
@IBOutlet weak var bannerView: OcmBannerView!
Loading a Banner
bannerView.load(adUnitId: "1001-sreq-test-300x250-imp-1", delegate: self)
Parameters:
adUnitId: The ID of the ad unit from your configurationdelegate: Object conforming toOcmBannerViewDelegate
OcmBannerViewDelegate
Respond to banner ad events:
protocol OcmBannerViewDelegate: AnyObject {
func onAdLoaded()
func onAdFailed(_ error: Error)
func onAdClicked()
func onImpression()
}
Delegate Methods
| Method | Description |
|---|---|
onAdLoaded() | Called when the ad successfully loads |
onAdFailed(_ error: Error) | Called when ad loading fails |
onAdClicked() | Called when user taps the ad |
onImpression() | Called when ad impression is tracked |
Example Implementation
extension ViewController: OcmBannerViewDelegate {
func onAdLoaded() {
print("✅ Ad Loaded")
}
func onAdFailed(_ error: Error) {
print("❌ Ad Failed: \(error.localizedDescription)")
}
func onAdClicked() {
print("👆 Ad Clicked")
}
func onImpression() {
print("📊 Ad Impression")
}
}
🏆 Rewarded Ads
OcmRewardedLoader
Display rewarded video ads and reward users for watching.
Initialization
let loader = OcmRewardedLoader(adUnitId: "ca-app-pub-3940256099942544/5224354917")
loader.delegate = self
Loading and Showing
// Load the ad
loader.load()
// Show when ready
loader.show(from: self)
OcmRewardedLoaderDelegate
Handle rewarded ad events:
protocol OcmRewardedLoaderDelegate: AnyObject {
func rewardedLoaderDidLoadAd(_ loader: OcmRewardedLoader)
func rewardedLoader(_ loader: OcmRewardedLoader, didFailToLoadAdWithCode code: Int, message: String)
func rewardedLoaderDidPresentAd(_ loader: OcmRewardedLoader)
func rewardedLoaderDidDismissAd(_ loader: OcmRewardedLoader)
func rewardedLoader(_ loader: OcmRewardedLoader, didFailToPresentAdWithCode code: Int, message: String)
func rewardedLoader(_ loader: OcmRewardedLoader, didEarnReward result: RewardedResult)
}
Delegate Methods
| Method | Description |
|---|---|
rewardedLoaderDidLoadAd() | Ad has loaded and is ready to show |
didFailToLoadAdWithCode() | Ad failed to load |
rewardedLoaderDidPresentAd() | Ad is now being displayed |
rewardedLoaderDidDismissAd() | User dismissed the ad |
didFailToPresentAdWithCode() | Ad failed to present |
didEarnReward() | User completed the ad and earned reward |
Example Implementation
extension ViewController: OcmRewardedLoaderDelegate {
func rewardedLoaderDidLoadAd(_ loader: OcmRewardedLoader) {
print("✅ Rewarded ad loaded")
}
func rewardedLoader(_ loader: OcmRewardedLoader, didEarnReward result: RewardedResult) {
print("🏆 User earned reward: \(result)")
// Grant reward to user
}
}
🎬 Interstitial Ads
OcmInterstitialLoader
Display full-screen interstitial ads at natural transition points.
Initialization
let interstitialLoader = OcmInterstitialLoader(
prebidConfigAdId: "1001-sreq-test-300x250-imp-1",
gamAdUnitId: "/75351959/testadunit/test_app_interstitial"
)
interstitialLoader.delegate = self
Loading and Showing
// Load the ad
interstitialLoader.loadAd()
// Check if ready and show
if interstitialLoader.isAdReady() {
interstitialLoader.showAd(from: self)
} else {
print("Interstitial not ready yet")
}
OcmInterstitialLoaderDelegate
Handle interstitial ad events:
protocol OcmInterstitialLoaderDelegate: AnyObject {
func onInterstitialLoaded()
func onInterstitialFailedToLoad(reason: String)
func onInterstitialShown()
func onInterstitialClicked()
func onInterstitialClosed()
}
Delegate Methods
| Method | Description |
|---|---|
onInterstitialLoaded() | Ad loaded successfully |
onInterstitialFailedToLoad() | Ad failed to load |
onInterstitialShown() | Ad is now displayed |
onInterstitialClicked() | User tapped the ad |
onInterstitialClosed() | User closed the ad |
Example Implementation
extension ViewController: OcmInterstitialLoaderDelegate {
func onInterstitialLoaded() {
print("✅ Interstitial loaded")
}
func onInterstitialFailedToLoad(reason: String) {
print("❌ Failed: \(reason)")
}
func onInterstitialClosed() {
print("User closed interstitial")
// Resume app experience
}
}
📱 Native Ads
OcmNativeAdLoader
OcmNativeAdLoader supports direct Prebid native rendering and Prebid demand followed by GAM. Initialize the SDK before loading, and retain the loader while its ad is displayed.
Initialization
let loaderNative = OcmNativeAdLoader(
gamAdUnitId: "/6499/example/native",
rootViewController: self,
containerView: yourContainerView,
delegate: self,
configId: "your-prebid-config-id",
mode: .prebid
)
Parameters:
gamAdUnitId: Your GAM native ad unit ID. Required by the initializer; the direct Prebid path does not make a GAM request.rootViewController: The presenting view controllercontainerView: UIView to display the native addelegate: Object conforming toOcmNativeAdLoaderDelegateconfigId: Optional Prebid placement ID. If omitted, the SDK usesprebid.configId, then the first configured ad unit ID.gamNativeFormatId: Optional GAM custom native format ID for a Prebid native creative served through GAM.mode:.prebidrenders the Prebid bid directly;.gam(default) fetches Prebid targeting and requests a GAM native ad.
For GAM mediation, pass mode: .gam and your GAM ad unit ID. Set gamNativeFormatId to the matching GAM custom native format ID if the GAM line item should serve Prebid native wins. Without it, GAM can still serve its own native demand, but the loader cannot resolve a Prebid native win.
Loading
loaderNative.load()
OcmNativeAdLoaderDelegate
Handle native ad events:
@objc protocol OcmNativeAdLoaderDelegate: AnyObject {
func onNativeAdLoaded()
func onNativeAdFailed(_ error: Error)
@objc optional func onNativeAdClicked()
@objc optional func onNativeAdImpression()
}
Delegate Methods
| Method | Description |
|---|---|
onNativeAdLoaded() | Native ad rendered in the supplied container |
onNativeAdFailed(_:) | Native ad failed to load or render |
onNativeAdClicked() | Optional click callback for direct Prebid native ads |
onNativeAdImpression() | Optional impression callback for direct Prebid native ads |
Example Implementation
extension ViewController: OcmNativeAdLoaderDelegate {
func onNativeAdLoaded() {
print("✅ Native ad loaded")
}
func onNativeAdFailed(_ error: Error) {
print("❌ Native ad failed: \(error.localizedDescription)")
}
func onNativeAdClicked() {
print("👆 Native ad clicked")
}
func onNativeAdImpression() {
print("📊 Native ad impression")
}
}
Common Types
RewardedResult
Contains reward information when user completes a rewarded ad:
struct RewardedResult {
let type: String // Reward type
let amount: Int // Reward amount
}
Error Handling
All error delegates provide descriptive error messages. Common error scenarios:
- Network connectivity issues
- Invalid ad unit IDs
- No ad inventory available
- SDK not initialized
- Privacy restrictions
Always implement error delegates to handle these gracefully.
Next Steps
- See complete working examples in Examples
- Learn about Configuration options