# Implement Gamedistribution ads using webview

**URL:** <https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958>\
**Category:** Questions\
**Created:** [September 16, 2016, 5:35pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958 "2016-09-16T17:35:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![qkygames](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/q/6de8d8/32.png) [@qkygames](https://forum.defold.com/u/qkygames)\
**Post date:** [September 16, 2016, 5:35pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/1 "2016-09-16T17:35:35Z")

</div>

Hi, I am Maxi from QkyGames Studio. We are just starting using defold and we are already making some games, but we need to use this API to show ads in our games, please check it here: [http://www.gamedistribution.com/Api/1](http://www.gamedistribution.com/Api/1)

Is it possible use this API to show ads in html5 games? For mobile games we use Appnext and for that I’ve already found a library here in the forums.

Sorry for my english.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [September 16, 2016, 6:19pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/2 "2016-09-16T18:19:17Z")

</div>

Are these for banner style ads?

Webview for banners isn’t available yet but it’s planned. You can only use fullscreen webview right now.

---

<div class="post-metadata">

**Author:** ![qkygames](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/q/6de8d8/32.png) [@qkygames](https://forum.defold.com/u/qkygames)\
**Post date:** [September 19, 2016, 1:43pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/3 "2016-09-19T13:43:34Z")

</div>

they have fullscreen ads for games, video or image.

I try make this in my game:

```
function webview_callback(self, webview_id, request_id, type, data)
    if type == webview.CALLBACK_RESULT_URL_OK then
        -- the page is now loaded, let's show it
        webview.set_visible(webview_id, 1)
    elseif type == webview.CALLBACK_RESULT_URL_ERROR then
        print("Failed to load url: " .. data["url"])
        print("Error: " .. data["error"])
    elseif type == webview.CALLBACK_RESULT_EVAL_OK then
        print("Eval ok. Result: " .. data['result'])
    elseif type == webview.CALLBACK_RESULT_EVAL_ERROR then
        print("Eval not ok. Request # " .. request_id)
    end
end

function init(self)
	local webview_id = webview.create(webview_callback)
end

```

but I get this error: `main/gd_ads.script:16: attempt to index global 'webview' (a nil value) stack traceback:`

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [September 19, 2016, 5:26pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/4 "2016-09-19T17:26:58Z")

</div>

You must test webview on iOS/Android only. Put conditionals in to disable on other platforms.

> [@Big List of Defold Pro Tips!](https://forum.defold.com/t/big-list-of-defold-pro-tips/1519/12):
>
> Detecting the current OS information for selective code. Sometimes you want to enable or disable code, or have dummy simulated code go off depending on which platform you are testing. Is there a list of all current possibilities somewhere? Can someone check what the Linux one is? local sysinfo = sys.get\_sys\_info() if sysinfo.system\_name == “Android” then … elseif sysinfo.system\_name == “iPhone OS” then … elseif sysinfo.system\_name == “Windows” then … elseif sysinfo.system\_name == “Linu…

[https://github.com/britzl/defads\_example](https://github.com/britzl/defads_example)

---

<div class="post-metadata">

**Author:** ![qkygames](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/q/6de8d8/32.png) [@qkygames](https://forum.defold.com/u/qkygames)\
**Post date:** [September 19, 2016, 5:29pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/5 "2016-09-19T17:29:38Z")

</div>

so, I can not use webview for html5 games?

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [September 19, 2016, 5:33pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/6 "2016-09-19T17:33:24Z")

</div>

Not at this time, but you can edit the html file and add ads that way.

---

<div class="post-metadata">

**Author:** ![qkygames](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/q/6de8d8/32.png) [@qkygames](https://forum.defold.com/u/qkygames)\
**Post date:** [September 19, 2016, 5:35pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/7 "2016-09-19T17:35:34Z")

</div>

what html file need edit? the “index.html” exported file?

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [September 19, 2016, 5:40pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/8 "2016-09-19T17:40:53Z")

</div>

Yes, you could modify it to put banner ads in, or full screen ads which periodically show up.

---

<div class="post-metadata">

**Author:** ![qkygames](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/q/6de8d8/32.png) [@qkygames](https://forum.defold.com/u/qkygames)\
**Post date:** [September 19, 2016, 5:42pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/9 "2016-09-19T17:42:37Z")

</div>

And i can call to the api from defold? or get callback from pausegame or resumegame function in the index?

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [September 19, 2016, 5:54pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/10 "2016-09-19T17:54:32Z")

</div>

There is no way to directly communicate between your game and scripts running on the page yet unfortunately… you could communicate with a server but that’s more messy. I wish there was some way to send messages back and forth between the game and a script on the page.

I am not sure how this works on HTML5 builds - if it checks the canvas focus or window focus - but you could try it for detecting if game is out of focus [API reference (Window)](http://www.defold.com/ref/window/)

You could make the game fullscreen, and then have a permanent rotating banner ad anchored at the bottom middle above the canvas.

[https://www.bookofdefold.com/examples/FullWindowCanvas/](https://www.bookofdefold.com/examples/FullWindowCanvas/)

[https://codepen.io/tag/fixed%20footer/](https://codepen.io/tag/fixed%20footer/)

---

<div class="post-metadata">

**Author:** ![qkygames](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/q/6de8d8/32.png) [@qkygames](https://forum.defold.com/u/qkygames)\
**Post date:** [September 19, 2016, 6:20pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/11 "2016-09-19T18:20:42Z")

</div>

I have a strange error with the edited index: [Bird Animation](http://html5.GameDistribution.com/fef023cc25cd4e01a32e921059d93252/)

I can see test ads in the gamedistribution page, but the game crash and not start.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [September 19, 2016, 6:31pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/12 "2016-09-19T18:31:19Z")

</div>

Does not appear to be uploaded properly

[http://html5.gamedistribution.com/fef023cc25cd4e01a32e921059d93252/archive/game.projectc0](http://html5.gamedistribution.com/fef023cc25cd4e01a32e921059d93252/archive/game.projectc0) 404

---

<div class="post-metadata">

**Author:** ![qkygames](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/q/6de8d8/32.png) [@qkygames](https://forum.defold.com/u/qkygames)\
**Post date:** [September 19, 2016, 6:38pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/13 "2016-09-19T18:38:52Z")

</div>

to upload the game I need make this:

Notice: For HTML5 games, file must be ZIPped file and file extention name ends with “.html5”. Ex: gamename.html5 … Index file name is “index.html”

so, I export my game to html5 and enable “release mode” compress the folde and rename.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [September 19, 2016, 7:18pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/14 "2016-09-19T19:18:53Z")

</div>

Are you zipping all of the files together and not just the index on its own? You should contact them to figure out why the game is not uploading properly.

---

<div class="post-metadata">

**Author:** ![qkygames](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/q/6de8d8/32.png) [@qkygames](https://forum.defold.com/u/qkygames)\
**Post date:** [September 19, 2016, 8:50pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/15 "2016-09-19T20:50:35Z")

</div>

They fix the problem now, so, I can pass value from Javascript into Game?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [September 20, 2016, 6:00am UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/16 "2016-09-20T06:00:30Z")

</div>

Just to be clear: Do you want to pass data to and from a running Defold HTML5 game or only pass data to the game when it is starting?

- You can not send data to/from a running HTML5 game using Javascript (I suppose you could send data using a local websocket though)
- You can send additional data when launching the game (like a user id, session token or something else)

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [September 20, 2016, 7:06am UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/17 "2016-09-20T07:06:59Z")

</div>

> [@Pkeod](#):
>
> There is no way to directly communicate between your game and scripts running on the page yet unfortunately

Not sure if this is what you’re after, but there is [webview.eval()](http://www.defold.com/ref/webview/#webview.eval:webview_id-code) which allows you to communicate back and forth with the running page.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [September 20, 2016, 7:20am UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/18 "2016-09-20T07:20:22Z")

</div>

Webview does not work on HTML5 builds. It would be great if it did.

---

<div class="post-metadata">

**Author:** ![qkygames](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/q/6de8d8/32.png) [@qkygames](https://forum.defold.com/u/qkygames)\
**Post date:** [September 25, 2016, 10:20pm UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/19 "2016-09-25T22:20:55Z")

</div>

Hi,

so, I can add a setInterval() function in my index to check every time if my localstorage is changed, so i can check if ads = 1 call function showad() and set ads = 0 in localstorage, the question is, how can create a localstorage in defold to read and set from my index.html (javascript), the game is HTML5.

sorry for my english.  
Maxi

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [September 26, 2016, 3:12am UTC](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958/20 "2016-09-26T03:12:25Z")

</div>

Great idea! I had not thought of that.

Have your Defold game and Javascript constantly check a file for any communication. You could set up a method of message passing between the game and the local Javascript files with this method. Each side can have their own inbox which they can clear, and put messages into the other’s inbox. Or share a file. I’m not sure what would be ideal in this case.

From there, you can then do any logic you need.

You can use Lua io save/load functions.

Defold uses IndexedDB API for dealing with a kind of filesystem on HTML5 builds. It uses the “/data” database version 21. If you used sys.get\_save\_file(“html5\_inbox”, “html5\_inbox\_state”) it would be the text of a path “/data/.html5\_inbox/html5\_inbox\_state” so in JS you would connect to the “/data” db and then access the “/data/.html5\_inbox/html5\_inbox\_state” key to be able to load that file.

You could save / load a simple file type, or encode/decode from something like JSON. [Here’s a JSON encoder/decoder as Defold’s build in one only decodes.](https://gist.github.com/subsoap/9c5c38416ae44a894e4c27f2e5561f0b)

Here’s an indexedDB example although as the comments there say it may be a better idea to use a pre-made solution and not work with the indexedDB api directly. I have not experimented with all of these things together yet so I can’t say for sure how to make this all work for sure but if you time give it a try.

[https://gist.github.com/BigstickCarpet/a0d6389a5d0e3a24814b](https://gist.github.com/BigstickCarpet/a0d6389a5d0e3a24814b)

I want to make an example but I do not have the spare time right now. This should be possible to do though. Again, great idea! It will probably be a workable html5 game to browser JS communication problem solution.

[Next page](https://forum.defold.com/t/implement-gamedistribution-ads-using-webview/2958.md?page=2)
