Phone apps and games: iOS, Android and Unity
A web view, one small page and two messages.
A phone app or a game shows a Swoop unit in a web view: a small browser window inside your app. Your app loads one small page, and the page draws the unit and tells your app how tall it is.
What you need#
- Make a Spot for your app in the app. Choose iPhone app or Android app, or Game for Unity. Copy the public key (pk_…).
- Pick an address for the page. Your app loads the page with a base address, for example https://app.example.com. For a web Spot that address must be on the Spot’s list; app Spots may use any https address.
- Fill the four values in the page, load it in the web view, and listen for two messages.
The page#
This is unit.html from swoop-webview. The app replaces {{SPOT}}, {{UNIT}}, {{THEME}} and {{SLOT}} (leave the slot empty for the default).
<div id="swoop"></div>
<script>
var s = document.createElement('script')
s.src = 'https://swoopads.xyz/t.js'
s.async = true
s.setAttribute('data-spot', '{{SPOT}}')
s.setAttribute('data-unit', '{{UNIT}}') // banner | text | native
s.setAttribute('data-theme', '{{THEME}}') // light | dark | auto
s.setAttribute('data-target', '#swoop')
document.head.appendChild(s)
// ...then forward the unit's messages to the app (the full file does this for iOS, Android and Unity)
</script>The full file, with the bridge for each platform, is in the Gully Labs SDK repository under packages/swoop-webview.
Messages to your app#
| Message | Meaning | What to do |
|---|---|---|
| height with h | The unit is this many points tall (and w wide): its exact size. 0 means there is nothing to show. | Set the web view to exactly that size while it is off screen, or hide it when 0. Never stretch it. |
| state filled | A sponsor card is showing. | Show the view. |
| state viewed | Half of it was on screen for a second. One view counted. | Nothing. For your own stats only. |
| state empty | Nothing to show, or the player hid it, or the page could not load. | Collapse the row. |
On each platform#
iOS (Swift)
let ad = SwoopAdView(spot: "pk_YOUR_SPOT_KEY", unit: "banner",
baseURL: URL(string: "https://app.example.com")!)
ad.onHeight = { h in self.adHeight.constant = h } // 0: collapse the row
ad.onState = { state, reason in print(state, reason ?? "") }
view.addSubview(ad); ad.load()
// SwoopAdView wraps WKWebView with a non-persistent data store and a
// WKScriptMessageHandler named "swoop". UNTESTED: not compiled here.Android (Kotlin)
val ad = SwoopAdView(context, spot = "pk_YOUR_SPOT_KEY", unit = "banner",
baseUrl = "https://app.example.com")
ad.onHeight = { px -> /* set the view height; 0 collapses it */ }
ad.onState = { state, reason -> Log.d("swoop", "$state $reason") }
container.addView(ad); ad.load()
// SwoopAdView is a WebView with JavaScript on, storage off and a
// JavascriptInterface named "SwoopBridge". UNTESTED: not compiled here.Unity (C#)
// Needs a web view plugin; this uses the open-source gree/unity-webview (MIT).
// Put SwoopAdView on a GameObject, drag unit.html into "template", fill the fields.
var ad = GetComponent<SwoopAdView>();
ad.spot = "pk_YOUR_SPOT_KEY";
ad.unit = "banner";
ad.Show(); // when your menu opens
ad.Hide(); // when it closes
// UNTESTED: not compiled here (no Unity on the build machines).Good practice for apps#
- Never over a button that moves money. Do not put a unit on a checkout, send or claim screen; it would stay empty anyway.
- Keep the row collapsible. An empty unit has height 0. Do not reserve space for it.
- No storage. The samples use a non-persistent web view, so nothing is kept between launches.
- App store rules. Say in your store listing that the app shows ads. Ads in apps for children need extra care in most countries; ask counsel before you turn them on.
- For rewarded offers (play, watch, swap), see the integrate guide.