淘先锋技术网

首页 1 2 3 4 5 6 7

上一篇写了如何集成多盟积分墙,但是多盟积分墙只有中文版,当我们的应用需要发布到美国的APP store时,我们需要集成其他的积分墙工具来增加应用的收入。其中tapjoy就是类似于多盟的第三方移动广告提供商。


这边简单的写了一个tapjoy的积分墙Manager类,如果有需要大家可以下载,你只需要在显示积分墙的地方简单的调用manager类显示函数。

[[CPTapjoyManager sharedTJManager] showOfferWall];

CPTapjoyManager.h头文件

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

#define APP_ID @"fc145b41-7d9b-4942-9d90-6d87162794a5"
#define APP_SECRET_KEY @"z1cWed53f0ZzRwnOaE3L"

@interface CPTapjoyManager : NSObject<TJCAdDelegate,TJCVideoAdDelegate,TJCViewDelegate,UIAlertViewDelegate>
{
    int tapPoints;
    UIWindow* rootWindow;
}


+(CPTapjoyManager*) sharedTJManager;



-(void) showOfferWall;
-(void)showBannerAd;
-(void)dismissBannerAd;

-(void)showFullScreenAd;

@end

CPTapjoyManager.m文件

#import "CPTapjoyManager.h"
#import "AppDelegate.h"
#import "DataManager.h"


static CPTapjoyManager * _tjManager;

@implementation CPTapjoyManager

+(CPTapjoyManager *)sharedTJManager
{
    if(_tjManager==nil)
    {
        _tjManager = [[CPTapjoyManager alloc] init];
    }
    return _tjManager;
}


-(id)init
{
    if(self == [super init])
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tjcConnectSuccess:) name:TJC_CONNECT_SUCCESS object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tjcConnectFail:) name:TJC_CONNECT_FAILED object:nil];
        
        [TapjoyConnect requestTapjoyConnect:APP_ID
                                  secretKey:APP_SECRET_KEY
                                    options:@{
              TJC_OPTION_TRANSITION_EFFECT : @(TJCTransitionExpand),
                 TJC_OPTION_ENABLE_LOGGING : @(YES),
             TJC_OPTION_COLLECT_MAC_ADDRESS: @(TJCMacAddressOptionOffWithVersionCheck),
         // If you are not using Tapjoy Managed currency, you would set your own user ID here.
         //TJC_OPTON_USER_ID : @"A_UNIQUE_USER_ID"
         }];
        
        //注册积分墙消息监视
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(offerwallClosed:) name:TJC_VIEW_CLOSED_NOTIFICATION object:nil];
        
        //注册积分查询、消费监视
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getTJPoints:) name:TJC_TAP_POINTS_RESPONSE_NOTIFICATION object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getTJPointsError:) name:TJC_TAP_POINTS_RESPONSE_NOTIFICATION_ERROR object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(consumeTJPoints:) name:TJC_SPEND_TAP_POINTS_RESPONSE_NOTIFICATION object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(consumeTJPointsError:) name:TJC_SPEND_TAP_POINTS_RESPONSE_NOTIFICATION_ERROR object:nil];
        
        //注册全屏广告
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getTJFullScreenAd:) name:TJC_FULL_SCREEN_AD_RESPONSE_NOTIFICATION object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fullScreenAdClosed:) name:TJC_VIEW_CLOSED_NOTIFICATION object:nil];


        
    }
    return self;
}

//显示积分墙及获取积分
-(void) showOfferWall
{
    [TapjoyConnect showOffersWithViewController:[[AppDelegate sharedAppDelegate] rootViewController]];
    [TapjoyConnect getTapPoints];
}
//显示banner广告
-(void)showBannerAd
{
    [TapjoyConnect getDisplayAdWithDelegate:self];
   
}
-(void)dismissBannerAd
{
    [[TapjoyConnect getDisplayAdView] removeFromSuperview];
    rootWindow = nil;
}
//显示全屏广告
-(void)showFullScreenAd
{
    [TapjoyConnect getFullScreenAd];

}
#pragma mark Tapjoy Connect Observer Method

//连接监视函数
-(void)tjcConnectSuccess:(NSNotification*)notifyObj
{
	NSLog(@"Tapjoy connect Succeeded");
}


- (void)tjcConnectFail:(NSNotification*)notifyObj
{
	NSLog(@"Tapjoy connect Failed");
}

