title: How can I access an SwiftUI environment object from an NSObject?
body: <p>I'm trying to increment the variable adsWatched by 1 when an ad is dismissed by changing the environment object from a UIViewController and passing it to a SwiftUI view. I've tried putting an environment object in my NSObject class AdManager, which contains the function I want to change the environment object in, but I get the error message &quot;Unknown attribute 'EnvironmentObject'&quot;:</p>
  <pre><code>    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] = [&quot;&quot;]
  
  
  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() -&gt; 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(&quot;Reward based video ad failed to load. \(error.debugDescription)&quot;)
          self.delegateReward?.rewardAdFailedToLoad()
        } else {
          // Ad successfully loaded.
          print(&quot;Reward based video ad is received.&quot;)
          self.delegateReward?.rewardAdDidReceive(
              rewardViewController: self.rewardViewController,
              rewardedAd: self.rewardedAd,
              delegate: self
          )
        }
      }
  }
  }
  
  
  // MARK:- GADRewardBasedVideoAdDelegate
  extension AdManager : GADRewardedAdDelegate {
  public func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) {
      print(&quot;Rewarded ad presented.&quot;)
      delegateReward?.rewardAdDidOpen()
  }
  
  public func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) {
      print(&quot;Rewarded ad failed to present.&quot;)
      delegateReward?.rewardAdFailedToPresent()
  }
  
  public func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
      print(&quot;Rewarded ad dismissed.&quot;)
      // i want to increase adsWatched by 1 here
      settings.adsWatched += 1
      delegateReward?.rewardAdDidClose()
  }
  
  public func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
      print(&quot;Reward received with currency: \(reward.type), amount \(reward.amount).&quot;)
      delegateReward?.rewardAdGiveRewardToUser(type: reward.type, amount: reward.amount)
  }
  }
  </code></pre>
  <p>I've tried passing the object to it from my view controller, but I get the error message: &quot;Instance member 'environmentObject' of type 'View' cannot be used on instance of nested type 'Ads.ViewController'&quot; for this:</p>
  <pre><code>    struct Ads: UIViewControllerRepresentable {
      
      @EnvironmentObject var settings: UserSettings
      
      @Environment(\.presentationMode) var presentationMode: Binding&lt;PresentationMode&gt;
      
  
  typealias UIViewControllerType = UIViewController
      
  
  func makeUIViewController(context: Context) -&gt; UIViewController {
      return ViewController()
  }
  
  func updateUIViewController(_ uiView: UIViewController, context: Context) {
  }
  
  class ViewController: UIViewController, GADRewardedAdDelegate, AdManagerRewardDelegate {
      
  var rewardedAd: GADRewardedAd?
      
  @Environment(\.presentationMode) var presentationMode: Binding&lt;PresentationMode&gt;
      
      @EnvironmentObject var settings: UserSettings
      
  override func viewDidLoad() {
  super.viewDidLoad()
          
      AdManager.shared.loadAndShowRewardAd(AdIds.rewarded.rawValue, viewController: self, environmentObject(settings))
      // error messages here^: &quot;Extra argument in call&quot;,&quot;Instance member 'environmentObject' of type 'View' cannot be used on instance of nested type 'Ads.ViewController'&quot;
      AdManager.shared.delegateReward = self
      }
      
  
      func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
        print(&quot;Reward received: \(reward.type), amount \(reward.amount).&quot;)
      }
      
  }
  }
  </code></pre>
  <p>Is there any way I can change the variable from an NSObject?</p>