Create an AdMob account https://developers.google.com/admob/android/quick-start
First, install CocoaPods if you haven’t already:
sudo gem install cocoapods
Inside the Android Studio terminal, enter the following commands. The command creates a Pods directory “iosApp/Pods/Podfile” in your iOS project.
cd iOSApp
pod init
The simplest way to import the SDK into an iOS project is to use CocoaPods. Open your project’s Pods/Podfile and add this line to your app’s target.
pod 'Google-Mobile-Ads-SDK'
Also add below to Podfile
target 'iosApp' do
use_frameworks!pod 'Google-Mobile-Ads-SDK'
end
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0'
config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'arm64'
end
end
end
Install the dependencies:
pod install --repo-update
Add the AdMob library framework to you iOS project.
Option 1: Use the cocoapods IntelliJ Plugin org.jetbrains.kotlin.native.cocoapods
[read blog post]
Option 2: Open the file iosApp.xcworkspace (right click open with Xcode). Go to iosApp -> Build Settings -> Link Binary with Libraries -> click add button ‘+’ -> search for GoogleMobileAds.xcframework.
Create the iOS-specific implementation: code
Now let’s add code the file created AdMobBanner.ios.kt
// iosMain/AdMobBanner.ios.kt
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.UIKitInteropProperties
import androidx.compose.ui.viewinterop.UIKitView@Composable
actual fun AdMobBanner(modifier: Modifier) {
UIKitView(
factory = {
generateIOSBanner().view
},
modifier = modifier
.fillMaxWidth()
.defaultMinSize(minHeight = 50.dp)
.height(50.dp),
update = { },
properties = UIKitInteropProperties(
isInteractive = true,
isNativeAccessibilityEnabled = true
)
)
}
Add the necessary Swift files:
Create a new file AppDelegate.swift and add the code below
// iosApp/AppDelegate.swift
import Foundation
import SwiftUI
import GoogleMobileAdsclass AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// initialize AdMob iOS Ads SDK
GADMobileAds.sharedInstance().start(completionHandler: nil)
return true
}
}
Create the file BannerAdView.swift and add the code below
// iosApp/BannerAdView.swift
import Foundation
import SwiftUI
import GoogleMobileAds
import UIKit
import SwiftUIstruct BannerAdView: UIViewRepresentable {
func makeUIView(context: Context) -> GADBannerView {
let bannerView = GADBannerView()
bannerView.adUnitID = "ca-app-pub-3940256099942544/2435281174" // Replace with your ad unit ID
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let rootViewController = windowScene.windows.first?.rootViewController {
bannerView.rootViewController = rootViewController
}
let request = GADRequest()
bannerView.load(request)
return bannerView
}
func updateUIView(_ uiView: GADBannerView, context: Context) {}
}
Update ContentView.swift file
// iosApp/ComposeView.swift
import UIKit
import SwiftUI
import ComposeApp
import GoogleMobileAdsstruct ComposeView: UIViewControllerRepresentable {
init() {
MainViewControllerKt.IOSBanner = {
let adBannerView = VStack {
BannerAdView()
}
return UIHostingController(rootView: adBannerView)
}
}
func makeUIViewController(context: Context) -> UIViewController {
MainViewControllerKt.MainViewController()
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}
struct ContentView: View {
var body: some View {
ComposeView()
.ignoresSafeArea(.keyboard) // Compose has own keyboard handler
}
}
Update iOSApp.swift file
// iosApp/iOSApp.swiftimport SwiftUI
@main
struct iOSApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Update the file MainViewController.kt
in your iOS main source set:
// iosMain/MainViewController.ktimport androidx.compose.ui.window.ComposeUIViewController
import platform.UIKit.UIViewController
lateinit var IOSBanner: () -> UIViewController
fun generateIOSBanner(): UIViewController {
return IOSBanner()
}
fun MainViewController() = ComposeUIViewController(
configure = {
enforceStrictPlistSanityCheck = false
}
) {
App()
}
Add the required AdMob configuration the AdMob app id and the SKAdNetworkIdentifier.
Update the file Info.plist using Xcode or Android Studio.
Xcode: Open the file iosApp.xcworkspace (right click open with Xcode) -> Info.plist.
Android Studio: Open the file Info.plist
- Add GADApplicationIdentifier key with a string value of your AdMob app ID found in the AdMob UI. Using test admob app id.
<key>GADApplicationIdentifier</key>
<string>ca-app-pub-3940256099942544~1458002511</string>
- Add SKAdNetworkItems key with SKAdNetworkIdentifier values for Google (cstr6suwn9.skadnetwork) and select third-party buyers who have provided these values to Google. Complete list of third-party buyers: skadnetwork
<array>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>cstr6suwn9.skadnetwork</string>
</dict>
<!-- Add other SKAdNetwork identifiers as needed -->
</array>