gpt4 book ai didi

swift - 如何从 NSObject 访问 SwiftUI 环境对象?

转载 作者:行者123 更新时间:2023-12-04 17:27:12 25 4
gpt4 key购买 nike

我试图通过从 UIViewController 更改环境对象并将其传递给 SwiftUI View 来关闭广告时将变量 adsWatched 增加 1。我已经尝试将环境对象放入我的 NSObject 类 AdManager 中,其中包含我想要更改环境对象的函数,但我收到错误消息“Unknown attribute 'EnvironmentObject'”:

    public protocol AdManagerRewardDelegate{
func rewardAdGiveRewardToUser(type:String, amount: NSDecimalNumber)
func rewardAdFailedToLoad()
func rewardAdDidReceive(
rewardViewController: UIViewController?,
rewardedAd: GADRewardedAd?,
delegate: AdManager
)
func rewardAdDidOpen()
func rewardAdDidClose()
func rewardAdFailedToPresent()
}


//default implementation AdManagerRewardDelegate
public extension AdManagerRewardDelegate{
func rewardAdGiveRewardToUser(type:String, amount: NSDecimalNumber) {}
func rewardAdFailedToLoad() {}
func rewardAdDidReceive(
rewardViewController: UIViewController?,
rewardedAd: GADRewardedAd?,
delegate: AdManager
) {
if rewardedAd?.isReady == true {
if let rewardViewController = rewardViewController {
rewardedAd?.present(fromRootViewController: rewardViewController, delegate: delegate)
}
}
}
func rewardAdDidOpen() {}
func rewardAdDidClose() {}
func rewardAdFailedToPresent() {}
}

public class AdManager: NSObject {
public static let shared = AdManager()
public var ADS_DISABLED = false

public var delegateReward: AdManagerRewardDelegate?

private var viewController:UIViewController?
private var bannerViewContainer:UIView?
private var rewardViewController:UIViewController?

var interestial:GADInterstitial?
private var testDevices:[String] = [""]


private var rewardedAd: GADRewardedAd?

let borderSizeBetweenBannerAndContent:CGFloat = 5

@EnvironmentObject var settings: UserSettings


public override init() {
super.init()
}

public func configureWithApp(){
GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = testDevices
}

public func setTestDevics(testDevices: [String]){
self.testDevices = testDevices
self.testDevices += [kGADSimulatorID as! String ] //all simulator
}

private func getGADRequest() -> GADRequest{
let request = GADRequest()
return request
}



// MARK:- Reward Video Ads
public func loadAndShowRewardAd(_ adUnit: String, viewController: UIViewController){
self.rewardViewController = viewController

rewardedAd = GADRewardedAd(adUnitID: adUnit)
rewardedAd?.load(getGADRequest()) { error in
if let error = error {
// Handle ad failed to load case.
print("Reward based video ad failed to load. \(error.debugDescription)")
self.delegateReward?.rewardAdFailedToLoad()
} else {
// Ad successfully loaded.
print("Reward based video ad is received.")
self.delegateReward?.rewardAdDidReceive(
rewardViewController: self.rewardViewController,
rewardedAd: self.rewardedAd,
delegate: self
)
}
}
}
}


// MARK:- GADRewardBasedVideoAdDelegate
extension AdManager : GADRewardedAdDelegate {
public func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) {
print("Rewarded ad presented.")
delegateReward?.rewardAdDidOpen()
}

public func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) {
print("Rewarded ad failed to present.")
delegateReward?.rewardAdFailedToPresent()
}

public func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
print("Rewarded ad dismissed.")
// i want to increase adsWatched by 1 here
settings.adsWatched += 1
delegateReward?.rewardAdDidClose()
}

public func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
print("Reward received with currency: \(reward.type), amount \(reward.amount).")
delegateReward?.rewardAdGiveRewardToUser(type: reward.type, amount: reward.amount)
}
}

我已尝试将对象从我的 View Controller 传递给它,但我收到错误消息:“类型为‘View’的实例成员‘environmentObject’不能用于嵌套类型‘Ads.ViewController’的实例”这个:

    struct Ads: UIViewControllerRepresentable {

@EnvironmentObject var settings: UserSettings

@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>


typealias UIViewControllerType = UIViewController


func makeUIViewController(context: Context) -> UIViewController {
return ViewController()
}

func updateUIViewController(_ uiView: UIViewController, context: Context) {
}

class ViewController: UIViewController, GADRewardedAdDelegate, AdManagerRewardDelegate {

var rewardedAd: GADRewardedAd?

@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

@EnvironmentObject var settings: UserSettings

override func viewDidLoad() {
super.viewDidLoad()

AdManager.shared.loadAndShowRewardAd(AdIds.rewarded.rawValue, viewController: self, environmentObject(settings))
// error messages here^: "Extra argument in call","Instance member 'environmentObject' of type 'View' cannot be used on instance of nested type 'Ads.ViewController'"
AdManager.shared.delegateReward = self
}


func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
print("Reward received: \(reward.type), amount \(reward.amount).")
}

}
}

有什么方法可以更改 NSObject 中的变量吗?

最佳答案

这里的设置只是一个引用,所以你可以通过参数传递它

public func loadAndShowRewardAd(_ adUnit: String, viewController: UIViewController,
settings: UserSettings) {
self.rewardViewController = viewController
// ... other code
}

在你的代表中

struct Ads: UIViewControllerRepresentable {

@EnvironmentObject var settings: UserSettings
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>


typealias UIViewControllerType = UIViewController


func makeUIViewController(context: Context) -> UIViewController {
return ViewController(settings: settings, mode: presentationMode)
}

func updateUIViewController(_ uiView: UIViewController, context: Context) {
}

class ViewController: UIViewController, GADRewardedAdDelegate, AdManagerRewardDelegate {

var rewardedAd: GADRewardedAd?
var presentationMode: Binding<PresentationMode>
var settings: UserSettings

init(settings: UserSettings, mode: Binding<PresentationMode>) {
self.settings = settings
self.presentationMode = mode
}

override func viewDidLoad() {
super.viewDidLoad()

AdManager.shared.loadAndShowRewardAd(AdIds.rewarded.rawValue, viewController: self, settings: settings)
// error messages here^: "Extra argument in call","Instance member 'environmentObject' of type 'View' cannot be used on instance of nested type 'Ads.ViewController'"
AdManager.shared.delegateReward = self
}


func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
print("Reward received: \(reward.type), amount \(reward.amount).")
}

}
}

关于swift - 如何从 NSObject 访问 SwiftUI 环境对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62522253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com