View

public extension View
  • Loads an app open ad when request becomes non-nil and presents it automatically each time the app returns to the foreground.

    After dismissal or a presentation failure the ad is released and request is reset to nil automatically. To reload, set request to a non-nil value again — for example, in an .onChange(of: scenePhase) handler. On a load failure request is also reset to nil; handle .didFailToLoad via onEvent if you want to retry immediately.

    @main
    struct MyApp: App {
        @State private var adRequest: AdRequest?
        @Environment(\.scenePhase) private var scenePhase
    
        var body: some Scene {
            WindowGroup {
                ContentView()
                    .appOpenAd(request: $adRequest)
            }
            .onChange(of: scenePhase) {
                if scenePhase == .active, adRequest == nil {
                    adRequest = AdRequest(adUnitID: "R-M-XXXXXX-Y")
                }
            }
        }
    }
    

    Declaration

    Swift

    @MainActor
    func appOpenAd(
        request: Binding<AdRequest?>,
        onEvent: ((AppOpenAdEvent) -> Void)? = nil
    ) -> some View

    Parameters

    request

    A binding to an optional ad request. Setting it to a non-nil value triggers loading. Setting it to nil cancels any in-progress load.

    onEvent

    An optional closure called with lifecycle events.

  • Loads an interstitial ad when request becomes non-nil and presents it when isPresented becomes true.

    @State private var request: AdRequest? = nil
    @State private var isPresented = false
    
    Button("Load") { request = AdRequest(adUnitID: "R-M-XXXXXX-Y") }
    Button("Show") { isPresented = true }
        .interstitialAd(isPresented: $isPresented, request: $request) { event in
            if case .didLoad = event { print("ready") }
        }
    

    Declaration

    Swift

    @MainActor
    func interstitialAd(
        isPresented: Binding<Bool>,
        request: Binding<AdRequest?>,
        onEvent: ((InterstitialAdEvent) -> Void)? = nil
    ) -> some View

    Parameters

    isPresented

    A binding that controls presentation. Set to true to show the loaded ad. Reset to false automatically after dismissal or on failure.

    request

    A binding to an optional ad request. Setting it to a non-nil value triggers loading. Setting it to nil cancels any in-progress load.

    onEvent

    An optional closure called with lifecycle events.

  • Loads a rewarded ad when request becomes non-nil and presents it when isPresented becomes true.

    @State private var request: AdRequest? = nil
    @State private var isPresented = false
    
    Button("Load") { request = AdRequest(adUnitID: "R-M-XXXXXX-Y") }
    Button("Show") { isPresented = true }
        .rewardedAd(isPresented: $isPresented, request: $request) { event in
            if case .didReward(let reward) = event { print("reward: \(reward.type)") }
        }
    

    Declaration

    Swift

    @MainActor
    func rewardedAd(
        isPresented: Binding<Bool>,
        request: Binding<AdRequest?>,
        onEvent: ((RewardedAdEvent) -> Void)? = nil
    ) -> some View

    Parameters

    isPresented

    A binding that controls presentation. Set to true to show the loaded ad. Reset to false automatically after dismissal or on failure.

    request

    A binding to an optional ad request. Setting it to a non-nil value triggers loading. Setting it to nil cancels any in-progress load.

    onEvent

    An optional closure called with lifecycle events.