Advertisement
luck-alex13

AdColonyPartner

Aug 30th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.00 KB | None | 0 0
  1. struct Constants
  2. {
  3.     static let adColonyAppID = "app0da80fed71f14b81b0"
  4.     static let adColonyZoneID = "vzc8cf608f563b470c8f"
  5.     static let userID = "Dy9X6wfmfoO94GtSYw2fysp4I0YGB6ji"
  6. }
  7.  
  8. class AdColonyPartner: Partner {
  9.    
  10.     var fetchedAd: AdColonyInterstitial?
  11.    
  12.     init() {
  13.     }
  14.    
  15.    
  16.     func initAdColony(from controller: UIViewController?) -> AdColonyPartner{
  17.         let opt: AdColonyAppOptions = AdColonyAppOptions()
  18.         opt.userID = Constants.userID
  19.        
  20.         AdColony.configure(withAppID: Constants.adColonyAppID, zoneIDs: [Constants.adColonyZoneID], options: nil) { (zones) in
  21.             self.requestInterstitial(from: controller)
  22.         }
  23.         return self
  24.     }
  25.    
  26.     func requestInterstitial(from controller: UIViewController?) {
  27.         NSLog("requesting Video...");
  28.         if let cntr = controller {
  29.             //AlertManager.shared.showProgressDialog(from: cntr)
  30.         }
  31.         AdColony.requestInterstitial(inZone: Constants.adColonyZoneID, options: nil, success: { (videoAd) in
  32.             NSLog("Video loaded ");
  33.             videoAd.setOpen({
  34.                 NSLog("Ad opened");
  35.             })
  36.             videoAd.setClose({
  37.                 NSLog("Ad closed");
  38.                 self.fetchedAd = nil
  39.             })
  40.             videoAd.setExpire({
  41.                 NSLog("Ad expired");
  42.                 self.fetchedAd = nil
  43.             })
  44.             videoAd.setClick({
  45.                 NSLog("Ad clicked");
  46.             })
  47.            
  48.             self.fetchedAd = videoAd
  49.             if let cntr = controller {
  50.                 // hiding progress
  51.                 // AlertManager.shared.hideProgress()
  52.                 self.fetchedAd!.show(withPresenting: cntr)
  53.             }
  54.         }) { (adError) in
  55.             NSLog("SAMPLE_APP: Request failed with error: " + adError.localizedDescription + " and suggestion: " + adError.localizedRecoverySuggestion!)
  56.             AlertManager.shared.hideProgress()
  57.         }
  58.     }
  59.        
  60.     override func showAd(from controller: UIViewController) {
  61.         if let ad = fetchedAd {
  62.             if (!ad.expired) {
  63.                 ad.show(withPresenting: controller)
  64.             }
  65.         }else {
  66.             requestInterstitial(from: controller)
  67.         }
  68.     }
  69.    
  70.     override func getName() -> String {
  71.         return "AdColony"
  72.     }
  73.    
  74.     override func getAlertMessage() -> String {
  75.         return "AdColony"
  76.     }
  77. }
  78.  
  79. // MainViewController for testing ad
  80.  
  81.     private func setUpNavigationBar(){
  82.  
  83.         let backButton = UIBarButtonItem(title: "", style: .plain, target: navigationController, action: nil)
  84.         self.navigationItem.backBarButtonItem = backButton
  85.        
  86.         self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(adTapped))
  87.         adcolony = AdColonyPartner().initAdColony()
  88.        
  89.     }
  90.    
  91.     @objc fileprivate func adTapped() {
  92.         // body method here
  93.         adcolony!. showAd(from: self)
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement