无法将 'MopubBannerAdView' 类型的 return 表达式转换为 return 类型 'UIViewController?'
Cannot convert return expression of type 'MopubBannerAdView' to return type 'UIViewController?'
我试图在我的 swiftUI 应用程序中显示 mopubBanner,但我正在努力处理我必须在委托中实现的委托方法之一。我不确定这里是否可以 return a viewController。
不实施此方法会导致横幅广告不可点击。
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
}
}
}
我怎样才能最好地解决这个问题?
您只需要设置呈现 viewController,为此您可以使用 window rootVC,例如
func viewControllerForPresentingModalView() -> UIViewController! {
return UIApplication.shared.windows.filter {[=10=].isKeyWindow}.first?.rootViewController
}
我试图在我的 swiftUI 应用程序中显示 mopubBanner,但我正在努力处理我必须在委托中实现的委托方法之一。我不确定这里是否可以 return a viewController。
不实施此方法会导致横幅广告不可点击。
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
}
}
}
我怎样才能最好地解决这个问题?
您只需要设置呈现 viewController,为此您可以使用 window rootVC,例如
func viewControllerForPresentingModalView() -> UIViewController! {
return UIApplication.shared.windows.filter {[=10=].isKeyWindow}.first?.rootViewController
}