[cocos 2DX 3.0 + ios] How to write iAd s plugin

Keywords: iOS xcode SDK

Links to the original text: http://www.cnblogs.com/KevinYuen/p/3704717.html

Cocos 2dx3.0 contains plugins for promotion, revenue and other aspects of third-party plugins, but there is no support for iAds, probably because it is exclusive to IOS, there is no need for a separate library, but in order to use the plug-in management of advertising, it is necessary to encapsulate an exclusive IOS IAD plug-in. Interested friends can refer to the imperfections, please point out:

1: How to create XCODE libraries? Create the same directories in other libraries. The file structure is the same. There are three files altogether:

  .pch

  AdsApple.h

  AdsApple.m

2: To join the unified management of Plugin Manager of Cocos 2dx, our new class needs to inherit InterfaceAds and link iAd.framework.

3: It's almost 1 o'clock, go directly to the code, complete comments:

Header document:

//
//  AdsApple
//  AdsApple
//
//  Created by kevin on 14-5-2.
//  Copyright (c) 2014 kevin. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "iAd/iAd.h"

#import "InterfaceAds.h"

// Types of Advertising
typedef enum {
    kTypeBanner = 1,    // Advertising column
    kTypeFullScreen,    // Full screen
} AppleType;


@interface AdsApple : NSObject <InterfaceAds, ADBannerViewDelegate>
{
}

@property BOOL debug;
@property bool bannerVisible;
@property int bannerPos;
@property (assign, nonatomic) ADBannerView* bannerView;

// Setting up Developer Information
- (void) configDeveloperInfo: (NSMutableDictionary*) devInfo;

// Display Advertisement
- (void) showAds: (NSMutableDictionary*) info position:(int) pos;

// Hidden Advertising
- (void) hideAds: (NSMutableDictionary*) info;

// Location acquisition
- (void) queryPoints;

//
- (void) spendPoints: (int) points;

// Switch debugging mode
- (void) setDebugMode: (BOOL) isDebugMode;

// Get SDK version
- (NSString*) getSDKVersion;

// Get the plug-in version
- (NSString*) getPluginVersion;

@end

Realize:

//
//  AdsApple
//  AdsApple
//
//  Created by kevin on 14-5-2.
//  Copyright (c) 2014 kevin. All rights reserved.
//

#import "AdsApple.h"
#import "AdsWrapper.h"

#define OUTPUT_LOG(...)     if (self.debug) NSLog(__VA_ARGS__);
#define OUT_POS CGPointMake(-1024, -1024)

@implementation AdsApple

@synthesize debug = __debug;

// Initialization
- (id)init
{
    self = [super init];
    if (self) {
        
    }
    
    return self;
}

// release
- (void)dealloc
{
    if( self.bannerView != nil ) {
        [self.bannerView removeFromSuperview];
        [self.bannerView release];
        self.bannerView = nil;
    }
    [super dealloc];
}

#pragma mark InterfaceAds impl

// Setting up Developer Information
- (void) configDeveloperInfo: (NSMutableDictionary*) devInfo
{
}

// Display Advertisement
- (void) showAds: (NSMutableDictionary*) info position:(int) pos
{
    NSString* strType = [info objectForKey:@"AppleType"];
    int type = [strType intValue];
    switch (type) {
        case kTypeBanner:
        {
            [self showBanner:pos];
            break;
        }
        case kTypeFullScreen:
            OUTPUT_LOG(@"Now not support full screen view in AppleType");
            break;
        default:
            OUTPUT_LOG(@"The value of 'AppleType' is wrong (should be 1 or 2)");
            break;
    }
}

- (void) hideAds: (NSMutableDictionary*) info
{
    NSString* strType = [info objectForKey:@"AppleType"];
    int type = [strType intValue];
    switch (type) {
        case kTypeBanner:
        {
            if (nil != self.bannerView) {
                [self.bannerView removeFromSuperview];
                [self.bannerView release];
                self.bannerView = nil;
            }
            break;
        }
        case kTypeFullScreen:
            OUTPUT_LOG(@"Now not support full screen view in AppleType");
            break;
        default:
            OUTPUT_LOG(@"The value of 'AppleType' is wrong (should be 1 or 2)");
            break;
    }
}

