简体   繁体   中英

Chartboost Delegate not working in swift

Desired : I want to do something when Delegates method call Observed :Delegates method not calling Ad's show on the screen successfully Error code:Chartboost.delegate=self

Error: Type 'Chartboost' has no member 'delegate'

AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool  {

    Chartboost.start(withAppId: "4f21c409cd1cb2fb7000001b", appSignature: "92e2de2fd7070327bdeb54c15a5295309c6fcd2d", delegate: nil)

    return true
}

ViewController Code

class ViewController: UIViewController,GADBannerViewDelegate, GADInterstitialDelegate,GADRewardBasedVideoAdDelegate,IMBannerDelegate, IMInterstitialDelegate ,ChartboostDelegate{

    @IBAction func Vedio(_ sender: Any) {

        Chartboost.showRewardedVideo(CBLocationMainMenu)

    }

    @IBAction func LoadFullAd(_ sender: Any) {

        Chartboost.showInterstitial(CBLocationHomeScreen)
    }

     private func shouldDisplayRewardedVideo(_ location: CBLocation) -> Bool {
        return true
    }

    private func shouldRequestInterstitial(_ location: CBLocation) -> Bool {
        return true
    }

}

If the delegate is set to nil, the class that calls the delegate's methods (in this case Chartboost) will not be able to make the delegate's method calls. You should set the delegate to the 'self' of the class where you have implemented the delegate methods expected by Chartboost.

In the example above, you could set the Chartboost delegate to the 'self' of the ViewController.

For example, inside of ViewController, you have already declared the 'ChartboostDelegate' in the class signature. When you want to turn on the Chartboost delegate methods, assign the ViewController's 'self' to the Chartboost delegate using something like:

Chartboost.delegate = self

In the case of Chartboost, it looks like the author made the delegate private, so it can be set in the ViewController using:

Chartboost.start(withAppId: "some uid", appSignature: "some other uid", delegate: self)

or, as later found out:

Chartboost.setDelegate(self)

(It can also be set in the AppDelegate class by locating the ViewController instance in the storyboard. Not a great fit in this case.)

If you're having problems generating the delegate method's call signature stubs (the method calls expected by the delegate), XCode will autogenerate them for you. Just click on the error message found next to your class declaration:

Type '<your class implementing the delegate methods>' does not conform to protocol '<the delegate protocol to implement>'

More detail about the error will appear. Click the 'Fix' button and XCode will autogenerate the method stubs for you.

A had to set the delegate as self with Chartboost.setDelegate(self)

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    Chartboost.start(withAppId: "4f21c409cd1cb2fb7000001b", appSignature: "92e2de2fd7070327bdeb54c15a5295309c6fcd2d", delegate:self as ChartboostDelegate)
    Chartboost.setDelegate(self as ChartboostDelegate)

    return true
}

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

func shouldDisplayRewardedVideo(_ location: CBLocation) -> Bool
{

    return true
}
 func shouldRequestInterstitial(_ location: CBLocation) -> Bool {

    return true
}

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 shouldDisplayRewardedVideo(_ location: String) -> Bool
{

    return true
}
 func shouldRequestInterstitial(_ location: String) -> Bool {

    return true
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM