Search code examples
objective-cswiftdelegateschartboost

Objective C Delegate in Swift


I just implemented Chartboost into my all swift app.

I have rewarded interstitials. So I need to set up a delegate using the objective C function Chartboost gave me. I cannot figure out how to do it. This is how I did Admob via Swift.

   NSNotificationCenter.defaultCenter().addObserver(self, selector: "presentInterstitial:", name: "AdMobCall", object: nil) 

   func presentInterstitial(sender:UIButton!) {

    if let isReady = interstitial?.isReady {   // _ isReady
        interstitial?.presentFromRootViewController(self)

    }
}

 NSNotificationCenter.defaultCenter().postNotificationName("AdMobCall", object: nil)

But the Objective C Chartboost function I am given is:

  • (void)didCompleteRewardedVideo:(CBLocation)location withReward:(int)reward;

And I do not understand how to create the same delegate I did for Admob using this function. it doesn't seem possible. I have the Chartboost rewarded intersitials working. But I have no way of setting up the delegate to see if they watched the video to completion or not.


Solution

  • If you want achieve objective-c delegate in to swift. You need to do following steps.

    1. Need to create bridge file to import your objective-c classes

      //
      //  Use this file to import your target's public headers that you would like to expose to Swift.
      //
      
      #import "UVIViewController.h"
      
    2. You need to extend the protocol name in your swift viewcontroller

      import UIKit
      
      class ViewController: UVIViewController, UVIDataRefreshProtocol {   // 2. Extend Protocal
      
          var secondController: DelegateViewController?
      
          @IBOutlet var delegateRefresh: UILabel!
      
          override func viewDidLoad() {
              super.viewDidLoad()
              // Do any additional setup after loading the view, typically from a nib.
      
              self.registerForBasicDataRefreshNotification();             //  3. Register the protocol notification
          }
      
          override func didReceiveMemoryWarning() {
              super.didReceiveMemoryWarning()
              // Dispose of any resources that can be recreated.
          }
      
          // Register the protocol notification with same name. Example : DataRefreshNotification
          override func registerForBasicDataRefreshNotification() {
      
              self.validateBasicDataRefreshNotification();
              NSNotificationCenter.defaultCenter().addObserver(        // 4. Register the protocol notification with same name.
                  self,
                  selector: "basicDataDidRefresh",
                  name: DataRefreshNotification,
                  object: nil)
      
          }
      
          override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
              delegateRefresh.text = "";
              if segue.identifier == "view_to_delegate" {
                  secondController = segue.destinationViewController as? DelegateViewController
              }
          }
      
          // Basic Data Did Refresh Notification
          func basicDataDidRefresh() {                            //  4. Event will be triggered while calling the protocol post notification. called with same name
                  NSLog("\n Basic data is refreshed")
                  delegateRefresh.text = "Basic data is refreshed";
          }
      
      }
      

        5. NSNotificationCenter.defaultCenter().postNotificationName(DataRefreshNotification, object: nil)  // Call the notification.
    

    Sample Project: https://github.com/vigneshuvi/Objective-C-Delegate-Swift