// // EQNBaseViewController.m // Earthquake Network // // Created by Busi Andrea on 16/08/2020. // Copyright © 2020 Earthquake Network. All rights reserved. // #import "EQNBaseViewController.h" #import "Costanti.h" @import GoogleMobileAds; @interface EQNBaseViewController () @property (weak, nonatomic, readwrite) IBOutlet UIView *bannerContainerView; @property (strong, nonatomic) GADBannerView *bannerView; @end @implementation EQNBaseViewController #pragma mark - Accessories - (BOOL)isBannerVisible { #if ADS_ENABLED return ![EQNPurchaseUtility isProVersionEnabled]; #else return NO; #endif } #pragma mark - View Lifecycle - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveInAppNotification:) name:EQNInAppPurchaseDidCompleteNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveDownloadCompleteNotification:) name:EQNDownloadDataDidCompleteNotification object:nil]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; #if ADS_ENABLED [self handleAdBanner]; #else [self hideAdBanner]; #endif } #pragma mark - Public - (void)refreshUI { // nope } #pragma mark - Private - (void)handleAdBanner { // ad banner mut be displayed only for non pro users if (!self.isBannerVisible) { [self hideAdBanner]; return; } [self showAdBanner]; } - (void)hideAdBanner { // make sure that the banner is hidden self.bannerContainerHeightConstraint.constant = 0; // remove the banner (if available) [self.bannerView removeFromSuperview]; self.bannerView = nil; } - (void)showAdBanner { if (self.bannerView == nil) { [self createBanner]; } // Determine the view width to use for the ad width. // Here safe area is taken into account, hence the view frame is used after // the view has been laid out. CGRect frame = UIEdgeInsetsInsetRect(self.view.frame, self.view.safeAreaInsets); CGFloat viewWidth = frame.size.width; // Step 3 - Get Adaptive GADAdSize and set the ad view. // Here the current interface orientation is used. If the ad is being // preloaded for a future orientation change or different orientation, the // function for the relevant orientation should be used. GADAdSize adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(viewWidth); self.bannerView.adSize = adSize; self.bannerContainerHeightConstraint.constant = adSize.size.height; // Step 4 - Create an ad request and load the adaptive banner ad. GADRequest *request = [GADRequest request]; [self.bannerView loadRequest:request]; } - (void)createBanner { self.bannerView = [[GADBannerView alloc] init]; self.bannerView.adUnitID = EQNAdMobAppIdAdaptiveBanner; self.bannerView.rootViewController = self; self.bannerView.translatesAutoresizingMaskIntoConstraints = NO; [self.bannerContainerView addSubview:self.bannerView]; [self.bannerView.topAnchor constraintEqualToAnchor:self.bannerContainerView.topAnchor].active = YES; [self.bannerView.bottomAnchor constraintEqualToAnchor:self.bannerContainerView.bottomAnchor].active = YES; [self.bannerView.leadingAnchor constraintEqualToAnchor:self.bannerContainerView.leadingAnchor].active = YES; [self.bannerView.trailingAnchor constraintEqualToAnchor:self.bannerContainerView.trailingAnchor].active = YES; } #pragma mark - Notification - (void)didReceiveInAppNotification:(NSNotification *)notification { [self hideAdBanner]; dispatch_async(dispatch_get_main_queue(), ^{ [self refreshUI]; }); } - (void)didReceiveDownloadCompleteNotification:(NSNotification *)notification { dispatch_async(dispatch_get_main_queue(), ^{ [self refreshUI]; }); } @end