[英]Chartboost Delegate not working in swift
所需:当代理方法调用成功时,我想做些事情观察:代理方法未成功调用屏幕上的广告的显示错误代码:Chartboost.delegate = self
错误:类型“ Chartboost”没有成员“代表”
AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Chartboost.start(withAppId: "4f21c409cd1cb2fb7000001b", appSignature: "92e2de2fd7070327bdeb54c15a5295309c6fcd2d", delegate: nil)
return true
}
ViewController代码
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
}
}
如果委托设置为nil,则调用委托方法的类(在本例中为Chartboost)将无法进行委托的方法调用。 您应该将委托设置为实现Chartboost期望的委托方法的类的“自身”。
在上面的示例中,您可以将Chartboost委托设置为ViewController的“自身”。
例如,在ViewController内部,您已经在类签名中声明了“ ChartboostDelegate”。 当您想打开Chartboost委托方法时,可以使用以下方法将ViewController的“自身”分配给Chartboost委托:
Chartboost.delegate = self
对于Chartboost,看起来作者将委托设为私有,因此可以使用以下方法在ViewController中进行设置:
Chartboost.start(withAppId: "some uid", appSignature: "some other uid", delegate: self)
或者,后来发现:
Chartboost.setDelegate(self)
(也可以通过在故事板上找到ViewController实例在AppDelegate类中进行设置。这种情况不太适合。)
如果在生成委托方法的调用签名存根(委托期望的方法调用)时遇到问题,XCode将为您自动生成它们。 只需单击您的类声明旁边的错误消息:
Type '<your class implementing the delegate methods>' does not conform to protocol '<the delegate protocol to implement>'
将出现有关该错误的更多详细信息。 单击“修复”按钮,XCode将为您自动生成方法存根。
必须使用Chartboost.setDelegate(self)将委托设置为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
}
在研究了如何在Swift中正确转换Objective-C方法之后,我添加了下划线(_),将功能更改为:
func shouldDisplayRewardedVideo(_ location: CBLocation) -> Bool
{
return true
}
func shouldRequestInterstitial(_ location: CBLocation) -> Bool {
return true
}
然后,XCode提示我接近委托方法,但是需要更改位置类型,最后我得到了
func shouldDisplayRewardedVideo(_ location: String) -> Bool
{
return true
}
func shouldRequestInterstitial(_ location: String) -> Bool {
return true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.