Search code examples
iosobjective-cswiftchartboost

How to implement Chartboost didCloseInterstitial in Swift


I've successfully set up Chartboost in my Swift app but would like to call a function at certain times when the interstitial view is closed.

I have virtually zero experience in Objective-C (probably the problem) but I apparently have my Obj-C files linked due to being able to access the Chartboost class and the whole process working.

I've tried the following in a couple places with no success:

func didCloseInterstitial(location: CBLocation) {
    print("closing interstitial")
}

func didDismissInterstitial(location: CBLocation) {
    print("dismissing interstitial")
}

Here's the Obj-C documentation:

/*!
 @abstract
 Called after an interstitial has been dismissed.

 @param location The location for the Chartboost impression type.

 @discussion Implement to be notified of when an interstitial has been dismissed for a given CBLocation.
 "Dismissal" is defined as any action that removed the interstitial UI such as a click or close.
 */
- (void)didDismissInterstitial:(CBLocation)location;

/*!
 @abstract
 Called after an interstitial has been closed.

 @param location The location for the Chartboost impression type.

 @discussion Implement to be notified of when an interstitial has been closed for a given CBLocation.
 "Closed" is defined as clicking the close interface for the interstitial.
 */
- (void)didCloseInterstitial:(CBLocation)location;

Solution

  • After looking at how to properly convert Objective-C methods in Swift, I added the underscore (_), which changed the function to:

    func didDismissInterstitial(_ location: CBLocation) {
        print("dismissing interstitial")
    }
    

    XCode then gave me a hint that I was close to the delegate method, but needed to change the type of location and I ended up with

    func didDismissInterstitial(_ location: String) {
        print("dismissing interstitial")
    }
    

    I also had to set the delegate as self with Chartboost.setDelegate(self) and it now works.