How to add Admob Firebase in Swift Xcode project

Today we will learn how to add Admob in Swift project. Admob is mobile monetization provider by Google.

1. First we need to create a new project in Xcode.  First you need to sign up as admob publisher. Then click the Monetise tab and click the Monetise New App Button.
NewImage

2. Then click add your app manually, you need to enter the app name, select platform as iOS and click add App.
NewImage
3. Then we will add a new banner and give the banner a new name.
NewImage
4. It is a good idea to link a new firebase Analytics tool to help you learn more about your user.
NewImage
5. You must remember this iOS bundle that you enter and match it with your Swift Xcode project later.
NewImage
6. Next you need to download the Google Mobile Ads SDK. AdMob is now part of Firebase.
The Mobile Ads SDK for iOS utilizes Apple's advertising identifier (IDFA). The SDK uses IDFA under the guidelines laid out in the iOS developer program license agreement. You must ensure you are in compliance with the iOS developer program license agreement policies governing the use of this identifier.NewImage

7.You also need to download the GoogleService-info.plist in step 3 so that you can use Admob properly with Xcode.

NewImage
NewImage
8. For our tutorial, Click Single View Application. Its also work with Tabbed Applciation and Game project.
NewImage

9. Setup a product name and organisation indentifier according to your need. Set language as swift and device as universal. Select Use Core Data, Include Unit Test and Include UI Tests. Click next to continue.
NewImage

10. Make sure you remember the Bundle Indentifer as it must match the App id in the  http://apps.admob.com

NewImage
11. Drag and Drop the GoogleMobileAds.framework into your Xocode project. If you have not download the SDK yet, follow step 6.
 NewImage
12. Select the checkbox the copy items if needed, create folder references and Add to targets or the Admob will not works. Refer to the image above.

NewImage
13. Drag and Drop the GoogleService-info.plist that you have downloaded in step 7 into Xcode Project
NewImage

14. Make sure the GoogleService-info.plist is named without any number append to it just like picture above.
NewImage
15. Select the Main.storyboard file and drag and drop the UIView component into it.

NewImage

16. In the custom Class, we set the class name as GADBannerView. This GADBannerView will display our admob ads.

NewImage

17. In the inspector size view, set the width as 320 and height 50. This is a standard admob banner size.
NewImage
18. Drag UIView to the bottom of the screen and left click and drag until the menu pop up like picture above select width.
NewImage
19. Left click and drag down on UIview and select height when the pop up menu appear.
NewImage
20. Right click on the UIview and drag and drop it into the View component. Make sure you select the Vertical Spacing to Buttom Layout Guide and Centre Horizontally in Container.

NewImage

21. Select the view Controller and click the NewImage icon to switch to show the editor.

NewImage

22. Next add import GoogleMobileAds under the import UIKit
NewImage
23. Rename the view to BannerView

NewImage
24. Click click on the BannerView and right click and drop it into edtior until the pop out (refer to image above)
NewImage
25. Name it to any name you want (in our tutorial we will choose BannerView) and click connect button.     @IBOutlet var BannerView: GADBannerView! code should appear in the code editor below the class ViewController.
NewImage
26. Add GADBannerViewDelegate superclass in the ViewController class.
NewImage


27. Add the following code
          let request = GADRequest()
            request.testDevices=[kGADSimulatorID]
            BannerView.delegate=self
            BannerView.adUnitID="ca-app-pub-YOURID/APPID"
            BannerView.rootViewController=self
            BannerView.loadRequest(request)
    }


NewImage
28. Now you can test and run the code in the simulator.

Download the project at github https://github.com/hafizmus/admobfirebaseswift
How to add Admob Firebase in Swift Xcode project How to add Admob Firebase in Swift Xcode project Reviewed by Admin on 6:56 AM Rating: 5

No comments:

Powered by Blogger.