Ezoic in a WebView app
This page is for iOS apps that show your Ezoic website inside the app, in a WKWebView (UIKit) or a SwiftUI view that wraps one. If your app shows Ezoic ads through an Ezoic SDK instead, see Apps using the Ezoic SDK.
Why the app must tell the page
Apple's App Tracking Transparency (ATT) rules cover web content your app shows, not only native code. Under App Store Review Guideline 5.1.1(iv), an app must not track a user who chose Ask App Not to Track, and Apple counts cookies that your own web content sets for advertising as tracking. Apple has rejected apps because the Ezoic consent banner appeared in the app's web view after the user had denied ATT.
A web page can't read the app's ATT status, so the app tells it. When the page knows that the user denied tracking, the Ezoic CMP rejects consent by itself and shows no banner.
Set the flag
Add this script to the web view whenever the ATT status is .denied or .restricted:
window.ezAttDenied = true;
- When:
ATTrackingManager.trackingAuthorizationStatusis.deniedor.restricted. Don't set it for.notDeterminedor.authorized. - Where: in every page load, before any of the page's scripts run. Add it as a
WKUserScriptwithinjectionTime: .atDocumentStartandforMainFrameOnly: false, as below. - Value: only the boolean
truecounts."true",1or any other value is ignored.
UIKit
import AppTrackingTransparency
import WebKit
let config = WKWebViewConfiguration()
if #available(iOS 14, *) {
let status = ATTrackingManager.trackingAuthorizationStatus
if status == .denied || status == .restricted {
config.userContentController.addUserScript(WKUserScript(
source: "window.ezAttDenied = true;",
injectionTime: .atDocumentStart,
forMainFrameOnly: false))
}
}
let webView = WKWebView(frame: .zero, configuration: config)
A user script is part of the web view's configuration, so it is fixed once the web view exists. Ask for ATT before you create the web view, and create a new web view when the status changes. The user can change it at any time in Settings > Privacy & Security > Tracking, so check it each time the app becomes active:
import AppTrackingTransparency
import UIKit
import WebKit
final class SiteViewController: UIViewController {
private let siteURL = URL(string: "https://example.com")!
private var webView: WKWebView?
private var webViewIsForDeniedATT: Bool?
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(
self, selector: #selector(refresh),
name: UIApplication.didBecomeActiveNotification, object: nil)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
refresh()
}
// Ask for ATT first (the prompt shows only while the app is active), then show the site.
@objc private func refresh() {
if #available(iOS 14, *),
ATTrackingManager.trackingAuthorizationStatus == .notDetermined {
ATTrackingManager.requestTrackingAuthorization { _ in
DispatchQueue.main.async { self.showSite() }
}
} else {
showSite()
}
}
private func showSite() {
let denied = isATTDenied()
guard denied != webViewIsForDeniedATT else { return } // nothing changed
webView?.removeFromSuperview()
let config = WKWebViewConfiguration()
if denied {
config.userContentController.addUserScript(WKUserScript(
source: "window.ezAttDenied = true;",
injectionTime: .atDocumentStart,
forMainFrameOnly: false))
}
let newWebView = WKWebView(frame: view.bounds, configuration: config)
newWebView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(newWebView)
newWebView.load(URLRequest(url: webView?.url ?? siteURL))
webView = newWebView
webViewIsForDeniedATT = denied
}
}
func isATTDenied() -> Bool {
guard #available(iOS 14, *) else { return false }
let status = ATTrackingManager.trackingAuthorizationStatus
return status == .denied || status == .restricted
}
If you'd rather keep one web view, you can instead call removeAllUserScripts() on its userContentController, add the script again if ATT is denied, and reload(). The new setting applies from the next page load.
SwiftUI
Wrap the web view in a UIViewRepresentable and give it the ATT status as its identity, so SwiftUI creates a new web view when the status changes:
import AppTrackingTransparency
import SwiftUI
import WebKit
struct SiteWebView: UIViewRepresentable {
let url: URL
let attDenied: Bool
func makeUIView(context: Context) -> WKWebView {
let config = WKWebViewConfiguration()
if attDenied {
config.userContentController.addUserScript(WKUserScript(
source: "window.ezAttDenied = true;",
injectionTime: .atDocumentStart,
forMainFrameOnly: false))
}
let webView = WKWebView(frame: .zero, configuration: config)
webView.load(URLRequest(url: url))
return webView
}
func updateUIView(_ webView: WKWebView, context: Context) {}
}
struct ContentView: View {
@Environment(\.scenePhase) private var scenePhase
@State private var attAsked = false
@State private var attDenied = isATTDenied() // the helper from the UIKit example
var body: some View {
Group {
if attAsked {
SiteWebView(url: URL(string: "https://example.com")!, attDenied: attDenied)
.id(attDenied) // a new web view when the ATT status changes
} else {
ProgressView()
}
}
.task { await refreshATT() }
.onChange(of: scenePhase) { phase in
if phase == .active { Task { await refreshATT() } }
}
}
// Ask for ATT first (the prompt shows only while the app is active), then show the site.
private func refreshATT() async {
if #available(iOS 14, *),
ATTrackingManager.trackingAuthorizationStatus == .notDetermined {
_ = await ATTrackingManager.requestTrackingAuthorization()
}
attDenied = isATTDenied()
attAsked = true
}
}
What the Ezoic CMP does with the flag
While window.ezAttDenied is true:
- GDPR regions: the CMP rejects consent, as if the visitor had chosen Reject all, and shows no banner, notice or settings dialog. It stores and signals the same thing Reject all does: a TC string with no consents, the Google Additional Consent string with no consents, and Google Consent Mode set to denied. No consent log is sent, because the visitor didn't make a choice.
- Covered US states: the CMP stores the state's opted-out choice, as after Do Not Sell My Information, and shows no banner. The visitor stays opted out while the flag is set, even if they open the opt-out dialog from your
ez-do-not-selllink and choose otherwise. - Earlier choices: a choice the visitor made before, even Accept all, is replaced by the rejection while the flag is set.
- Everywhere else: nothing changes.
When the flag is gone, for example because the user allowed tracking in Settings, the CMP forgets the automatic rejection and asks again, as for a new visitor. A choice the visitor made themselves is kept.
Android
Android has no App Tracking Transparency. An Android app that shows your site in a WebView needs no change: don't set the flag.
Apps using the Ezoic SDK
From iOS SDK 1.27.0, the Ezoic CMP inside the SDK does the same by itself: if the user denied App Tracking Transparency, it rejects consent automatically and shows no dialog, and it asks again if the user later allows tracking. The wrappers (Flutter, React Native, Capacitor and Unity) get this from 1.27.0. There is nothing to call. See Consent management (CMP).
The SDK doesn't add anything to your app's own web views. If your app also shows your website in a WKWebView, set the flag there as described above.
Troubleshooting
The banner still appears after ATT was denied.
- The flag has to be set before Ezoic's scripts run. Use
injectionTime: .atDocumentStart. A script added at.atDocumentEnd, or a value set withevaluateJavaScriptafter the page has loaded, comes too late. - Use
forMainFrameOnly: false, so the flag is also set in frames. - Add the user script before creating the web view (or reload after adding it). Scripts added after a page has loaded apply from the next load.
- Check the value is the boolean
true, not the string"true". - Check that the app asked for ATT before it built the web view. A web view created while the status was
.notDeterminedhas no flag.
Check it in Safari. Make the web view inspectable (set isInspectable = true on iOS 16.4 and later), then on your Mac open Safari > Develop > your device > your app's page, and in the console run:
window.ezAttDenied // true
__tcfapi('getTCData', 2, console.log) // GDPR regions: no purpose or vendor consents