View
public extension View
-
Loads an app open ad when
requestbecomes non-niland presents it automatically each time the app returns to the foreground.After dismissal or a presentation failure the ad is released and
requestis reset tonilautomatically. To reload, setrequestto a non-nilvalue again — for example, in an.onChange(of: scenePhase)handler. On a load failurerequestis also reset tonil; handle.didFailToLoadviaonEventif 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 ViewParameters
requestA binding to an optional ad request. Setting it to a non-
nilvalue triggers loading. Setting it tonilcancels any in-progress load.onEventAn optional closure called with lifecycle events.
-
Loads an interstitial ad when
requestbecomes non-niland presents it whenisPresentedbecomestrue.@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 ViewParameters
isPresentedA binding that controls presentation. Set to
trueto show the loaded ad. Reset tofalseautomatically after dismissal or on failure.requestA binding to an optional ad request. Setting it to a non-
nilvalue triggers loading. Setting it tonilcancels any in-progress load.onEventAn optional closure called with lifecycle events.
-
Loads a rewarded ad when
requestbecomes non-niland presents it whenisPresentedbecomestrue.@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 ViewParameters
isPresentedA binding that controls presentation. Set to
trueto show the loaded ad. Reset tofalseautomatically after dismissal or on failure.requestA binding to an optional ad request. Setting it to a non-
nilvalue triggers loading. Setting it tonilcancels any in-progress load.onEventAn optional closure called with lifecycle events.