iOS Banner Ad Integration
Overview
Banner ads are rectangular ads displayed at the top or bottom of the app interface, with the following characteristics:
- Small footprint, does not affect user experience
- Continuous display, increasing ad exposure
- Fast loading, responsive
- Suitable for various app scenarios
Banner ads need to be displayed through a container view (container: UIView).
Integration Steps
1. Create and Load Banner Ad
swift
import UIKit
import UjuAdCore
final class BannerAdHelper {
private var bannerAd: UjuAdObject?
func load(vc: UIViewController) {
// 创建广告配置,指定广告位 ID 与广告尺寸
let config = UjuAdConfig(
placementId: "YOUR_BANNER_PLACEMENT_ID",
adViewSize: AdViewSize.bannerSize320x50 // 320x50
)
let banner = UjuAdObject.getBannerObject(vc, config: config)
banner.setAdObjectListener(BannerListener(helper: self))
self.bannerAd = banner
banner.load()
}
func show(vc: UIViewController, container: UIView) {
// 展示前判断 isReady,需传入承载横幅的容器视图
if bannerAd?.isReady() == true {
bannerAd?.show(vc, container: container)
}
}
func destroy() {
bannerAd?.destroy()
bannerAd = nil
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2. Implement Banner Listener
swift
final class BannerListener: FeedAdObjectListener, @unchecked Sendable {
private weak var helper: BannerAdHelper?
init(helper: BannerAdHelper) { self.helper = helper }
func onLoadSuccess() {
// 加载成功,可在合适时机调用 show(vc, container:)
}
func onLoadError(error: UjuException) {
print("横幅加载失败: \(error.description)")
}
func onAdShow() {
// 通过 getAdInfo() 获取广告信息,读取 ecpm
// let ecpm = helper?.bannerAd?.getAdInfo()?.ecpm
}
func onAdClicked() {
// 广告被点击
}
func onAdClosed() {
// 广告关闭
}
func onLpClosed() {
// 落地页关闭
}
func onAdError(error: UjuException) {
print("横幅展示错误: \(error.description)")
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
UjuAdConfig Configuration
| Parameter | Type | Required | Description |
|---|---|---|---|
placementId | String | Yes | Ad placement ID |
adViewSize | AdViewSize? | No | Ad size, only effective for template rendering, default nil |
scenarioKey | String? | No | Ad display scenario identifier, used for analytics |
userId | String? | No | User ID |
customData | [String: String]? | No | Custom data, passed through to the ad server |
bidFloor | Double | No | Bid floor (CNY, CPM), passed by aggregation platform, not needed for normal integrators |
Ad Sizes
The UjuAd iOS SDK provides the following preset banner sizes (AdViewSize static properties, in pixels):
| Preset Property | Width × Height | Description |
|---|---|---|
AdViewSize.bannerSize320x50 | 320 × 50 | Standard banner (recommended) |
AdViewSize.bannerSize320x100 | 320 × 100 | Large banner |
AdViewSize.bannerSize320x75 | 320 × 75 | Smart banner |
To customize the size, construct directly:
swift
let customSize = AdViewSize(width: 300, height: 250)1
For the complete
AdViewSizepresets (including Native sizes), see API Reference.
Best Practices
1. Ad Placement
- Bottom placement: The most common position, does not obscure main content
- Top placement: Suitable for certain specific app scenarios
- Between content: Interspersed in long articles or lists
2. Optimization Recommendations
- Preload ads: Load in advance before needing to display ads
- Handle network status: Adjust loading strategy appropriately when network is poor
- Test different sizes: Choose the appropriate ad size based on your app interface
3. Issues to Avoid
- Do not place multiple banner ads on the same screen
- Do not obscure core app functionality
- Do not place ads in areas with frequent user interaction
FAQ
Q: Why doesn't the banner ad show?
A: Possible causes:
- Incorrect ad placement ID
- Network connection issues
- Insufficient ad inventory (error code 102
noFill) - Container view is not visible or has zero size
- Did not call
show(vc, container:)afteronLoadSuccess
Q: Can banner ads be used in UITableView/UICollectionView?
A: Yes, but note:
- Manage the ad view lifecycle in cells
- Properly handle
destroy()for ad objects to avoid memory leaks - Avoid frequent creation and destruction of ad views
