AdMob工具(仅限代码,无主故事板)

时间:2018-01-28 19:50:39

标签: swift admob

我想在我的应用中添加广告横幅。 我没有故事板,我有4个ViewCOntroller,主要是我可以帮助我吗?

在AppDelegate中:GADMobileAds.configure(withApplicationID: "...")

import UIKit
import Firebase
import GoogleMobileAds

class ViewController: UIViewController {
    var window: UIWindow?

    override func viewDidLoad(){
        super.viewDidLoad()

        self.title="Andrea Damante Quiz"
        self.view.backgroundColor=UIColor(patternImage: UIImage(named: "andrea15.jpeg")!)

        let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
        backgroundImage.image = UIImage(named: "andrea15.jpeg")
        backgroundImage.contentMode =  UIViewContentMode.scaleAspectFill
        self.view.insertSubview(backgroundImage, at: 0)
        setupViews()
    }

    @objc func btnGetStartedAction() {
        let v=QuizVC()
        self.navigationController?.pushViewController(v, animated: true)
    }

    func setupViews() {
        self.view.addSubview(lblTitle)
        lblTitle.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 150).isActive=true
        lblTitle.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive=true
        lblTitle.widthAnchor.constraint(equalToConstant: 250).isActive=true
        lblTitle.heightAnchor.constraint(equalToConstant: 80).isActive=true

        self.view.addSubview(btnGetStarted)
        btnGetStarted.heightAnchor.constraint(equalToConstant: 50).isActive=true
        btnGetStarted.widthAnchor.constraint(equalToConstant: 150).isActive=true
        btnGetStarted.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive=true
        btnGetStarted.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 0).isActive=true
    }

    let lblTitle: UILabel = {
        let lbl=UILabel()
        lbl.text="Quiz"
        lbl.textColor=UIColor.darkGray
        lbl.textAlignment = .center
        lbl.font = UIFont.systemFont(ofSize: 46)
        lbl.numberOfLines=2
        lbl.translatesAutoresizingMaskIntoConstraints=false
        return lbl
    }()

    let btnGetStarted: UIButton = {
        let btn=UIButton()
        btn.setTitle("Via!", for: .normal)
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.backgroundColor=UIColor.orange
        btn.layer.cornerRadius=5
        btn.layer.masksToBounds=true
        btn.translatesAutoresizingMaskIntoConstraints=false
        btn.addTarget(self, action: #selector(btnGetStartedAction), for: .touchUpInside)
        return btn
    }()
}

1 个答案:

答案 0 :(得分:0)

您需要声明并设置Exception thrown at 0x771718D6 (ntdll.dll) in vstest.executionengine.x86.exe: 0xC0000138: Ordinal Not Found.并将其添加到视图控制器。你不会需要GADBannerView。使用此代码:

UIWindow

取自此处:https://developers.google.com/admob/ios/banner

另外,请务必设置广告单元ID和代理:

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

    var bannerView: GADBannerView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // In this case, we instantiate the banner with desired ad size.
        bannerView = GADBannerView(adSize: kGADAdSizeBanner)

        addBannerViewToView(bannerView)
    }

    func addBannerViewToView(_ bannerView: GADBannerView) {
        bannerView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(bannerView)
        view.addConstraints(
            [NSLayoutConstraint(item: bannerView,
                                attribute: .bottom,
                                relatedBy: .equal,
                                toItem: bottomLayoutGuide,
                                attribute: .top,
                                multiplier: 1,
                                constant: 0),
             NSLayoutConstraint(item: bannerView,
                                attribute: .centerX,
                                relatedBy: .equal,
                                toItem: view,
                                attribute: .centerX,
                                multiplier: 1,
                                constant: 0)
            ])
    }

}

但是,再一次,你需要official guide中所需要的一切。