I remembered today that I promised I would share the code required to add ads into your game. The reason why it took me so long to get to it was because I stopped coding for school, I just didn’t have time. But I know you don’t want to hear my excuses. The ad company I’ve worked with is Chartboost, any other company you will have to write the code yourself… sorry and this is only for the people that have gotten as far as to export their game into xCode and have been working on it from there.
- Make an account with Chartboost chartboost.com
- After you have made an account, it will walk you through what how to add campaigns if I recall correctly, but if it doesn’t, make an app in your dashboard and fill out its information
- The add a publishing campaign, if it doesn’t allow you to, I highly recommend that you fill out the tax and billing information because that will restrict you from creating more campaigns.
- For the publishing campaign, fill out the correct info, Static interstitial is a quick pop-up, video interstitial is a, well, video, and a playable interstitial is an interactive playable ad
- Once you have created the campaign, you will need to go to xCode > “Your app name” > Addons, and add the two files given below
- Go to your AppDelegate.mm and go to the top above where it says
@implementation AppDelegate
and type in the following code:
#import "CBAds.h"
#import <ChartBoost/ChartBoost.h>
@interface AppDelegate ()<ChartboostDelegate>
@end
- Now go inside the first function
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
under whereself.viewController
is instantiated and type in the following code:
[self.viewController registerAddon:[CBAds sharedInstance]];
[Chartboost startWithAppId:@"Your App ID"
appSignature:@"Your App Signature"
delegate:self];
- You will have to go back to your Chartboost account to retrieve the app ID and app signature, and replace in the respective locations.
- While you are on your account, when you made your app, if it doesn’t have a valid iTunes URL (which means you haven’t made your app on iTunes Connect) set Test Mode to enabled
- Also what you will need to do is download the Chartboost framework https://answers.chartboost.com/en-us/articles/download
- Once you have downloaded it, take the framework and drag it into xCode > Frameworks
- Now that we are back in xCode, make sure you have put in your app’s app ID and Signature in the right spots, the go to the bottom of AppDelegate.mm right above
@end
and paste the following code:
- (void)didDismissInterstitial:(NSString *)location {
NSLog(@"Interstitial dismissed");
StandaloneCodeaViewController *currentCodea = self.viewController;
if (currentCodea.paused) {
currentCodea.paused = NO;
}
[CBAds sharedInstance].isPlaying = NO;
}
- (void) didDisplayInterstitial:(NSString *)location {
StandaloneCodeaViewController *currentCodea = self.viewController;
currentCodea.paused = YES;
[CBAds sharedInstance].isPlaying = YES;
}
- (void) didFailToLoadInterstitial:(CBLocation)location withError:(CBLoadError)error {
const char *errorText = nullptr;
switch(error){
case CBLoadErrorInternal: {
errorText = "Failed to load, internal error.";
} break;
case CBLoadErrorInternetUnavailable: {
errorText = "Failed to load, no Internet connection.";
} break;
case CBLoadErrorTooManyConnections: {
errorText = "Failed to load, too many connections.";
} break;
case CBLoadErrorWrongOrientation: {
errorText = "Failed to load, wrong orientation.";
} break;
case CBLoadErrorFirstSessionInterstitialsDisabled: {
errorText = "Failed to load, first session.";
} break;
case CBLoadErrorNetworkFailure: {
errorText = "Failed to load, network error.";
} break;
case CBLoadErrorNoAdFound : {
errorText = "Failed to load, no ad found.";
} break;
case CBLoadErrorSessionNotStarted : {
errorText = "Failed to load, session not started.";
} break;
case CBLoadErrorImpressionAlreadyVisible : {
errorText = "Failed to load, impression already visible.";
} break;
case CBLoadErrorUserCancellation : {
errorText = "Failed to load, impression cancelled.";
} break;
case CBLoadErrorNoLocationFound : {
errorText="Failed to load Interstitial, missing location parameter.";
} break;
case CBLoadErrorAssetDownloadFailure : {
errorText = "Failed to load, asset download failed.";
} break;
case CBLoadErrorPrefetchingIncomplete : {
errorText = "Failed to load, prefetching of video content is incomplete !";
} break;
default: {
errorText = "Failed to load, unknown error.";
}
}
NSString *OBJCError=[[NSString alloc] initWithUTF8String:errorText];
[CBAds sharedInstance].errorText=OBJCError;
[CBAds sharedInstance].errorReturned=YES;
NSLog(@"Error Returned Bool %d",[CBAds sharedInstance].errorReturned);
NSLog(@"%@",OBJCError);
}
- Now you have officially finished the Objective-C stuff, and now we can move onto the Lua implementation!