InMobi

Hey guys could someone Please give me some guidance on this topic. Im having a hard time figuring out how to show the add.

This is the code I have set up.

 local placementID = "1527058984044" local function adListener( event ) if ( event.phase == "init" ) then -- Successful initialization -- Load a banner ad inMobi.load( "banner", placementID ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.placementId ) print( event.isError ) print( event.response ) end end -- Initialize the InMobi plugin inMobi.init( adListener, { accountId="1527058984044" } ) -- Sometime later, check if the ad is loaded if ( inMobi.isLoaded( placementID ) ) then -- Show the ad inMobi.show( placementID, { yAlign="bottom" } ) end

I also have this at the very top 

local inMobi = require( “plugin.inMobi” )

I also already added the build settings code.

Three things.

  1. Are you sure your accountID and placementID should be the same value? Normally this would not be expected.

  2. You should put a table print statement at the top of your listener function so you can see any messages from the ad provider. See my code changes below.

  3. Finally, is this your actual code or are you actually waiting some time before you call the inMobi.show() block of code?  It’s likely if that’s your real code, you’re trying to show the ad before the plugin is initialized and certainly before it’s loaded.

    local json = require(“json”) – this can to at the very top of the module for better code organization local function adListener( event ) print( json.prettify( event ) ) if ( event.phase == “init” ) then – Successful initialization – Load a banner ad inMobi.load( “banner”, placementID ) elseif ( event.phase == “failed” ) then – The ad failed to load print( event.type ) print( event.placementId ) print( event.isError ) print( event.response ) end end – Initialize the InMobi plugin inMobi.init( adListener, { accountId=“1527058984044” } )

Keep in mind, testing ads only works on a real device. You can only see the console log messages if you’re on Windows by using Android Studio or “adb logcat” to watch the device’s console log. If you’re on a Mac, you can have the simulator install the app to your test device and the device console log will be routed to the Corona console log until you dismiss a dialog box.

You cannot debug this further until you see what messages you’re getting in your device’s console log.

Rob

You’re right my acc ID and app ID where different…

Should I have this inside the created scene ? Im still lost when it comes to the structure of the code.

You should only initialize the plugin once. Most people do this in main.lua. This is where your listener function should also reside. You should call inMobi.show() wherever in your code it makes sense.  If you game has a menu scene, you might want to show a banner ad there. If you’re building a game, that requires the users attention as it’s played, you generally do not want to show ads there because players simply won’t interact with them and you’re just wasting the advertisers inventory.  When the game is over or when going to a cut scene between levels, you might want to show a full screen “interstitial” ad. You would do this in the scene the user is going to so when the full screen ad is dismissed, the user sees the scene.

The tricky part is when do you call inMobi.load(). The sample code above isn’t necessarily production ready. It’s a simplistic approach to teach you that you can’t load an ad until the plugin has initialized. But once you show that loaded ad, there isn’t going to be another “init” event to call inMobi.load() again. So perhaps after you show the ad, you an call .load() to start pre-loading the next ad. Generally (and I can’t speak for this specific plugin) speaking, banners don’t need to be pre-loaded(), but any full-screen type ad does.

Rob

-- Initialize the InMobi plugin inMobi.init( adListener, { accountId="1527058984044" } ) -- Preset InMobi placement IDs (replace these with your own) local bannerPlacementID local interstitialPlacementID if ( system.getInfo("platformName") == "Android" ) then -- Android bannerPlacementID = "[1525868581506]" interstitialPlacementID = "[79e0d9d0710a46b5846aee1929a6beac]" end local function adListener( event ) print( json.prettify( event ) ) if ( event.phase == "init" ) then -- Successful initialization -- Load a banner ad inMobi.load( "banner", bannerPlacementID ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.bannerPlacementID ) print( event.isError ) print( event.response ) end end if ( inMobi.isLoaded( bannerPlacementID ) ) then -- Show the ad inMobi.show( bannerPlacementID, { yAlign="bottom" } ) end

I have narrowed it down to this…

Could you give me any pointers as to what i’am missing or doing wrong?

All iam trying to do is display a banner on my main page.

Lua is a single pass compiler. When you call:

inMobi.init( adListener, { accountId="1527058984044" } )

What is the value of adListener? You haven’t defined it yet, so it’s nil. Your adListener function will never get called.

Next, I don’t believe this is correct:

 bannerPlacementID = "[1525868581506]" interstitialPlacementID = "[79e0d9d0710a46b5846aee1929a6beac]"

The square brackets seem out of place. Try it with out them.

Since your adListener function is never called, you never load an ad. On top of that the code 

if ( inMobi.isLoaded( bannerPlacementID ) ) then -- Show the ad inMobi.show( bannerPlacementID, { yAlign="bottom" } ) end

is likely going to execute before the .init() function completes. You need to understand a major concept in app development and that’s synchronous vs asynchronous actions. Synchronous actions happen in order. These activities happen quickly and don’t create pauses in the user’s experience. Somethings take time to complete like initializing an ad plugin, or logging in to a social media network. You don’t want the user having to wait on these activities to complete. So they happen in the background and notify you when they are complete. This is why there is an adListener function.  

But since the .init() function fires in the background and notifies you when complete, code immediately after it executes as fast as it possibly can, so your inmobi.show() block will actually happen before the plugin has been initialized.

If you just want to build an ad demo app and you’re never going to have more than a main.lua and you always want a banner ad, then move the adListener function before you call inMobi.init(). You want to change your adListener function to something like:

local function adListener( event ) print( json.prettify( event ) ) if ( event.phase == "init" ) then -- Successful initialization -- Load a banner ad inMobi.load( "banner", bannerPlacementID ) elseif ( event.phase == "loaded" ) then -- Ad loaded successfully inMobi.show( bannerPlacementID, { yAlign="bottom" } ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.bannerPlacementID ) print( event.isError ) print( event.response ) end end

