Cocos2d Cocos2d x Documentation - RedTroops/IOS-SDK GitHub Wiki
#RedTroops SDK 3.2.1 for IOS ##Cocos2d - Cocos2d-x Documentation Requirements: IOS 6.0 +
###Getting Started
RedTroops SDK 3.2.1 currently features:
Push Notifications.
Send user spent time.
4 types of (HTML5/Image/Video/Audio) ads.
#####For Cocos2d-x #####Before adding any of the following to your code, make sure to change the target file extension from (.cpp) to (.mm).
Setting Up RedTroops SDK 3.2.1 In Your Project
Follow the steps below to get your RedTroops SDK 3.2.1 running:
-
Download the SDK from RedTroops' website.
-
Drag & Drop downloaded files (RedTroops.a + include) to your project.
-
Add following frameworks:
- SystemConfiguration.framework
- QuartzCore.framework
- CoreGraphics.framework
- Security.framework
- MobileCoreServices.framework
- CoreTelephony.framework
###Initial Setup
- In your app delegate (AppDelegate.m), import RTSessionManager
#import "RTSessionManager.h"
- In your app delegate (AppDelegate.m), find the method
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:
(NSDictionary*)launchOptions
and add the following line of code
[[RTSessionManager defaultManager]startSessionWithAPIKey:@"Your API KEY"];
- Your API Key: You can find it in RedTroops administration panel. http://developer.dev.redtroops.com/app/index
- In your app delegate (AppDelegate.m), find the method
- (void)applicationWillEnterForeground:(UIApplication*)application
and add the following line of code
[[RTSessionManager defaultManager]startSessionWithAPIKey:@"Your API KEY"];
- In your app delegate (AppDelegate.m), find the method
- (void)applicationWillTerminate:(UIApplication *)application
and add the following line of code
[[RTSessionManager defaultManager] endTheSession];
- In your app delegate (AppDelegate.m), find the method
(void)applicationDidEnterBackground:(UIApplication *)application
and add the following line of code
[[RTSessionManager defaultManager] endTheSession];
####Calling native function from Cocos2d-x
To call a function from Cocos2d-x
- Create a new class (name it ObjCCalls)
In ObjCCalls.h write the following code
class ObjCCalls{
public:
static void objectiveC_call();
};
In ObjCCalls.mm write the following code
void ObjCCalls::objectiveC_call()
{
NSLog(@"Enter here RedTroops ads methods listed below");
}
2.Add the following line of code anywhere in your Cocos2d-x project to call the previous function
#include"ObjCCalls.h"
void HelloWorld::obCCalling()
{
//calling objective-c code
ObjCCalls::objectiveC_call();
}
####RedTroops offer 3 types of ads:
#####1. Banner ( Top Screen Banner - Bottom Screen Banner)
#####2. Interstitial
#####3. Native
#####4. Audio
#####5. Video
###1. Banner
######Before adding a banner: In ObjCCalls.mm , add these 2 properties
float heightOfScreen;
float widthOfScreen;
add the following method (this method will give the current screen width and height using any iOS)
void getScreenSize ()
{
NSString *osVersion = [[UIDevice currentDevice] systemVersion];
float osVERSION = [osVersion floatValue];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
if (osVERSION >= 8)
{
heightOfScreen = screenHeight;
widthOfScreen = screenWidth;
}
else
{
UIInterfaceOrientation statusBarOrientation =[UIApplication sharedApplication].statusBarOrientation;
if (statusBarOrientation==4||statusBarOrientation==3)
{
heightOfScreen = screenWidth;
widthOfScreen = screenHeight;
}
else if (statusBarOrientation==1||statusBarOrientation==2)
{
heightOfScreen = screenHeight;
widthOfScreen = screenWidth;
}
}
}
#####A. Top Screen Banner
- Import the following file to your view controller
#import “RTAdView.h"
In ObjCCalls.mm , add this property
RTAdView *topBanner;
- Add the following line to your view controller
getScreenSize();
float widthOfAd;
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{widthOfAd = widthOfScreen*0.5;}
else
{widthOfAd = 320;}
float heightOfAd = widthOfAd*(75.0/320);
float xOfAd = (widthOfScreen-widthOfAd)/2;
float yOfAd = heightOfScreen-heightOfAd;
topBanner = [[RTAdView alloc] initWithSize:RTAdBannerTop];
topBanner.frame = CGRectMake(xOfAd,0,widthOfAd,heightOfAd);
[[[[[UIApplication sharedApplication]keyWindow]subviews]objectAtIndex:0]addSubview:topBanner];
[[[[[UIApplication sharedApplication]keyWindow]subviews]objectAtIndex:0]bringSubviewToFront: topBanner];
[topBanner prepareAd];
[topBanner loadRequest];
[topBanner showAd];
Ad availability could be checked before adding the ad itself
Call the following function
topBanner = [[RTAdView alloc] initWithSize:RTAdBannerTop];
int check = [topBanner checkAdAvailability];
check could be 1 if there is an ad available,and 0 if not
#####B. Bottom Screen Banner
- Import the following file to your view controller
#import "RTAdView.h"
In ObjCCalls.mm , add this property
RTAdView *bottomBanner;
- Add the following line to your view controller
getScreenSize();
float widthOfAd;
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{widthOfAd = widthOfScreen*0.5;}
else
{widthOfAd = 320;}
float heightOfAd = widthOfAd*(75.0/320);
float xOfAd = (widthOfScreen-widthOfAd)/2;
float yOfAd = heightOfScreen-heightOfAd;
bottomBanner = [[RTAdView alloc] initWithSize:RTAdBannerBottom];
bottomBanner.frame = CGRectMake(xOfAd,yOfAd,widthOfAd,heightOfAd);
[[[[[UIApplication sharedApplication]keyWindow]subviews]objectAtIndex:0]addSubview:bottomBanner];
[[[[[UIApplication sharedApplication]keyWindow]subviews]objectAtIndex:0]bringSubviewToFront: bottomBanner];
[bottomBanner prepareAd];
[bottomBanner loadRequest];
[bottomBanner showAd];
Ad availability could be checked before adding the ad itself
Call the following function
bottomBanner = [[RTAdView alloc] initWithSize:RTAdBannerBottom];
int check = [bottomBanner checkAdAvailability];
check could be 1 if there is an ad available,and 0 if not
######Important notes: Note 1: Banner’s size and position are fixed, changing any will result in removing them from the view.
Note 2: "showAd" method could be called anytime after "loadRequest" even in another method.
Note 3: Hidding the adView is not allowed.
###2. Interstitial
######Before adding a Interstitial: In ObjCCalls.mm , add these 2 properties
float heightOfScreen;
float widthOfScreen;
add the following method (this method will give the current screen width and height using any iOS)
void getScreenSize ()
{
NSString *osVersion = [[UIDevice currentDevice] systemVersion];
float osVERSION = [osVersion floatValue];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
if (osVERSION >= 8)
{
heightOfScreen = screenHeight;
widthOfScreen = screenWidth;
}
else
{
UIInterfaceOrientation statusBarOrientation =[UIApplication sharedApplication].statusBarOrientation;
if (statusBarOrientation==4||statusBarOrientation==3)
{
heightOfScreen = screenWidth;
widthOfScreen = screenHeight;
}
else if (statusBarOrientation==1||statusBarOrientation==2)
{
heightOfScreen = screenHeight;
widthOfScreen = screenWidth;
}
}
}
- Import the following file to your view controller
#import "RTAdView.h"
In ObjCCalls.mm , add this property
RTAdView *ad;
- Add the following lines to prepare the Ad
getScreenSize();
ad= [[RTAdView alloc] initWithSize:RTAdPopUp];
ad.frame = CGRectMake(0,0,widthOfScreen,heightOfScreen);
[[[[[UIApplication sharedApplication]keyWindow]subviews]objectAtIndex:0]addSubview:ad];
[[[[[UIApplication sharedApplication]keyWindow]subviews]objectAtIndex:0]bringSubviewToFront:ad];
[ad prepareAd];
[ad loadRequest];
- Add the following lines to show the Ad
[ad showAd];
Ad availability could be checked before adding the ad itself
Call the following function
ad = [[RTAdView alloc] initWithSize:RTAdPopUp];
int check = [ad checkAdAvailability];
check could be 1 if there is an ad available, and 0 if not.
###3. Native
- Import the following file to your view controller
#import "RTAdView.h"
In ObjCCalls.mm , add this property
RTAdView * native;
- Add the following line to your view controller
native = [[RTAdView alloc] initWithSize:RTAdNative1to1];
native.frame = CGRectMake(100,400,300,100);
[[[UIApplication sharedApplication]keyWindow]addSubview:native];
[[[UIApplication sharedApplication]keyWindow]bringSubviewToFront:native];
[native prepareAd];
[native loadRequest];
[native showAd];
Ad availability could be checked before adding the ad itself
Call the following function
native = [[RTAdView alloc] initWithSize:RTAdNative];
int check = [native checkAdAvailability];
check could be 1 if there is an ad available, and 0 if not.
######Third line of code means that the ad will be on position (x=100,y=400) with size (width=300, height=100).
######Important Notes:
Note 1.Native ads can be placed anywhere INSIDE the screen. Placing the ad outside the boarder of the screen will result in deleteing the ad. (This also apply after device orientation)
###4. Audio
The Audio Ad will play an audio file.
Import the following file to your view controller
#import "RTAudioAd.h"
#import <AVFoundation/AVFoundation.h>
add this object
AVPlayerItem *playerItem;
add this to play an audio ad
RTAudio *audio = [[RTAudio alloc]initWithSize:RTAdAudio];
[audio playAudioAd];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerItem];
and this method is called after the audio ad finished playing
void playerItemDidReachEnd:(NSNotification *) notification {
NSLog(@"The Ad finished");
}
Ad availability could be checked before adding the ad itself
Call the following function
audio = [[RTAudio alloc] initWithSize:RTAdAudio];
int check = [audio checkAudioAvailability];
check could be 1 if there is an ad available, and 0 if not.
###5. Video
In the targeted viewController.m , add these 2 properties
float heightOfScreen;
float widthOfScreen;
and the following method (this method will give the current screen width and height using any iOS)
void getScreenSize()
{
NSString *osVersion = [[UIDevice currentDevice] systemVersion];
float osVERSION = [osVersion floatValue];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
if (osVERSION >= 8)
{
heightOfScreen = screenHeight;
widthOfScreen = screenWidth;
}
else
{
UIInterfaceOrientation statusBarOrientation =[UIApplication sharedApplication].statusBarOrientation;
if (statusBarOrientation==4||statusBarOrientation==3)
{
heightOfScreen = screenWidth;
widthOfScreen = screenHeight;
}
else if (statusBarOrientation==1||statusBarOrientation==2)
{
heightOfScreen = screenHeight;
widthOfScreen = screenWidth;
}
}
}
- Import the following file to your view controller
#import "RTVideo.h"
- In the ViewController.m Interface
@interface ViewController ()
@end
Add the following property
RTVideo *videoAd;
- Add the following lines to get video ad
getScreenSize();
videoAd = [[RTVideo alloc]initWithSize:RTAdVideo];
videoAd .frame = CGRectMake(0, 0, _widthOfScreen*2, _heightOfScreen*2);
[[[[[UIApplication sharedApplication]keyWindow]subviews]objectAtIndex:0]addSubview:videoAd];
[[[[[UIApplication sharedApplication]keyWindow]subviews]objectAtIndex:0]bringSubviewToFront:videoAd];
[videoAd prepareAd];
[videoAd playAd];
- Add this, which will be called after closing the ad.
[videoAd addObserveForKeyPath:@"frame" withBlock:^() {
NSLog(@"Video ad Closed");
}];
- Do not forget to add this after the ad is closed to remove the observer
[videoAd unobserveKeyPath:@"frame"];
Ad availability could be checked before adding the ad itself
Call the following function
videoAd = [[RTAudio alloc] initWithSize:RTAdVideo];
int check = [audio checkVideoAvailability];
check could be 1 if there is an ad available, and 0 if not.
###Push Notifications
** You can find the guide for generating certificates and integrating them with RedTroops on this link
- Import the following file to your app delegate AppDelegate.m
#import “RTNotificationManager.h"
- In your app delegate (AppDelegate.m), find the method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
and add the following line of code
application.applicationIconBadgeNumber = 0 ;
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
//iOS 8
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert |UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
} else {
//iOS < 8
[application registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound |UIUserNotificationTypeAlert)];
}
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey])
{
NSDictionary *payload = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
[[RTNotificationManager defaultManager]startProcessingNotificationPayload:payload];
}
*Note: This code snippet handles notifications for iOS 8 and earlier versions. if you are targeting iOS 8 only, you can add the first part of the first if statement.
- In your app delegate (AppDelegate.m), find the method
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
and add the following line of code
[[RTSessionManager defaultManager] registerDeviceToken:deviceToken];
- In your app delegate (AppDelegate.m), find the method
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
and add the following line of code
[UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue];
[[RTNotificationManager defaultManager]startProcessingNotificationPayload:userInfo];
- In your app delegate (AppDelegate.m), find the method
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
and add the following line of code
NSLog(@"4:%@", error.localizedDescription);
*Remember: After creating you application on redtroop.com there 2 options (Development and Production).
Development is when the app is still under development and not on the AppStore yet. The certificates on Apple Dev Center are still the development ones.
Production is when the application in on the AppStore. The certificates on Apple Dev Center must be the production ones.
Common Log Errors
Click here
If you need any help or for more information, please visit: RedTroops Docs