How to add admob to your app



First download cocaopods: https://cocoapods.org/app

1. Go to admob and sign up/sign in

2. Click on apps -> IOS

3. Follow instructions
4. Get your admob id

Then go to your project folder in desktop in terminal:

cd Desktop/nameOfFolder

Type: pod init

Enter


Next, open your pod file inside it,

in the target, add: pod 'Google-Mobile-Ads-SDK'

Press install


When it has been installed, go to terminal and type:

pod install --repo-update

Next, open your Xcode project and go to the AppDelegate file:
import GoogleMobileAds

In the application function add:
(func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool )

GADMobileAds.configure(withApplicationID: "YOUR_ADMOB_APP_ID")

Inside your app itself:

 var interstitial: GADInterstitial!

//in viewDidLoad:
 interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        let request = GADRequest()
        request.testDevices = [ kGADSimulatorID ];
        interstitial.load(request)

If you need the ad to reload:
 func interstitialDidDismissScreen(_ ad: GADInterstitial) {
        
        interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        let request = GADRequest()
        request.testDevices = [ kGADSimulatorID ];
        interstitial.load(request)
        
    }


When you need to show the ad:
if interstitial.isReady {
    interstitial.present(fromRootViewController: self)
  }


If you are testing your ad, instead of using your admob id, use the test id given - this is for interstitial:
ca-app-pub-3940256099942544/4411468910
When you run it the first time, you will see this in the debug screen:
request.testDevices = @[ kGADSimulatorID ];
It might use another ID

Replace the kGADSimulatorID with the ID given


Comments

Popular posts from this blog

Setting up a playground