API Reference
Complete API documentation for all ad formats supported by the OCM AdNetwork SDK.
🖼️ Banner Ads
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
Display native ads that match your app's look and feel.
Initialization
let loaderNative = OcmNativeAdLoader(
gamAdUnitId: "/6499/example/native",
rootViewController: self,
containerView: yourContainerView,
delegate: self
)
Parameters:
gamAdUnitId: Your GAM native ad unit IDrootViewController: The presenting view controllercontainerView: UIView to display the native addelegate: Object conforming toOcmNativeAdLoaderDelegate
Loading
loaderNative.load()
OcmNativeAdLoaderDelegate
Handle native ad events:
protocol OcmNativeAdLoaderDelegate: AnyObject {
func customNativeAdDidLoad(_ ad: CustomNativeAd, into view: UIView)
func nativeAdDidLoad(_ ad: NativeAd, into view: UIView)
func nativeAdFailedToLoad(error: String)
}
Delegate Methods
| Method | Description |
|---|---|
customNativeAdDidLoad() | Custom native ad loaded successfully |
nativeAdDidLoad() | Standard Google native ad loaded |
nativeAdFailedToLoad() | Ad failed to load |
Example Implementation
extension ViewController: OcmNativeAdLoaderDelegate {
func customNativeAdDidLoad(_ ad: CustomNativeAd, into view: UIView) {
print("✅ CustomNativeAd loaded")
}
func nativeAdDidLoad(_ ad: NativeAd, into view: UIView) {
print("✅ Google NativeAd loaded")
}
func nativeAdFailedToLoad(error: String) {
print("❌ Native ad failed: \(error)")
}
}
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