please note: i am not professional code programing :)
Is there any error in the AdMob code, or any violation of AddMob ads, or any error in its combination?
//
// ViewController.swift
// المعلم الالكتروني الشامل
//
// Created by Majeed Raza on 12/11/20.
//
import UIKit
import WebKit
import GoogleMobileAds
class ViewController: UIViewController {
@IBOutlet weak var backButton: UIButton!
@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var bannerContainerView: UIView!
@IBOutlet weak var bannerHeightConstraints: NSLayoutConstraint!
var bannerView: GADBannerView!
var interstitial: GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
createAndLoadInterstitial()
webView.navigationDelegate = self
= GADAdSizeFromCGSize(CGSize(width: 300, height: 50))
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
addBannerViewToView(bannerView)
bannerView.adUnitID = "ca-app-pub-8177765238464378/5568600849"
bannerView.rootViewController = self
bannerView.load(GADRequest())
bannerView.delegate = self
addBannerViewToView(bannerView)
// Do any additional setup after loading the view.
// webView.load(URLRequest.init(url: URL.init(string:
//let url = Bundle.main.url(forResource: "index", withExtension: "html")
// webView.load(URLRequest.init(url: url!))
do {
guard let filePath = Bundle.main.path(forResource: "index", ofType: "html")
else {
print ("File reading error")
return
}
let baseUrl = URL(fileURLWithPath: filePath)
webView.loadFileURL(baseUrl, allowingReadAccessTo: baseUrl)
} catch {
print ("File HTML error")
}
}
func addBannerViewToView( bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
bannerContainerView.addSubview(bannerView)
// bannerContainerView.frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50)
}
@IBAction func backButtonTapped( sender: Any) {
webView.goBack()
}
func createAndLoadInterstitial() {
interstitial = GADInterstitial(adUnitID: "ca-app-pub-8177765238464378/9061478519")
interstitial.delegate = self
interstitial.load(GADRequest())
}
}
extension ViewController: GADBannerViewDelegate {
func adViewDidReceiveAd( bannerView: GADBannerView) {
print("adViewDidReceiveAd")
bannerView.alpha = 0
bannerHeightConstraints.constant = 50
UIView.animate(withDuration: 0.1) {
self.view.layoutIfNeeded()
self.bannerView.alpha = 1
}
}
/// Tells the delegate an ad request failed.
func adView( bannerView: GADBannerView,
didFailToReceiveAdWithError error: GADRequestError) {
print("adView:didFailToReceiveAdWithError: \(error)")
bannerHeightConstraints.constant = 0
self.bannerView.alpha = 1
UIView.animate(withDuration: 0.1) {
self.view.layoutIfNeeded()
self.bannerView.alpha = 0
}
}
/// Tells the delegate that a full-screen view will be presented in response
/// to the user clicking on an ad.
func adViewWillPresentScreen( bannerView: GADBannerView) {
print("adViewWillPresentScreen")
}
/// Tells the delegate that the full-screen view will be dismissed.
func adViewWillDismissScreen( bannerView: GADBannerView) {
print("adViewWillDismissScreen")
}
/// Tells the delegate that the full-screen view has been dismissed.
func adViewDidDismissScreen( bannerView: GADBannerView) {
print("adViewDidDismissScreen")
}
/// Tells the delegate that a user click will open another app (such as
/// the App Store), backgrounding the current app.
func adViewWillLeaveApplication( bannerView: GADBannerView) {
print("adViewWillLeaveApplication")
}
}
extension ViewController: GADInterstitialDelegate {
func interstitialDidReceiveAd( ad: GADInterstitial) {
print("interstitialDidReceiveAd")
}
/// Tells the delegate an ad request failed.
func interstitial( ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) {
print("interstitial:didFailToReceiveAdWithError: \(error.localizedDescription)")
}
/// Tells the delegate that an interstitial will be presented.
func interstitialWillPresentScreen( ad: GADInterstitial) {
print("interstitialWillPresentScreen")
}
/// Tells the delegate the interstitial is to be animated off the screen.
func interstitialWillDismissScreen( ad: GADInterstitial) {
print("interstitialWillDismissScreen")
self.createAndLoadInterstitial()
}
/// Tells the delegate the interstitial had been animated off the screen.
func interstitialDidDismissScreen( ad: GADInterstitial) {
print("interstitialDidDismissScreen")
}
/// Tells the delegate that a user click will open another app
/// (such as the App Store), backgrounding the current app.
func interstitialWillLeaveApplication( ad: GADInterstitial) {
print("interstitialWillLeaveApplication")
self.createAndLoadInterstitial()
}
}
extension ViewController: WKNavigationDelegate {
func webView( webView: WKWebView, didFinish navigation: WKNavigation!) {
if (webView.url?.absoluteString.contains("f1"))! || (webView.url?.absoluteString.contains("f2"))! {
if self.interstitial.isReady {
self.interstitial.present(fromRootViewController: self)
}
}
}
}
Is there any error in the AdMob code, or any violation of AddMob ads, or any error in its combination?
//
// ViewController.swift
// المعلم الالكتروني الشامل
//
// Created by Majeed Raza on 12/11/20.
//
import UIKit
import WebKit
import GoogleMobileAds
class ViewController: UIViewController {
@IBOutlet weak var backButton: UIButton!
@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var bannerContainerView: UIView!
@IBOutlet weak var bannerHeightConstraints: NSLayoutConstraint!
var bannerView: GADBannerView!
var interstitial: GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
createAndLoadInterstitial()
webView.navigationDelegate = self
= GADAdSizeFromCGSize(CGSize(width: 300, height: 50))
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
addBannerViewToView(bannerView)
bannerView.adUnitID = "ca-app-pub-8177765238464378/5568600849"
bannerView.rootViewController = self
bannerView.load(GADRequest())
bannerView.delegate = self
addBannerViewToView(bannerView)
// Do any additional setup after loading the view.
// webView.load(URLRequest.init(url: URL.init(string:
//let url = Bundle.main.url(forResource: "index", withExtension: "html")
// webView.load(URLRequest.init(url: url!))
do {
guard let filePath = Bundle.main.path(forResource: "index", ofType: "html")
else {
print ("File reading error")
return
}
let baseUrl = URL(fileURLWithPath: filePath)
webView.loadFileURL(baseUrl, allowingReadAccessTo: baseUrl)
} catch {
print ("File HTML error")
}
}
func addBannerViewToView( bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
bannerContainerView.addSubview(bannerView)
// bannerContainerView.frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50)
}
@IBAction func backButtonTapped( sender: Any) {
webView.goBack()
}
func createAndLoadInterstitial() {
interstitial = GADInterstitial(adUnitID: "ca-app-pub-8177765238464378/9061478519")
interstitial.delegate = self
interstitial.load(GADRequest())
}
}
extension ViewController: GADBannerViewDelegate {
func adViewDidReceiveAd( bannerView: GADBannerView) {
print("adViewDidReceiveAd")
bannerView.alpha = 0
bannerHeightConstraints.constant = 50
UIView.animate(withDuration: 0.1) {
self.view.layoutIfNeeded()
self.bannerView.alpha = 1
}
}
/// Tells the delegate an ad request failed.
func adView( bannerView: GADBannerView,
didFailToReceiveAdWithError error: GADRequestError) {
print("adView:didFailToReceiveAdWithError: \(error)")
bannerHeightConstraints.constant = 0
self.bannerView.alpha = 1
UIView.animate(withDuration: 0.1) {
self.view.layoutIfNeeded()
self.bannerView.alpha = 0
}
}
/// Tells the delegate that a full-screen view will be presented in response
/// to the user clicking on an ad.
func adViewWillPresentScreen( bannerView: GADBannerView) {
print("adViewWillPresentScreen")
}
/// Tells the delegate that the full-screen view will be dismissed.
func adViewWillDismissScreen( bannerView: GADBannerView) {
print("adViewWillDismissScreen")
}
/// Tells the delegate that the full-screen view has been dismissed.
func adViewDidDismissScreen( bannerView: GADBannerView) {
print("adViewDidDismissScreen")
}
/// Tells the delegate that a user click will open another app (such as
/// the App Store), backgrounding the current app.
func adViewWillLeaveApplication( bannerView: GADBannerView) {
print("adViewWillLeaveApplication")
}
}
extension ViewController: GADInterstitialDelegate {
func interstitialDidReceiveAd( ad: GADInterstitial) {
print("interstitialDidReceiveAd")
}
/// Tells the delegate an ad request failed.
func interstitial( ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) {
print("interstitial:didFailToReceiveAdWithError: \(error.localizedDescription)")
}
/// Tells the delegate that an interstitial will be presented.
func interstitialWillPresentScreen( ad: GADInterstitial) {
print("interstitialWillPresentScreen")
}
/// Tells the delegate the interstitial is to be animated off the screen.
func interstitialWillDismissScreen( ad: GADInterstitial) {
print("interstitialWillDismissScreen")
self.createAndLoadInterstitial()
}
/// Tells the delegate the interstitial had been animated off the screen.
func interstitialDidDismissScreen( ad: GADInterstitial) {
print("interstitialDidDismissScreen")
}
/// Tells the delegate that a user click will open another app
/// (such as the App Store), backgrounding the current app.
func interstitialWillLeaveApplication( ad: GADInterstitial) {
print("interstitialWillLeaveApplication")
self.createAndLoadInterstitial()
}
}
extension ViewController: WKNavigationDelegate {
func webView( webView: WKWebView, didFinish navigation: WKNavigation!) {
if (webView.url?.absoluteString.contains("f1"))! || (webView.url?.absoluteString.contains("f2"))! {
if self.interstitial.isReady {
self.interstitial.present(fromRootViewController: self)
}
}
}
}