创建共享横幅。你init
把它放在你的AppDelegate
,然后把它添加到UIViewController
你想在上面放横幅的地方:
class AppDelegate: UIResponder, UIApplicationDelegate, GADBannerViewDelegate {
var window: UIWindow?
var adBannerView = GADBannerView()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
adBannerView.adUnitID = "YourAdUnitID"
adBannerView.delegate = self
adBannerView.load(GADRequest())
adBannerView.isHidden = true
return true
}
func adViewDidReceiveAd(_ bannerView: GADBannerView!) {
adBannerView.isHidden = false
}
func adView(_ bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
adBannerView.isHidden = true
}
在每个UIViewController
你想有一个横幅:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
addBannerToView()
}
func addBannerToView() {
appDelegate.adBannerView.adSize = kGADAdSizeBanner
appDelegate.adBannerView.rootViewController = self
appDelegate.adBannerView.frame = CGRect(x: 0.0,
y: view.frame.height - appDelegate.adBannerView.frame.height,
width: view.frame.width,
height: appDelegate.adBannerView.frame.height)
view.addSubview(appDelegate.adBannerView)
}