But once you’ve moved beyond a basic app all in main.lua, you’re going to have to learn how to call your show function when you’re in the scene you want, hide it when you don’t want to show it, and all of that rarely happens in main.lua.

Rob

Three things.

  1. Are you sure your accountID and placementID should be the same value? Normally this would not be expected.

  2. You should put a table print statement at the top of your listener function so you can see any messages from the ad provider. See my code changes below.

  3. Finally, is this your actual code or are you actually waiting some time before you call the inMobi.show() block of code?  It’s likely if that’s your real code, you’re trying to show the ad before the plugin is initialized and certainly before it’s loaded.

    local json = require(“json”) – this can to at the very top of the module for better code organization local function adListener( event ) print( json.prettify( event ) ) if ( event.phase == “init” ) then – Successful initialization – Load a banner ad inMobi.load( “banner”, placementID ) elseif ( event.phase == “failed” ) then – The ad failed to load print( event.type ) print( event.placementId ) print( event.isError ) print( event.response ) end end – Initialize the InMobi plugin inMobi.init( adListener, { accountId=“1527058984044” } )

Keep in mind, testing ads only works on a real device. You can only see the console log messages if you’re on Windows by using Android Studio or “adb logcat” to watch the device’s console log. If you’re on a Mac, you can have the simulator install the app to your test device and the device console log will be routed to the Corona console log until you dismiss a dialog box.

You cannot debug this further until you see what messages you’re getting in your device’s console log.

Rob

You’re right my acc ID and app ID where different…

Should I have this inside the created scene ? Im still lost when it comes to the structure of the code.

You should only initialize the plugin once. Most people do this in main.lua. This is where your listener function should also reside. You should call inMobi.show() wherever in your code it makes sense.  If you game has a menu scene, you might want to show a banner ad there. If you’re building a game, that requires the users attention as it’s played, you generally do not want to show ads there because players simply won’t interact with them and you’re just wasting the advertisers inventory.  When the game is over or when going to a cut scene between levels, you might want to show a full screen “interstitial” ad. You would do this in the scene the user is going to so when the full screen ad is dismissed, the user sees the scene.

The tricky part is when do you call inMobi.load(). The sample code above isn’t necessarily production ready. It’s a simplistic approach to teach you that you can’t load an ad until the plugin has initialized. But once you show that loaded ad, there isn’t going to be another “init” event to call inMobi.load() again. So perhaps after you show the ad, you an call .load() to start pre-loading the next ad. Generally (and I can’t speak for this specific plugin) speaking, banners don’t need to be pre-loaded(), but any full-screen type ad does.

Rob

-- Initialize the InMobi plugin inMobi.init( adListener, { accountId="1527058984044" } ) -- Preset InMobi placement IDs (replace these with your own) local bannerPlacementID local interstitialPlacementID if ( system.getInfo("platformName") == "Android" ) then -- Android bannerPlacementID = "[1525868581506]" interstitialPlacementID = "[79e0d9d0710a46b5846aee1929a6beac]" end local function adListener( event ) print( json.prettify( event ) ) if ( event.phase == "init" ) then -- Successful initialization -- Load a banner ad inMobi.load( "banner", bannerPlacementID ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.bannerPlacementID ) print( event.isError ) print( event.response ) end end if ( inMobi.isLoaded( bannerPlacementID ) ) then -- Show the ad inMobi.show( bannerPlacementID, { yAlign="bottom" } ) end

I have narrowed it down to this…

Could you give me any pointers as to what i’am missing or doing wrong?

All iam trying to do is display a banner on my main page.

Lua is a single pass compiler. When you call:

inMobi.init( adListener, { accountId="1527058984044" } )

What is the value of adListener? You haven’t defined it yet, so it’s nil. Your adListener function will never get called.

Next, I don’t believe this is correct:

 bannerPlacementID = "[1525868581506]" interstitialPlacementID = "[79e0d9d0710a46b5846aee1929a6beac]"

The square brackets seem out of place. Try it with out them.

Since your adListener function is never called, you never load an ad. On top of that the code 

if ( inMobi.isLoaded( bannerPlacementID ) ) then -- Show the ad inMobi.show( bannerPlacementID, { yAlign="bottom" } ) end

is likely going to execute before the .init() function completes. You need to understand a major concept in app development and that’s synchronous vs asynchronous actions. Synchronous actions happen in order. These activities happen quickly and don’t create pauses in the user’s experience. Somethings take time to complete like initializing an ad plugin, or logging in to a social media network. You don’t want the user having to wait on these activities to complete. So they happen in the background and notify you when they are complete. This is why there is an adListener function.  

But since the .init() function fires in the background and notifies you when complete, code immediately after it executes as fast as it possibly can, so your inmobi.show() block will actually happen before the plugin has been initialized.

If you just want to build an ad demo app and you’re never going to have more than a main.lua and you always want a banner ad, then move the adListener function before you call inMobi.init(). You want to change your adListener function to something like:

local function adListener( event ) print( json.prettify( event ) ) if ( event.phase == "init" ) then -- Successful initialization -- Load a banner ad inMobi.load( "banner", bannerPlacementID ) elseif ( event.phase == "loaded" ) then -- Ad loaded successfully inMobi.show( bannerPlacementID, { yAlign="bottom" } ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.bannerPlacementID ) print( event.isError ) print( event.response ) end end

But once you’ve moved beyond a basic app all in main.lua, you’re going to have to learn how to call your show function when you’re in the scene you want, hide it when you don’t want to show it, and all of that rarely happens in main.lua.

Rob