swiftswiftuibannermopub

Cannot convert return expression of type 'MopubBannerAdView' to return type 'UIViewController?'


Im trying to show mopubBanner in my swiftUI app and Im struggling with one of the delegate methods which I have to implement in the delegate. Im not sure if its possible to return a viewController here.

Not implementing this method results in the banner ad not being clickable.

import MoPub

struct MopubBannerAdView: UIViewControllerRepresentable {

    typealias UIViewControllerType = UIViewController

    func makeUIViewController(context: Context) -> UIViewController {
        return MopubBannerViewController()
    }
    
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    class Coordinator: NSObject, UINavigationControllerDelegate, MPAdViewDelegate {
        
        private let parent: MopubBannerAdView
        
        init(_ mopubView: MopubBannerAdView) {
            self.parent = mopubView
        }
        
        func viewControllerForPresentingModalView() -> UIViewController! {
            return self.parent /// this line here throws the error
        }
    }
}

How can I best solve this?


Solution

  • You just need to set the presenting viewController and for that you can use the window rootVC e.g

      func viewControllerForPresentingModalView() -> UIViewController! {
           return  UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.rootViewController
       }