//积分墙监视函数
-(void)offerwallClosed:(NSNotification*)notifyObj
{
    NSLog(@"Offerwall closed");
}

//积分
//获取监视函数
-(void)getTJPoints:(NSNotification*)notifyObj
{
    NSLog(@"获取Tapjoy积分成功!");
    tapPoints = [(NSNumber*)notifyObj.object intValue];
    
    //if(tapPoints<=0)return;
    UIAlertView * alView= nil;
    if(tapPoints>0)
    {
         alView=[[[UIAlertView alloc] initWithTitle:@"Attention"
                                               message:[NSString stringWithFormat:@"You still have %d Tapjoy points. Do you want to exchange them to coins?",tapPoints]
                                               delegate:self
                                             cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil] autorelease];
        [alView show];
    }
    /*else
    {
        alView =[[[UIAlertView alloc] initWithTitle:@"Attention"
                                                          message:[NSString stringWithFormat:@"You have no Tapjoy points. Go ahead to earn more!"]
                                                         delegate:nil
                                                cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] autorelease];
    }
    
    [alView show];*/
}
-(void)getTJPointsError:(NSNotification*)notifyObj
{
    NSLog(@"获取Tapjoy积分失败!");
}

//消费监视函数
-(void)consumeTJPoints:(NSNotification*)notifyObj
{
    NSLog(@"Tapjoy积分消费成功!");
    [DataManager shareManager].coins = [DataManager shareManager].coins + tapPoints;
    UIAlertView * alView =[[[UIAlertView alloc] initWithTitle:@"Congratulations"
                                                      message:[NSString stringWithFormat:@"You get %d coins successfully!",tapPoints]
                                                     delegate:nil
                                            cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] autorelease];
    [alView show];
}
-(void)consumeTJPointsError:(NSNotification*)notifyObj
{
     NSLog(@"Tapjoy积分消费失败!");
    /*
    UIAlertView * alView =[[[UIAlertView alloc] initWithTitle:@"Bad News"
                                                      message:[NSString stringWithFormat:@"Your Tapjoy virtual currency fail to change to coins,now still has %d Tapjoy virtual currency!", tapPoints]
                                                     delegate:nil
                                            cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil] autorelease];
    [alView show];*/
}

//全屏广告监视函数
-(void)getTJFullScreenAd:(NSNotification*)notifyObj
{
    [TapjoyConnect showFullScreenAd];
}
//全屏广告退出函数
-(void)fullScreenAdClosed:(NSNotification*)notifyObj
{
    NSLog(@"Fullscreen Ad closed");
}

#pragma mark Tapjoy Display Ads Delegate Methods

- (void)didReceiveAd:(TJCAdView*)adView
{
	NSLog(@"Tapjoy Display Ad Received");
    
    if(rootWindow == nil)
    {
        rootWindow = [[AppDelegate sharedAppDelegate] window];
        [rootWindow addSubview:adView];
    }
    
}


- (void)didFailWithMessage:(NSString*)msg
{
	NSLog(@"No Tapjoy Display Ads available");
}


- (NSString*)adContentSize
{
	return TJC_DISPLAY_AD_SIZE_320X50;
}


- (BOOL)shouldRefreshAd
{
	return YES;
}


#pragma mark Tapjoy Video Ad Delegate Methods

- (void)videoAdBegan
{
	NSLog(@"Tapjoy Video Ad Began Playing");
}


- (void)videoAdClosed
{
	NSLog(@"Tapjoy Video Ad View Closed");
}


- (void)videoAdError:(NSString *)errorMsg
{
	NSLog(@"Tapjoy Video Ad Error: %@", errorMsg);
}



#pragma mark Tapjoy View Delegate Methods

- (void)viewWillAppearWithType:(int)viewType
{
	NSLog(@"Tapjoy viewWillAppearWithType: %d", viewType);
}


- (void)viewDidAppearWithType:(int)viewType
{
	NSLog(@"Tapjoy viewDidAppearWithType: %d", viewType);
    
}


- (void)viewWillDisappearWithType:(int)viewType
{
	NSLog(@"Tapjoy viewWillDisappearWithType: %d", viewType);
}


- (void)viewDidDisappearWithType:(int)viewType
{
	NSLog(@"Tapjoy viewDidDisappearWithType: %d", viewType);
}


#pragma mark UIAlertViewDelegate

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if( buttonIndex == 1) {
        [TapjoyConnect spendTapPoints:tapPoints];
    }
}

@end