How to get the UIViewController instance for displaying dialog when the app starts up?

Hi! iOS/MacOS developers,

I am trying to integrate a SDK to my iOS app, and I need to call a function provided by the SDK when the app starts running, to display a dialog.

My iOS app is developed with SwiftUI. For now, I attempt to add the code to the didFinishLaunchingWithOptions delegate of the AppDelegate.swift file, like the following:

import MoPubSDK
// ...
// Check whether you must show the consent dialog
MoPub.sharedInstance().shouldShowConsentDialog;
MoPub.sharedInstance().loadConsentDialog(){ 
   a in MoPub.sharedInstance().showConsentDialog(
           from: myUIViewController, completion: nil)}

However, I don't know how I can get the myUIViewController variable.

My questions are:

  1. how can I get a UIViewController instance when the app starts, such that I can pass it into the SDK function for dialog display?
  2. If it is not possible to do it in AppDelegate.swift, can it be done some where else?

Thank you!

Replies

how can I get a UIViewController instance when the app starts, such that I can pass it into the SDK function for dialog display?

If it is not possible to do it in AppDelegate.swift, can it be done some where else?

If your project have an automatically generated SceneDelegate.swift, you may do it in it. But it is hard to say if it really would work or not, without knowing what showConsentDialog does and how your project is organized.

  • Hi OOPer,

    My project does not have an automatically generated SceneDelegate.swift. I remember when the iOS13 was the newest iOS version, SceneDelegate.swift is automatically generated when I create a project, but for newer version (after iOS14) of Xcode, both SceneDelegate.swift and AppDelegate.swift are no longer automatically generated. I was able to add a an AppDelegate.swift file to the project, but don't know how to add SceneDelegate.swift.

    As long as there is a way for me to get the UIViewControl object that is being display on the phone, I think passing it into the SDK function will make it display the proper dialog.

    By the way, I followed the following link to add the AppDelegate.swift, but wasn't able to find a similar method for SceneDelegate.swift www .hackingwithswift.com/quick-start/swiftui/how-to-add-an-appdelegate-to-a-swiftui-app

  • for newer version (after iOS14) of Xcode, both SceneDelegate.swift and AppDelegate.swift are no longer automatically generated. Using Xcode 12.x, you can choose Life Cycle as UIKit App Delegate for iOS App, and Xcode will generate both SceneDelegate.swift and AppDelegate.swift. Better forget that unreliable link and make Xcode generate them, after that, you can move other SwiftUI files into the new project.

Add a Comment

Hi OOper,

Thank you for your suggestion about using Life Cycle as UIKit App Delegate. I will try it. On another hand, I found a way to get a UIViewController object from ContentView.swift file, like the code below. Is this solution Ok? or there will be issue?

Thank you!

import SwiftUI
import MoPubSDK

struct ContentView: View {
    var body: some View {
        return

        Text("Hello, world!")
            .padding()

            .onAppear(){
                // for MoPub: Check whether you must show the consent dialog
                let shouldShow = MoPub.sharedInstance().shouldShowConsentDialog;
                // for MoPub: Start loading the consent dialog. This call fails if the user has opted out of ad personalization
                let myUIViewController : UIViewController = (UIApplication.shared.windows.first?.rootViewController)!
                MoPub.sharedInstance().loadConsentDialog(){ a in
                    MoPub.sharedInstance().showConsentDialog(from: myUIViewController, completion: nil)
                }
            }
    }
}

Hi OOper,

For the "UIKit App Delegate" option of Life Cycle, I found it is only available if I chose "iOS" App template for the new project. If I chose "Multiplatform" template, then there is no Life Cycle choices for me to select.

Thank you!

  • You wrote my iOS app in your opening post, and there are no UIViewController available for Mac. What are you trying to do? You are not interested in using UIViewController  any more?

Add a Comment