Search Unity

I cannot get ads to work on iOS!

Discussion in 'Unity Ads & User Acquisition' started by Calamity_Jones, Oct 21, 2020.

  1. Calamity_Jones

    Calamity_Jones

    Joined:
    Jul 31, 2019
    Posts:
    25
    I've been bashing my head against a wall for the past two weeks trying to get Unity Ads to work, and I just can't seem to get anything to show on an iOS build deployed to TestFlight.

    Versions:
    • Unity Version: 2019.4.11f1
    • PackMan Ads Version 3.4.9
    • Asset Store Version: 3.5.0
    Services:
    • Ads enabled in Services.
    • Test mode enabled.
    Dashboard:
    • Designed for Families & Apple Kids is disabled.
    • Store IDs set for the app on both stores.
    • Test mode forced on for all devices on both platforms.
    • Ad delivery enabled on iOS and Android.
    I first tried the PackMan approach, since that seems to be the intended method, and I was able to display the ad placeholder in editor, and got a simulated ad video shown on Android. On iOS, however, I got nothing.

    I then tried a bunch of things (nothing worked) then switched to the asset store version, and got the same results. Works in editor and on Android - but does not work on iOS with TestFlight.

    Is there an issue with serving ads to an app deployed to TestFlight, or am I making a mistake somewhere?

    Thanks,
    Mark
     
  2. Unity_Adamski

    Unity_Adamski

    Unity Technologies

    Joined:
    Jul 20, 2020
    Posts:
    110
    Hi Mark, Would you be able to share with me your SDK integration code? If you aren't getting test ads at all on iOS it is almost certainly an issue with either your project setup or your integration code.
     
  3. Calamity_Jones

    Calamity_Jones

    Joined:
    Jul 31, 2019
    Posts:
    25
    Hello, thanks for getting back to me! Can you tell me where I can find this?
     
  4. Calamity_Jones

    Calamity_Jones

    Joined:
    Jul 31, 2019
    Posts:
    25
    Hello, did you mean my game IDs? They're 3847172 (iOS) and 3847173 (Android).
     
  5. Unity_Adamski

    Unity_Adamski

    Unity Technologies

    Joined:
    Jul 20, 2020
    Posts:
    110
    I was meaning, could you send me the code you have used in your project to implement our ads SDK?
     
  6. Calamity_Jones

    Calamity_Jones

    Joined:
    Jul 31, 2019
    Posts:
    25
    Oh right! I'll blame that misunderstanding on a lack of coffee :D

    I've attached the code below:


    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Advertisements;
    4.  
    5. public class AdvertisingManager : MonoBehaviour, IUnityAdsListener
    6. {
    7.     private string GameID = "";
    8.     public bool TestMode = true;
    9.     private bool IsAdvertScheduled = false;
    10.  
    11.     // Show a standard video advertisement.
    12.     public void ShowAdvertising_Interstitial()
    13.     {
    14.         if(IsAdvertScheduled == false)
    15.         {
    16.             Debug.Log("Advertising: No advertising scheduled.");
    17.         }
    18.         else
    19.         {
    20.             Advertisement.AddListener(this);
    21.  
    22.             GameID = "3847172";
    23.  
    24.             Debug.Log("Advertising Initialised. ID: " + GameID + ", Test Mode: " + TestMode);
    25.  
    26.             Advertisement.Initialize(GameID, TestMode);
    27.  
    28.             StartCoroutine(ShowAdvertising_Interstitial2());
    29.         }
    30.     }
    31.     private IEnumerator ShowAdvertising_Interstitial2()
    32.     {
    33.         float WaitDuration = 5.0f;
    34.         float Time = 0;
    35.         bool IsWaiting = true;
    36.         while (Time <= WaitDuration && IsWaiting == true)
    37.         {
    38.             // Check if UnityAds ready before calling Show method:
    39.             if (Advertisement.IsReady())
    40.             {
    41.                 Advertisement.Show("FullScreenVideo");
    42.                 Debug.Log("Advertising: Interstitial ad displayed!");
    43.                 IsWaiting = false;
    44.                 IsAdvertScheduled = false;
    45.             }
    46.             Time += UnityEngine.Time.deltaTime;
    47.             yield return null;
    48.         }
    49.         if(IsWaiting == true)
    50.         {
    51.             Debug.Log("Advertising: Interstitial ad not ready.");
    52.         }
    53.     }
    54. }
     
  7. Calamity_Jones

    Calamity_Jones

    Joined:
    Jul 31, 2019
    Posts:
    25
    Screenshot 2020-10-21 003815.jpg

    This code shows the placeholder ad in the editor (above), and a simulated ad on Android (below):
    Screenshot_20201022-120301.png
     
  8. Unity_Adamski

    Unity_Adamski

    Unity Technologies

    Joined:
    Jul 20, 2020
    Posts:
    110
    Just to confirm, are you using the correct gameID when building for iOS as using the Android one will cause ads not to show at all. Would you also be able to tell me which of you debug logs you are seeing when this code is run on iOS?

    One issue I can see in your code, unrelated to this issue, is that is possible to have the SDK initialized more than once, and I no point are you removing this class as a listener. This might cause issues later on.
     
  9. Calamity_Jones

    Calamity_Jones

    Joined:
    Jul 31, 2019
    Posts:
    25
    I'm using a "brute force" approach to ensure that the correct ID is being used. I changed the GameID to the Android code before making the Android build, and then tested it to take the above screenshots. I had originally used a bit of code to switch the ID for different platforms, but I wanted to eliminate the possibility that it was causing my problems.

    Originally, I had the initialization in a separate method, which was called when the app starts, but I wanted to experiment with initialising immediately before trying to display the ad. This previous approach of initialising at app start also worked fine for Android and editor - but not on iOS.

    I think the issue is that the initialisation is failing - what do you think? I just can't understand why it would work exactly as expected on one platform, and not at all on another. I'm not seeing anything on iOS - absolutely nothing Ad-related - no placeholders, etc.
     
  10. Unity_Adamski

    Unity_Adamski

    Unity Technologies

    Joined:
    Jul 20, 2020
    Posts:
    110
    Hi are you able to get any logs out of your iOS device? In the logs you should be able to see if the ads SDK is being initialized correctly. I've used a tool called 3uTools in the past to quickly read the device logs from an iOS device.

    If you not seeing test ads it is almost certainly an initialization error. In addition to the logs would you be able to tell me what SDK version you are using?
     
  11. Calamity_Jones

    Calamity_Jones

    Joined:
    Jul 31, 2019
    Posts:
    25
    Can you think of anything that might stop the app from initialising? I unfortunately have to wait for a colleague to make builds for me to test, as I don't have a mac. Is the initialisation process immediate? I.e. should I put the initialisation inside a wait on a coroutine maybe? I just modified the code to the below...

    So basically:
    1. The first method is called, which checks if an ad is scheduled.
    2. If so, the second is called, which calls initialization, then waits until the .IsInitialized flag returns true, subsequently calling the next coroutine.
    3. This second coroutine then waits until .IsReady() returns true, then shows the actual ad.

    I stripped out the listener stuff, since that was just used for rewarded ads.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Advertisements;
    4.  
    5. public class AdvertisingManager : MonoBehaviour
    6. {
    7.     private string GameID = "";
    8.     public bool TestMode = true;
    9.     private bool IsAdvertScheduled = false;
    10.  
    11.     // Show a standard video advertisement.
    12.     public void ShowAdvertising_Interstitial()
    13.     {
    14.         if(IsAdvertScheduled == false)
    15.         {
    16.             Debug.Log("Advertising: No advertising scheduled.");
    17.         }
    18.         else
    19.         {
    20.             GameID = "3847173";
    21.             Debug.Log("Advertising is supported: " + Advertisement.isSupported);
    22.             Debug.Log("Advertising: Attempting to initialise.");
    23.             StartCoroutine(InitialiseAdvertising());
    24.         }
    25.     }
    26.  
    27.     private IEnumerator InitialiseAdvertising()
    28.     {
    29.         float WaitDuration = 5.0f;
    30.         float Time = 0;
    31.         bool IsWaiting = true;
    32.  
    33.         Advertisement.Initialize(GameID, TestMode);
    34.  
    35.         while (Time <= WaitDuration && IsWaiting == true)
    36.         {
    37.             // Check if UnityAds ready before calling Show method:
    38.             if (Advertisement.isInitialized == true)
    39.             {
    40.                 StartCoroutine(ShowAdvertising_Interstitial2());
    41.                 Debug.Log("Advertising Initialised. ID: " + GameID + ", Test Mode: " + TestMode);
    42.                 IsWaiting = false;
    43.             }
    44.  
    45.             Time += UnityEngine.Time.deltaTime;
    46.             yield return null;
    47.         }
    48.         if (IsWaiting == true)
    49.         {
    50.             Debug.Log("Advertising: Failed to initialise.");
    51.         }
    52.     }
    53.  
    54.     private IEnumerator ShowAdvertising_Interstitial2()
    55.     {
    56.         float WaitDuration = 5.0f;
    57.         float Time = 0;
    58.         bool IsWaiting = true;
    59.         while (Time <= WaitDuration && IsWaiting == true)
    60.         {
    61.             // Check if UnityAds ready before calling Show method:
    62.             if (Advertisement.IsReady())
    63.             {
    64.                 Advertisement.Show("FullScreenVideo");
    65.                 Debug.Log("Advertising: Interstitial ad displayed!");
    66.                 IsWaiting = false;
    67.                 IsAdvertScheduled = false;
    68.             }
    69.             Time += UnityEngine.Time.deltaTime;
    70.             yield return null;
    71.         }
    72.         if(IsWaiting == true)
    73.         {
    74.             Debug.Log("Advertising: Interstitial ad not ready.");
    75.         }
    76.     }
    77.  
    78.     // Schedule an advert once returned to the menu.
    79.     public void ScheduleAdvertisement()
    80.     {
    81.         IsAdvertScheduled = true;
    82.     }
    83. }
     
  12. Calamity_Jones

    Calamity_Jones

    Joined:
    Jul 31, 2019
    Posts:
    25
    So, that last bit of code has fixed my problem, and I got the test ads to show correctly on my TestFlight build.

    It turns out that you have to wait for the initialization process to finish - it is not immediate.

    Perhaps the example code on the unity documentation could be altered to suggest this?

    Or is there a better way of handling this?
     
  13. Unity_Adamski

    Unity_Adamski

    Unity Technologies

    Joined:
    Jul 20, 2020
    Posts:
    110
    That's a good spot I missed that. Yes as the Initialization process happens asynchronously due to requiring contact with the Unity servers, you have to be prepared for the SDK to still be initializing if you want to Initialize and run an ad in the function. The normal process for doing this is to put a while loop in to check if you ads SDK is initialized.

    Here's an example form out documentation
    Code (CSharp):
    1. IEnumerator ShowBannerWhenInitialized () {
    2.     while (!Advertisement.isInitialized) {
    3.         yield return new WaitForSeconds(0.5f);
    4.     }
    5.     Advertisement.Banner.Show (placementId);
    6. }