iOS Rewarded Example

Both rewarded and rewarded interstitial ads offer users a reward for engaging with an ad.

In the example below, a rewarded ad is displayed when the menu item is clicked. This is a use-case for a rewarded ad.

The ad could also be displayed at natural breaks in the app (e.g between two screens) and automatically reward users for watching without explicit opt-in. This would be an example of a rewarded interstitial ad.

The event .reward will be sent each time a user earns a reward. You can use this event to trigger providing the user a reward in your app. For example, after watching a rewarded ad, you might give the user bonus points or unlock a new level.

final class RewardedAdManager: NSObject, ObservableObject, FuseFullScreenAdViewDelegate {
    enum Event: Equatable {
        case done
        case reward(type: String, amount: Double)
    }

    @Published var lastEvent: Event? = nil
    private let ad: FuseFullScreenAdView

    init(code: String) {
        ad = FuseFullScreenAdView(code: code)
        ad.delegate = self
    }

    func showAd() {
        ad.show()
    }

    func onEvent(adView: FuseFullScreenAdView, event: FuseAdViewEvent) {
        switch (event) {
        case .dismissed:
        case .displayTimeout:
        case .displayError: self.lastEvent = .done
        case .reward(let type, let amount): self.lastEvent = .reward(type: type, amount: amount)
        default: break
        }
    }
}

struct MyView: View {
    @StateObject private var adManager = RewardedAdManager(code: "<zone_code>")
    @State private var isAlertPresented = false
    @State private var alertMessage = ""

    var body: some View {
        NavigationStack {
            ScrollView {
                VStack {
                    // ...

                    Button("Show rewarded ad") {
                        adManager.showAd()
                    }
                    .alert(alertMessage, isPresented: $isAlertPresented) {
                        Button("OK", role: .cancel) { }
                    }

                    // ...
                }
            }
        }
        .onChange(of: adManager.lastEvent) { newEvent in
            guard let event = newEvent else { return }

            switch event {
            case .reward(let type, let amount):
                alertMessage = "Reward received: \(amount) of \(type)"
            default:
                break
            }

            isAlertPresented = true
            adManager.lastEvent = nil
        }
    }
}