- (void) queryPoints
{
    OUTPUT_LOG(@"AdsApple not support query points!");
}

- (void) spendPoints: (int) points
{
    OUTPUT_LOG(@"AdsApple not support spend points!");
}

- (void) setDebugMode: (BOOL) isDebugMode
{
    self.debug = isDebugMode;
}

- (NSString*) getSDKVersion
{
    return @"6.4.2";
}

- (NSString*) getPluginVersion
{
    return @"0.2.0";
}

// Display Advertising Bar
- (void) showBanner: (int) pos
{
    // If it exists, delete it first and recreate it
    if (nil != self.bannerView) {
        [self.bannerView removeFromSuperview];
        [self.bannerView release];
        self.bannerView = nil;
    }
    
    // Establish
    self.bannerView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    self.bannerView.frame = CGRectOffset( self.bannerView.frame, 0, -50 );
    self.bannerView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
    self.bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    self.bannerView.delegate=self;
    [AdsWrapper addAdView:self.bannerView atPos:pos];
    self.bannerView.center = OUT_POS;
    self.bannerPos = pos;
    [UIView commitAnimations];
    self.bannerVisible = false;
}

// Notice before advertising
- (void)bannerViewWillLoadAd:(ADBannerView *)banner  NS_AVAILABLE_IOS(5_0)
{
    NSLog( @"bannerViewWillLoadAd" );
}

// Announcement after each new advertisement is loaded
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    NSLog( @"bannerViewDidLoadAd" );
    if( self.bannerVisible == false ) {
        [self.bannerView removeFromSuperview];
        [AdsWrapper addAdView:self.bannerView atPos:self.bannerPos];
        [UIView commitAnimations];
        self.bannerVisible = true;
        // Send an advertisement to the monitor
        [AdsWrapper onAdsResult:self withRet:kAdsShown withMsg:@"ok"];
    }
    // Send notifications of received data to listeners
    [AdsWrapper onAdsResult:self withRet:kAdsReceived withMsg:@"ok"];
}

// Error occurred
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    NSLog( @"didFailToReceiveAdWithError" );
    if( self.bannerVisible ) {
        self.bannerView.center = OUT_POS;
        self.bannerVisible = false;
        // Send an advertisement concealment (missed) notification to the listener
        [AdsWrapper onAdsResult:self withRet:kAdsDismissed withMsg:@"ok"];
    }
    // Send advertisements to listeners to receive data errors
    [AdsWrapper onAdsResult:self withRet:kNetworkError withMsg:error.domain];
}

// When the user clicks on the advertisement board notice, the return value BOOL specifies whether the advertisement is open or not.
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    NSLog( @"bannerViewActionShouldBegin" );
    return TRUE;
}

// When the full-screen advertisement is finished, the interface is called.
// When the interface is called, the current program will generally run as a background program.
// Processing that was interrupted before reverting to the interface (if any)
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
    NSLog( @"bannerViewActionDidFinish" );
    // Send a notification to the monitor that the advertisement clicks successfully closed
    [AdsWrapper onPlayerGetPoints:self withPoints:1];
}

@end

4: When using, load, display, hide, uninstall like other plug-ins.

    #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
    m_pNowAdsPtl = dynamic_cast<ProtocolAds*>(PluginManager::getInstance()->loadPlugin( "AdsApple" ) );
    if( m_pNowAdsPtl ) {
        m_mapAdsInfo["AppleType"] = "1";
        m_bUsingIAD = true;
    }
    #endif

5:iAd s have event notifications before and after click switching. In order to ensure that the listening interface remains unchanged, GetPoint becomes a listening callback after click advertising, which is used for tipping.

6:iAd s are not supported in some countries. They can switch between advertising platforms according to time zone or other detection methods. I use failure detection, which is not written out here. Each has its own methods.

End~

Reprinted at: https://www.cnblogs.com/Kevin Yuen/p/3704717.html

Posted by mickro on Wed, 10 Jul 2019 14:41:44 -0700