AdMobManager
A package to help support the implementation of ads on your iOS app.
- For Swift 5.3, Xcode 12.0 (macOS Big Sur) or later.
- Support for apps from iOS 10.0 and up.
Ad Type
- SplashAd
- InterstitialAd
- AppOpenAd
- NativeAd
- BannerAd
Installation
Swift Package Manager
The Swift Package Manager is a tool for managing the distribution of Swift code. To use AdMobManager with Swift Package Manger, add it to dependencies in your Package.swift
dependencies: [
.package(url: "https://github.com/trinhxuanminh/AdMobManager.git")
]
Get started
Initial setup as documented by Google AdMob:
Set the -ObjC linker flag at Info.plist
:
<key>GADIsAdManagerApp</key>
<true/>
Usage
Firstly, import AdMobManager
.
import AdMobManager
1. Parameter setting
Advertising ID
This function helps to change the ad ID, available for the next load.
- Ad parameters is nil will not load the corresponding ad type.
AdMobManager.shared.set_AdUnit(splashAd_ID: String?, interstitialAd_ID: String?, appOpenAd_ID: String?, nativeAd_ID: String?, bannerAd_ID: String?)
Deployment Time (Optional)
This function helps to set the date to start showing ads.
- Default is nil, the ad will be displayed as soon as it is ready.
- Changes only for
SplashAd
,InterstitialAd
,AppOpenAd
.
AdMobManager.shared.set_Time_Show_Full_Feature(start: Date)
Time between (Optional)
This function helps to change the minimum display time between ads of the same type.
- Default is 5 seconds.
- Changes only for
InterstitialAd
,AppOpenAd
.
e.g.
AdMobManager.shared.set_Time_Between(adType: AdMobManager.AdType, time: Double)
2. Control
is_Ready()
This function returns a value true/false indicating if the ad is ready to be displayed.
e.g.
AdMobManager.shared.is_Ready(adType: AdMobManager.AdType)
show()
This function will display ads when ready.
Parameters:
- willPresent: The block executes after the ad is about to show.
- willDismiss: The block executes after the ad is about to disappear.
- didDismiss: The block executes after the ad has disappeared.
e.g.
AdMobManager.shared.show(adType: AdMobManager.AdType)
3. NativeAd
Ads are displayed automatically.
a) NativeAdCollectionViewCell / NativeAdvancedAdCollectionViewCell
This class returns a UICollectionViewCell displaying NativeAd.
Register
collectionView.register(UINib(nibName: NativeAdCollectionViewCell.className, bundle: AdMobManager.bundle), forCellWithReuseIdentifier: NativeAdCollectionViewCell.className)
collectionView.register(UINib(nibName: NativeAdvancedAdCollectionViewCell.className, bundle: AdMobManager.bundle), forCellWithReuseIdentifier: NativeAdvancedAdCollectionViewCell.className)
Datasource
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let collectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: NativeAdCollectionViewCell.className, for: indexPath) as! NativeAdCollectionViewCell
// Optional
return collectionViewCell
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let collectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: NativeAdvancedAdCollectionViewCell.className, for: indexPath) as! NativeAdvancedAdCollectionViewCell
// Optional
return collectionViewCell
}
b) NativeAdTableViewCell / NativeAdvancedAdTableViewCell
This class returns a UITableViewCell displaying NativeAd.
Register
tableView.register(UINib(nibName: NativeAdTableViewCell.className, bundle: AdMobManager.bundle), forCellReuseIdentifier: NativeAdTableViewCell.className)
tableView.register(UINib(nibName: NativeAdvancedAdTableViewCell.className, bundle: AdMobManager.bundle), forCellReuseIdentifier: NativeAdvancedAdTableViewCell.className)
Datasource
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tableViewCell = tableView.dequeueReusableCell(withIdentifier: NativeAdTableViewCell.className, for: indexPath) as! NativeAdTableViewCell
// Optional
return tableViewCell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tableViewCell = tableView.dequeueReusableCell(withIdentifier: NativeAdvancedAdTableViewCell.className, for: indexPath) as! NativeAdvancedAdTableViewCell
// Optional
return tableViewCell
}
c) NativeAdView / NativeAdvancedAdView
Then, there are two ways you can create NativeAdView
/ NativeAdvancedAdView
:
- By storyboard, changing class of any
UIView
toNativeAdView
/NativeAdvancedAdView
. Note: SetModule
toAdMobManager
. - By code, using initializer.
d) Optional
.set_Color(style: AdMobManager.Style?, backgroundColor: UIColor?, themeColor: UIColor?)
.set_Loading_Type(type: NVActivityIndicatorType)
.setAd(index: Int)
4. BannerAd
Ads are displayed automatically.
Then, there are two ways you can create BannerAdView
:
- By storyboard, changing class of any
UIView
toBannerAdView
. Note: SetModule
toAdMobManager
. - By code, using initializer.
5. Error handling options
Limit reloading
This function helps to limit the reload of the ad when an error occurs.
- Time reload ads after failed load.
- Unit is milliseconds.
- Default is nil, ad will be reloaded immediately.
AdMobManager.shared.limit_Reloading_Of_Ads_When_There_Is_An_Error(adReloadTime: Int)
Stop loading SplashAd
This function helps to block reloading of SplashAd.
- Recommended when splash ads don’t need to appear anymore.
AdMobManager.shared.stop_Loading_SplashAd()