MobWithAD iOS SDK
支持环境 |
---|
部署目标:iOS 13.0 及以上 |
最新版本的 Xcode (Xcode 13.0 / Swift 5.3) |
MobWithAD SDK是用 Swift 开发的。如果要在基于 Swift 的项目中使用MobWithAD SDK,请务必使用最新版本的 Xcode。
1. 安装 SDK
1) 使用 Cocoapods 安装
将 'MobWithAD' 添加到项目的 Podfile 中。
pod 'MobWithAD'
2) 手动安装
- 下载数字广告框架 SDK。
- 将 MobWithADSDKFramework.framework 拖放到应用程序项目的 General > Embedded Binaries 项。
- 本框架是利用 AdFitSDK 的业务板模板开启的。因此,需要额外安装 AdFitSDK。有关详细信息,请点击 这里 按照AdFitSDK 的安装指南进行。
- AdFitSDK 版本已优化为 3.12.22。
2. 项目的设置
1) iOS 9 ATS(App Transport Security)处理
从 iOS 9 开始,ATS(App Transport Security) 功能默认启用,只允许加密的 HTTPS 方式的通信。
MobWithAD SDK 设计为在 ATS 启用状态下也能正常工作,但是通过广告显示的广告主页面可能不支持 HTTPS。
因此,请将以下内容应用到应用程序项目的 Info.plist 文件中。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
2) Objective-C 项目
MobWithAD SDK 是用 Swift 开发的。在 Objective-C 项目的项目中使用 MobWithAD SDK,需要将 Swift 标准库嵌入到项目中。
在应用程序项目构建设置中,将 Always Embed Swift Standard Libraries 项设置为 Yes。
4) ATT(App Tracking Transparency) 框架应用
针对 iOS14 的应用程序必须应用 ATT 框架才能获取 IDFA。
(1) 更新 Info.plist
应用程序使用用户或设备的数据权限进行追踪的原理,需要在用户界面中提醒用户。
<key> NSUserTrackingUsageDescription </key>
<string> 맞춤형 광고 제공을 위해 사용자의 데이터가 사용됩니다. </string>
(2) ATTrackingManager 代码应用
MobWithAD SDK 在调用 loadAD() 函数用于广告加载时,将自动审阅并获取权限。但是,如果直接处理权限,也可以进行如下控制。
if #available(iOS 14, *) {
ATTrackingManager.requestTrackingAuthorization { (status) in
if status == .authorized {
mobWithAdView.loadAd()
}
}
}
else {
mobWithAdView.loadAd()
}
3. 广告请求
1. 框架导入
import MobWithADSDKFramework // MobWithAD SDK 추가
import AdFitSDK // AdFitSDK 추가 (비즈보드를 사용하는 경우, 여백 설정을 위해 필요합니다)
2. SDK 初始化
MobWithADSDK.standard.initSDK()
3. 广告请求及应用
let mobWithAdView = MobWithAdView.init(CGRect(x: 0, y: 100, width: width, height: height),
type: .BANNER_320x50,
bannerUnitId: '발급받은 광고 UNIT ID')
self.view.addSubview(mobWithAdView)
mobWithAdView.loadAd()
当前支持的广告类型如下。
enum MMBannerType: Int {
case Banner320x50
case Banner300x250
}
1) 广告自动刷新
像以下示例一样,将 MobWithAdView 的 interval 设置为 60 或以上,则广告横幅将按设置的时间间隔自动刷新。
设置值是 '秒'的值,必须设置为 60 秒或以上。
设置为 0 情况下自动刷新不会执行,而小于 60 的值会每 60 秒执行一次。
mobWithAdView?.interval = 60
2) 广告自动刷新停止及重新开始
广告自动刷新的停止及重新开始,请参考以下命令。
mobWithAdView?.stop() // 광고 자동 갱신 중지
mobWithAdView?.restart() // 광고 자동 갱신 재시작
4. 广告视图的宽度和高度设置
1. 共同
要显示广告的视图大小必须与以下 BannerType 的尺寸相同。
mobWithAdView = MobWithAdView.init(CGRect(x: 0, y: 100, width: 320, height: 50),
type: .BANNER_320x50,
bannerUnitId: '발급받은 광고 UNIT ID')
2. 使用 AdFit 业务板时
对于业务板广告,已通过为基本分配适当的空间进行实现。因此在创建并加载数字广告之前,请参考以下代码并设置必要的空间。
...
BizBoardTemplate.defaultEdgeInset = UIEdgeInsets.init(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
...
此外,对于在业务板中进行广告显示,还要求广告视图的大小比率。以下是使用该比率应用广告的方法。
- 从 1.2.0 版本起,开发了将广告展示在用户指定的屏幕尺寸上的功能,因此以下仅供参考。
let width = UIScreen.main.bounds.width
let height = (view.frame.width - BizBoardTemplate.defaultEdgeInset.left + BizBoardTemplate.defaultEdgeInset.right) / (1029 / 258) + BizBoardTemplate.defaultEdgeInset.top + BizBoardTemplate.defaultEdgeInset.bottom
mobWithAdView = MobWithAdView.init(CGRect(x: 0, y: 100, width: width, height: height),
type: .BANNER_320x50,
bannerUnitId: '발급받은 광고 UNIT ID')
有关广告视图宽度和高度的更多详细信息,请参考 AdFitSDK的业务板模板 中的 广告视图宽度和高度设置 以及该部分的以下 视图类型。
5. MobWithADViewDelegate
当发生广告接收成功或失败、广告点击等情况时,想要接收响应,请按照以下方法添加和应用 MobWithADViewDelegate 协议。
class ViewController: UIViewController, MobWithADViewDelegate {
.....
let mobWithAdView = MobWithAdView.init(CGRect(x: 0, y: 100, width: width, height: height),
type: .BANNER_320x50,
bannerUnitId: '발급받은 광고 UNIT ID')
mobWithAdView.adDelegate = self
.....
}
请通过以下Delegate方法针对不同情况进行相应操作。
func mobWithAdViewDidReceivedAd() {
// 광고 수신 성공
}
func mobWithAdViewDidFailToReceiveAd() {
// 광고 수신 실패
}
func mobWithAdViewClickedAd() {
// 광고 배너 클릭시 발생
}
4. MMNativeAdView
MMNativeAdView是一个仅负责使用户直接设置视图,并将配置的视图由SDK传递给各个视图,以设置广告数据的AdView。如果没有正确设置视图,则广告可能无法正确显示,请注意。
1) 广告调用方法
var nativeAdView = MMNativeAdView(bannerUnitId: mediaCode,
adContainerView: adContainerView,
nativeAdRootView: nativeAdRootView,
adImageView: thumbnailImageView,
logoImageView: logoImageView,
titleLabel: titleLabel,
descriptionLabel: descLabel,
gotoSiteButton: goButton,
infoLogoImageView: infoLogoImageView)
nativeAdView?.adDelegate = self
nativeAdView?.loadAd()
2) 无法使用广告点击按钮的情况
nativeAdView?.performAdClicked()
调用该方法可以将广告点击的效果传递出去。
5. MobWithNativeAdLoader广告示例
当需要在列表类型视图上展示MMNativeAdView时,可以使用MobWithNativeAdLoader。
1. 广告加载方法
首先创建广告展示的View。在此,这些视图需要通过MobwithNativeAdViewRender进行扩展,并为每个方法定义,如果您使用AppLovin,则必须继承自MANativeAdView。
class NativeAdLoaderView: MANativeAdView {
static let needHeight:CGFloat = 347.0
@IBOutlet weak var thumbnailImageView: UIImageView!
@IBOutlet weak var logoImageView: UIImageView!
@IBOutlet weak var infoLogoImageView: UIImageView!
@IBOutlet weak var adTitleLabel: UILabel!
@IBOutlet weak var descLabel: UILabel!
@IBOutlet weak var goButton: UIButton!
}
extension NativeAdLoaderView: MobwithNativeAdViewRender {
func getAdImageView() -> UIImageView? {
return thumbnailImageView
}
func getAdLogoImageView() -> UIImageView? {
return logoImageView
}
func getAdTitleLabel() -> UILabel? {
return adTitleLabel
}
func getAdDescriptionLabel() -> UILabel? {
return descLabel
}
func getGoToSiteButton() -> UIButton? {
return goButton
}
func getInfoLogoImageView() -> UIImageView? {
return infoLogoImageView
}
}
接下来,通过创建和初始化MobWithNativeAdLoader来加载广告。您需要将先前创建的广告展示View传递给它。
//MobwithNativeAdLoader 생성
let mediaCodes:[String] = [ "광고 Unit ID" ] //1개 이상의 Unit를 설정해 주어야 합니다.
var nativeAdLoader = MobWithNativeAdLoader(unitIds: mediaCodes, nibName: "NativeAdLoaderView", bundle: nil)
nativeAdLoader.nativeAdLoaderDelegate = self
//광고를 표시할 View 설정
adLoader.setNativeADView(this,
R.layout.custom_native_ad_view,
R.id.mediaContainerView,
R.id.imageViewAD,
R.id.imageViewLogo,
R.id.textViewTitle,
R.id.textViewDesc,
R.id.buttonGo,
R.id.infoViewLayout,
R.id.imageViewInfo);
.......
然后,您可以在列表类型视图(如UITableView)中按照以下方式检索广告View并在屏幕上展示。
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath.row % 5 == 4) {
let cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "NativeADCellID", for: indexPath)
// loadAD()를 호출하면 이미 로드된 광고가 있거나 이미 생성된 뷰가 있는 경우 해당 View를 전달해 줍니다.
// isLoadedAd()를 호출하면 광고를 받아온 경우 true를 반환합니다. 해당 값을 확인후 뷰에 추가하는것을 권장드립니다.
if let adView = nativeAdLoader.loadAd(At: indexPath), nativeAdLoader.isLoadedAd(At: indexPath) {
cell?.addSubview(adView)
adView.translatesAutoresizingMaskIntoConstraints = false
cell?.widthAnchor.constraint(equalTo: adView.widthAnchor).isActive = true
cell?.heightAnchor.constraint(equalTo: adView.heightAnchor).isActive = true
cell?.centerXAnchor.constraint(equalTo: adView.centerXAnchor).isActive = true
cell?.centerYAnchor.constraint(equalTo: adView.centerYAnchor).isActive = true
}
else {
if nativeAdLoader.isFailLoadAd(At: indexPath) {
nativeAdLoader.retryLoadAd(At: indexPath)
}
cell?.subviews.forEach({ view in
(view as? NativeAdLoaderView)?.removeFromSuperview()
})
}
return cell ?? UITableViewCell.init()
}
else {
...
}
...
- 更详细的信息,请参考Sample应用。