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
platform :ios, '13.0' target '<Your target>' do use_frameworks! <Other Pods> pod 'mopub-ios-sdk' end
Initialize:
Add the following lines to your AppDelegate
file under didFinishLaunchingWithOptions
function.
import MoPub ..... func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { ...... ...... let sdkConfig = MPMoPubConfiguration(adUnitIdForAppInitialization: "<Ad Unit ID>") sdkConfig.loggingLevel = .info MoPub.sharedInstance().initializeSdk(with: sdkConfig, completion: nil) return true }
Add a Banner Ad:
Open your ViewController
and follow the steps below:
- Import MoPub SDB
- Declare a variable
import MoPub .... var moPubBannerView: MPAdView?
- Add the following code inside
viewDidLoad
or you can add a private function and call it fromviewDidLoad
self.moPubBannerView = MPAdView.init(adUnitId: "<Ad Unit ID>") let adSize = UIDevice.current.userInterfaceIdiom == .pad ? kMPPresetMaxAdSize90Height : kMPPresetMaxAdSize50Height let tabBarHeight = tabBarController.tabBar.frame.size.height let bounds = view.bounds var adFrame = CGRect.zero adFrame.size = UIDevice.current.userInterfaceIdiom == .pad ? CGSize(width: 728, height: 90) : CGSize(width: 320, height: 50) adFrame.origin.x = (bounds.size.width-adFrame.size.width)/2 adFrame.origin.y = bounds.size.height - adFrame.size.height - tabBarHeight self.moPubBannerView?.frame = adFrame self.moPubBannerView?.maxAdSize = adSize self.moPubBannerView?.delegate = self self.view.addSubview(self.moPubBannerView!) self.moPubBannerView?.loadAd()
- Add an extension for listening to ad events
extension ViewController: MPAdViewDelegate { func adViewDidLoadAd(_ view: MPAdView!, adSize: CGSize) { print("MoPubAd loaded with width: " + adSize.width.description) self.moPubBannerView?.startAutomaticallyRefreshingContents() } func adView(_ view: MPAdView!, didFailToLoadAdWithError error: Error!) { print("MoPubAd load failed: " + error.debugDescription) } func viewControllerForPresentingModalView() -> UIViewController? { return self } }
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.
Is there any method for doing this using storyboard?
Unfortunately I haven’t found a way to do this using storyboard yet.
Thanks for your reply. I worked on it an entire afternoon and couldn’t get it done either. In the end, I just had to rework the code. Thanks for your guidance.
You are most welcome. I will try to find out a way to do it using storyboard and if I find one, will update the article.
hello sir, i’m getting some errors after installing the pod (pod ‘mopub-ios-sdk’), as :
1. can’t find SKAdImpression in the scope
2. can’t find SKAdImpression in scope
i’m using xcode Version 12.2 (12B45b)