How to Get Midscroll Ads to Display in an iOS Native App

This guide will outline the steps you must take to implement midscroll ads in your iOS native app using SwiftUI (though the same principles should apply to UIKit as well).

By following these steps, you can embed the ad in a SwiftUI view, position it with a "fixed" position on the screen as the user scrolls, and utilize a Google ad view within the SwiftUI view. Resizing the view based on the device dimensions, finding the WKWebView within the Google ad view, and setting Adnami parameters are essential actions you should undertake to successfully display midscroll ads.


Step-by-Step Guide:
  1. Embed the Ad in a SwiftUI View:
       - Create a new SwiftUI view that will contain the ad.
       - Position the ad using a "fixed" position on the screen by displacing it with the y-offset of the scroll view.
  2. Add a UIViewRepresentable View:
       - Within your SwiftUI view, add another view using UIViewRepresentable.
       - This view will encapsulate the Google ad and provide the necessary bridging between SwiftUI and UIKit.
  3. Resize the View:
       - In the UIViewRepresentable view, resize the view based on the device dimensions.
       - Set the width of the view to match the device's width, and adjust the height according to the agreed-upon dimensions.
       - Utilize the Ad-framework's resize method to resize the view accordingly.
  4. Load the Ad and Find the WKWebView:
       - When the ad loads, recursively search for the WKWebView within the Google ad view.
       - Check the document.readyState property of the web view.
       - If the ready state is not "complete," wait for a short timeout and check again.
  5. Set Adnami Parameters:
       - Once the ready state is "complete," set the desired parameters for Adnami in the ad.
       - Use the web view's evaluateJavaScript method to execute the necessary JavaScript code.
       - Set the adnm.renderMode to 'BANNER' and adnm.appName to your identifier.
       - Handle the result and error, if any.
  6. Ad Displayed:
       - Following these steps, the ad should be successfully displayed in your iOS native app using midscroll functionality.

The evaluateJavaScript code to execute:
webview.evaluateJavaScript("var adnm = adnm || {};adnm.renderMode = 'BANNER';adnm.appName = \"https://[YOUR APP IDENTIFIER DOMAIN]\";") 
{
// Check result, error in
// Ad done
}
Please change the [YOUR APP IDENTIFIER DOMAIN] into a domain that you wish to track the views under - example could be https://app1.adnami.io.

Note: Ensure you have the necessary dependencies and configurations in place, such as integrating Adnami and Google Ad SDKs, setting up appropriate permissions and app identifiers, and handling any required callback or event handling for the ad display. Implementing these actions will contribute to the successful integration of midscroll ads in your app.