Integrate MoPub using Swift

Recently I moved from AdMob to MoPub because I wanted to use a mediation and try out Facebook Audience Network and AdMob rather than stick to just AdMob. Also, MoPub is very good in terms of revenue generation.

Implementing on my Android app was a breeze. It was so easy and simple and the documentation was clear enough. But when it came to implementing in iOS using Swift, their documentation, without a lack of better word, sucks big time. It’s written in Obj C and although the sample app is written in Swift, it is very difficult to follow. It took me lots of searching to find one article which helped me, but that too was bit outdated. So, naturally I thought it would be better to put these in an article for someone else to use.

Things you need:

  • An active, approved MoPub account with running ad units
  • An iOS app, written in Swift

Integrate:

I used CocoaPods to integrate MoPub SDK into my app. For that, I added the following to my pod file and ran pod install

Initialize:

Add the following lines to your AppDelegate file under didFinishLaunchingWithOptions function.

Add a Banner Ad:

Open your ViewController and follow the steps below:

  • Import MoPub SDB
  • Declare a variable
  • Add the following code inside viewDidLoad or you can add a private function and call it from viewDidLoad
  • Add an extension for listening to ad events

And that’s it, you are done implementing MoPub into your app. You just need to build, run and publish it to see your ads in the app.

Leave a Reply

Scroll to Top