Unity: iOS SDK Integration - RedTroops/IOS-SDK GitHub Wiki

###Getting Started

Follow the steps in Unity SDK Integration before proceeding.

###Setting Up RedTroops SDK 3.2.1 In Your Project


Follow the steps below to get your RedTroops SDK 3.2.1 running:

  1. Download the SDK from RedTroops' website. (The SDK must be added to the exported xCode project, NOT the unity project).

  2. After downloading the SDK, drag & drop the files (RedTroops.a + include) to your xCode project.

  3. Add following frameworks:

  • SystemConfiguration.framework
  • QuartzCore.framework
  • CoreGraphics.framework
  • Security.framework
  • MobileCoreServices.framework
  • CoreTelephony.framework

###Initial Setup

  1. In your app delegate (AppDelegate.m), import RTSessionManager
    #import "RTSessionManager.h"
  1. 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"];
  1. 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"];
  1. In your app delegate (AppDelegate.m), find the method
    - (void)applicationWillTerminate:(UIApplication *)application

and add the following line of code

    [[RTSessionManager defaultManager] endTheSession];
  1. In your app delegate (AppDelegate.m), find the method
(void)applicationDidEnterBackground:(UIApplication *)application 

and add the following line of code

 [[RTSessionManager defaultManager] endTheSession];

Now if you try to build your xCode project, you will face some errors. The functions you declared in unity are missing, follow these steps to add them

  1. In the xCode project navigator, right click on the "Unity" folder, and click on New File...

  2. From the iOS Source choose C++ File then click next.

  3. Name the file "RedTroopsClass" and also make sure to create a header file.

#####Make sure to change the target file extension from (RedTroopsClass.cpp) to (RedTroopsClass.mm).

  1. Add this code to RedTroopsClass.h
#ifdef __cplusplus
extern "C" {
#endif
    
    int checkAd(int x)
    void  getAd(int y);

#ifdef __cplusplus
}
#endif

checkAd method will get int parameter a return int value. The parameter is for your own use, if you want to check for more than one ad type. The return value will return 1 if there is an ad, and 0 if there is no ad.

and add this code to RedTroopsClass.mm

#import "RTAdView.h"


float heightOfScreen;
float widthOfScreen;


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;
        }
    }
    
}
int checkAd(int x)
{
  
    int check;

    if(x==0)
    {
       RTAdView *ad =[[RTAdView alloc]initWithSize:RTAdBannerTop];
       check = [ad checkAdAvailability];
    }
    else if(x==1)
    {
       RTAdView *ad =[[RTAdView alloc]initWithSize:RTAdBannerBottom];
       check = [ad checkAdAvailability];
    }
    else if(x==2)
    {
       RTAdView *ad =[[RTAdView alloc]initWithSize:RTAdPopUp];
       check = [ad checkAdAvailability];
    }
    else if(x==3)
    {
       RTAdView *ad =[[RTAdView alloc]initWithSize:RTAdNative];
       check = [ad checkAdAvailability];
    }
    else if(x==4)
    {
       RTAudio *player = [[RTAudio alloc]init];
       check = [player checkAudioAvailability];
    }
    else if(x==5)
    {
       RTVideo *ad =[[RTVideo alloc]initWithSize:RTAdVideo];
       check = [ad checkVideoAvailability];
    }
    else 
    {
      check = 0;
    }



    if(check==1)
    {
        //Ad is available
        return 1;
    }
    else
    {
        //Ad is not available
        return 0;
    }
    
}

The above method will check if there is an Interstitial ad, and return 1 if so. This method applies on Interstitial, Banner, and Native Ads

void getAd(int y)
{
    if(x==0)
    {
       //add TopBanner as shown below
    }
    else if(x==1)
    {
       //add BottomBanner as shown below
    }
    else if(x==2)
    {
       //add Interstitial as shown below
    }
    else if(x==3)
    {
       //add Native as shown below
    }
    else if(x==4)
    {
       //add Audio as shown below
    }
    else if(x==5)
    {
       //add Video as shown below
    }
}

getAd method will get int parameter. The parameter is for your own use, if you want to check for more than one ad type.


####RedTroops offer 2 types of ads:

#####1. Banner ( Top Screen Banner - Bottom Screen Banner)

#####2. Interstitial

#####3. Native

#####4. Audio

#####5. Video


###1. Banner

#####A. Top Screen Banner

In RedTroopsClass.mm , add this property

RTAdView *topBanner;
  1. Add the following code in the getAd(int y)
    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];

#####B. Bottom Screen Banner

In RedTroopsClass.mm , add this property

RTAdView *bottomBanner;
  1. Add the following code to getAd(int y)
    getScreenSize();

    float widthOfAd;
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
    {widthOfAd = widthOfScreen*0.5;}
    else
    {widthOfAd = 320;}
    NSLog(@"%f",widthOfScreen);
    NSLog(@"%f",heightOfScreen);
    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];

######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

In RedTroopsClass.mm , add this property

RTAdView *ad;

Add the following code to getAd(int y)

    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];  
    [ad showAd];

Note: "showAd" could be called anytime after "loadRequest", even in another method.


###3. Native

In RedTroopsClass.mm , add this property

RTAdView *nativeAd;

Add the following code to getAd(int y)

    getScreenSize();
    nativeAd = [[RTAdView alloc] initWithSize:RTAdNative];
    nativeAd.frame = CGRectMake(100,100,200,200);
    [[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] addSubview: nativeAd];
    [[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] bringSubviewToFront: nativeAd];
    [nativeAd prepareAd];
    [nativeAd loadRequest];  
    [nativeAd showAd];

Note: "showAd" could be called anytime after "loadRequest", even in another method.


###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;
        }
    }
}
  1. Import the following file to your view controller
    #import "RTVideo.h"
  1. In the ViewController.m Interface
@interface ViewController ()

@end

Add the following property

   RTVideo *videoAd;
  1. Add the following lines to get video ad
    getScreenSize();
    self.videoAd = [[RTVideo alloc]initWithSize:RTAdVideo];
    self.videoAd .frame = CGRectMake(0, 0, _widthOfScreen*2, _heightOfScreen*2);
    [self.view addSubview:self.videoAd ];
    [[[[[UIApplication sharedApplication]keyWindow]subviews]objectAtIndex:0]addSubview:self.videoAd];
    [[[[[UIApplication sharedApplication]keyWindow]subviews]objectAtIndex:0]bringSubviewToFront:self.videoAd];

    [self.videoAd  prepareAd];
    [self.videoAd  playAd];

  1. Add this, which will be called after closing the ad.
    [videoAd addObserveForKeyPath:@"frame" withBlock:^() {
     
        NSLog(@"Video ad Closed");
        
     }];
  1. Do not forget to add this after the ad is closed to remove the observer
    [videoAd unobserveKeyPath:@"frame"];

###Push Notifications

** You can find the guide for generating certificates and integrating them with RedTroops on this link

  1. Import the following file to your app delegate AppDelegate.m
    #import “RTNotificationManager.h"
  1. 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.

  1. 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];
  1. 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];
  1. 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 underdevelopment and not on the AppStore yet. The certificates on Apple Dev Center are still the development ones.

Production is when the application is 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

⚠️ **GitHub.com Fallback** ⚠️