插屏自定义平台的适配方法
插屏的自定义适配来和横幅类似,AFP已经默认适配了一些平台的插屏样式,并且适配器的源代码是开放的,开发者可以参考这些实例来完成自己的适配,这里以适配广点通的插屏(MMUGDTInterstitialAdapter.m)为例介绍。
&emsp首先开发者定义的适配器必须是MMUInterstitialAdNetworkAdapter的子类,并实现以下方法:
返回平台ID的方法:
+ (MMUAdNetworkType)networkType { return MMUAdNetworkTypeGDT; }
此方法和之前介绍的一样,就是需要返回目前适配的平台编号,对于自定义平台,这个是开发者在AFP后台生成的自定义平台编号。
重写基类的load方法。
+ (void)load { [[MMUSDKInterstitialNetworkRegistrysharedRegistry] registerClass:self]; }
此方法是将当前的适配器类注册给AFP SDK,这样AFP SDK就会知道当前支持插屏创意的有哪几个平台,并根据AFP服务下发的配置中的平台列表依次向这些平台请求创意。
重写基类的getAd方法,当AFP SDK尝试向某个实现了插屏样式的第三方平台请求创意的时候会调用此方方法。它做的几个重要的事情如下
- (void)getAd { //1,需要调用基类的adInterstitialWillStartRequest方法, //此方法告诉AFP SDK已经找到一个平台,将要开始创意请求。 [self adInterstitialWillStartRequest]; ...... //2,self.ration是当前适配器获取的针对当前平台的配置信息,它是NSDictinary类型的,主要的成员有: //(1)该平台SDK插屏创意请求所需的信息,key值为KEY_NETSET,对于广点通来说value为appid、pid的值。 NSDictionary *config = [self.ration mmuDictionaryValueForKey:@"netset"]; NSString *appid = [config mmuStringValueForKey:@"appid"]; NSString *pid = [config mmuStringValueForKey:@"pid"]; //3,至此获取了广点通插屏创意请求所需要的全部参数,根据这些参数使用广点通的SDK请求插屏创意 self.interstitial = [[GDTMobInterstitial alloc] initWithAppkey:appid placementId:pid]; self.interstitial.delegate = self; //4,通知AFP插屏创意请求已经开始 [self adInterstitialDidStartRequest]; }
注意这里的插屏创意请求分为“即将开始”和“已经开始”两个步骤,这允许开发者在“即将开始”之后和“已经开始”之前中止请求,比如,可以使用adInterstitialFailWithError让请求直接结束。
重写基类的插屏展现方法
- (void)presentInterstitial { if (!self.interstitial.isReady) { NSLog(@"%s ad is not ready",__FUNCTION__); return; } UIViewController *vc = [self.delegate viewControllerForPresentingInterstitialModalView]; [self.interstitial presentFromRootViewController:vc]; }
- 该平台插屏请求成功的回调
- (void)interstitialSuccessToLoadAd:(GDTMobInterstitial *)interstitial { [self adInterstitiaDidReceiveAd]; }
- 该平台插屏请求失败的回调
- (void)interstitialFailToLoadAd:(GDTMobInterstitial *)interstitial error:(NSError *)error { [self adInterstitialFailWithError:MMUE_PFAdFailed]; }
- 插屏点击回调
- (void)interstitialClicked:(GDTMobInterstitial *)interstitial { [self adInterstitialClicked]; }
该平台插屏其他代理方法
//插屏广告将要展示 - (void)interstitialWillPresentScreen:(GDTMobInterstitial *)interstitial { if ([self getAdapterState] >= ST_MMUAapterStop) { return; } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wundeclared-selector" //直接发消息给delegate if ([self.delegate respondsToSelector:@selector(MMUEXT_InterstitialAdWillPresent)]) { [self.delegate performSelector:@selector(MMUEXT_InterstitialAdWillPresent)]; } #pragma clang diagnostic pop } //插屏广告展示成功回调该方法 - (void)interstitialDidPresentScreen:(GDTMobInterstitial *)interstitial { NSLog(@"广点通插屏已经显示在屏幕"); [self adInterstitiaDidPresent]; } //插屏广告展示结束回调该方法 - (void)interstitialDidDismissScreen:(GDTMobInterstitial *)interstitial { NSLog(@"广点通插屏已经消失在屏幕"); if ([self getAdapterState] >= ST_MMUAapterStop) { return; } [self adInterstitialDismissScreen]; } // 当点击下载应用时会调用系统程序打开,应用切换到后台 - (void)interstitialApplicationWillEnterBackground:(GDTMobInterstitial *)interstitial { NSLog(@"广点通插屏将要进入后台"); } //插屏广告曝光回调 - (void)interstitialWillExposure:(GDTMobInterstitial *)interstitial { NSLog(@"广点通插屏将要被显示"); }
开发者需要更该平台支持的回调通知基础类响应的方法,基类没有定义的方法,但是该平台定义了,可以通过直接发消息给delegate(MMUInterstitialView的代理)的方式实现。