我正在尝试快速将MoPub的本地广告手动实施到我的应用中。
我能够成功加载
Loan Calculator[4217:1022703] MOPUB: Successfully loaded native ad.
我在情节提要中创建了一个视图,并且该视图已连接
@IBOutlet var NativeAdView: UIView!
由于MoPub的文档是用Objective C编写的,因此我必须将所有内容快速翻译。这是我的代码,用于加载位于viewDidLoad()下的原生广告。
let settings = MPStaticNativeAdRendererSettings()
settings.renderingViewClass = CalculatorNativeAdView.self
let config = MPStaticNativeAdRenderer.rendererConfiguration(with: settings)
let adRequest = MPNativeAdRequest(adUnitIdentifier: MoPubTestID, rendererConfigurations: [config!])
let targeting = MPNativeAdRequestTargeting()
targeting.desiredAssets = Set<AnyHashable>([kAdTitleKey, kAdTextKey, kAdCTATextKey, kAdIconImageKey, kAdMainImageKey, kAdStarRatingKey])
adRequest?.start(completionHandler: { request, response, error in
if error != nil {
print(error.debugDescription)
} else {
let nativeAd = response
nativeAd?.delegate = self
let nativeAdViewCon: UIView? = try! nativeAd?.retrieveAdView()
nativeAdViewCon?.frame = self.NativeAdView.bounds
print(nativeAdViewCon?.frame)
if let aView = nativeAdViewCon {
self.NativeAdView.addSubview(aView)
}
}
})
CalculatorNativeAdView已连接到我为此原生广告创建的.xib。
import UIKit
import MoPub
class CalculatorNativeAdView: UIView, MPNativeAdRendering {
@IBOutlet var titleLabel: UILabel!
@IBOutlet var mainTextLabel: UILabel!
@IBOutlet var mainImageView: UIImageView!
@IBOutlet var iconImageView: UIImageView!
@IBOutlet var callToActionLabel: UILabel!
@IBOutlet var privacyInformationIconImageView: UIImageView!
override func layoutSubviews() {
super.layoutSubviews()
// layout your views
}
func nativeMainTextLabel() -> UILabel? {
return mainTextLabel
}
func nativeTitleTextLabel() -> UILabel? {
return titleLabel
}
func nativeCallToActionTextLabel() -> UILabel? {
return callToActionLabel
}
func nativeIconImageView() -> UIImageView? {
return iconImageView
}
func nativeMainImageView() -> UIImageView? {
return mainImageView
}
func nativePrivacyInformationIconImageView() -> UIImageView? {
return privacyInformationIconImageView
}
}
我将MPNativeAdDelegate添加到viewController并添加了viewControllerForPresentingModalView()
func viewControllerForPresentingModalView() -> UIViewController! {
return self
}
NativeAdView有框架,所以我认为不是问题所在
Optional((0.0, 0.0, 260.0, 260.0))
广告应显示在此处显示的橙色框中
链接到MoPub的原生广告文档:https://developers.mopub.com/docs/ios/native/
-更新-
我做了一些实验,确认该视图实际上已添加到我的NativeAdView中。我将aView的背景色更改为蓝色。
aView.backgroundColor = UIColor.blue
这是结果。
广告仍然没有展示。将对.xib文件进行一些研究,并确保我做的正确。
-更新-
打印响应的属性给了我这个。
Optional([AnyHashable("privacyiconuiimage"): <UIImage: 0x283eadb90>, {20, 20}, AnyHashable("ctatext"): Go, AnyHashable("title"): MoPub, AnyHashable("iconimage"): https://d30x8mtr3hjnzo.cloudfront.net/creatives/6591163c525f4720b99abf831ca247f6, AnyHa
所以看来我正确地获得了信息。
答案 0 :(得分:1)
在将子视图添加到橙色框后,您可以尝试layoutSubviews()方法,在此链接中,除其他方法可能会对您有所帮助https://medium.com/@abhimuralidharan/ios-swift-setneedslayout-vs-layoutifneeded-vs-layoutsubviews-5a2b486da31c
之外,该方法还有更好的解释。答案 1 :(得分:0)