Skip to content

Commit

Permalink
feat: added InterstitialAd #52
Browse files Browse the repository at this point in the history
  • Loading branch information
gumaciel committed Jul 30, 2023
1 parent 16f414a commit 0018ac9
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 14 deletions.
6 changes: 6 additions & 0 deletions PoingGodotAdMob/PoingGodotAdMob.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
46C7E77DD2ECA925F3952036 /* Pods_vungle.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C4ED341537F1C46ECD8226C4 /* Pods_vungle.framework */; };
5E027D892A74A770001551B6 /* PoingGodotAdMobInterstitialAd.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5E027D882A74A770001551B6 /* PoingGodotAdMobInterstitialAd.mm */; };
5E08983D2A4B91E30066A53A /* PoingGodotAdMobAdColonyAppOptions.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5E08983C2A4B91E30066A53A /* PoingGodotAdMobAdColonyAppOptions.mm */; };
5E38B5DB2A7546EF00E35218 /* InterstitialAd.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5E38B5DA2A7546EF00E35218 /* InterstitialAd.mm */; };
5E5BDE182A4632770071BE86 /* PoingGodotAdMobModule.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5E5BDE172A4632770071BE86 /* PoingGodotAdMobModule.mm */; };
5E5BDE3E2A4686B20071BE86 /* PoingGodotAdMobAdColonyModule.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5E5BDE3D2A4686B20071BE86 /* PoingGodotAdMobAdColonyModule.mm */; };
5E5BDE3F2A4686B20071BE86 /* PoingGodotAdMobAdColonyModule.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 5E5BDE3C2A4686B20071BE86 /* PoingGodotAdMobAdColonyModule.h */; };
Expand Down Expand Up @@ -97,6 +98,8 @@
5E08983B2A4B91C80066A53A /* PoingGodotAdMobAdColonyAppOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PoingGodotAdMobAdColonyAppOptions.h; sourceTree = "<group>"; };
5E08983C2A4B91E30066A53A /* PoingGodotAdMobAdColonyAppOptions.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = PoingGodotAdMobAdColonyAppOptions.mm; sourceTree = "<group>"; };
5E08983F2A4B95B50066A53A /* poing-godot-admob-adcolony.gdip */ = {isa = PBXFileReference; lastKnownFileType = text; path = "poing-godot-admob-adcolony.gdip"; sourceTree = "<group>"; };
5E38B5D92A7546D100E35218 /* InterstitialAd.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = InterstitialAd.h; sourceTree = "<group>"; };
5E38B5DA2A7546EF00E35218 /* InterstitialAd.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = InterstitialAd.mm; sourceTree = "<group>"; };
5E5BDE162A4632300071BE86 /* PoingGodotAdMobModule.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PoingGodotAdMobModule.h; sourceTree = "<group>"; };
5E5BDE172A4632770071BE86 /* PoingGodotAdMobModule.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = PoingGodotAdMobModule.mm; sourceTree = "<group>"; };
5E5BDE3A2A4686B20071BE86 /* poing-godot-admob-adcolony.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "poing-godot-admob-adcolony.a"; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -351,6 +354,8 @@
children = (
5EA276152A67396200D5CF1E /* Banner.h */,
5EA276162A6739D200D5CF1E /* Banner.mm */,
5E38B5D92A7546D100E35218 /* InterstitialAd.h */,
5E38B5DA2A7546EF00E35218 /* InterstitialAd.mm */,
);
path = adformats;
sourceTree = "<group>";
Expand Down Expand Up @@ -634,6 +639,7 @@
buildActionMask = 2147483647;
files = (
5E027D892A74A770001551B6 /* PoingGodotAdMobInterstitialAd.mm in Sources */,
5E38B5DB2A7546EF00E35218 /* InterstitialAd.mm in Sources */,
5EB759202A68860400523E88 /* DeviceOrientationHelper.mm in Sources */,
5E80AAD52A44A50C001C750F /* PoingGodotAdMob.mm in Sources */,
5EA2761A2A67461A00D5CF1E /* GodotDictionaryToObject.mm in Sources */,
Expand Down
12 changes: 2 additions & 10 deletions PoingGodotAdMob/src/ads/PoingGodotAdMob.mm
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,9 @@
GADRequestConfiguration *requestConfiguration = [GADMobileAds sharedInstance].requestConfiguration;

requestConfiguration.maxAdContentRating = [NSString stringWithUTF8String:maxAdContentRating.utf8().get_data()];
if (tagForChildDirectedTreatment == 1) {
[requestConfiguration tagForChildDirectedTreatment:true];
} else if (tagForChildDirectedTreatment == 0) {
[requestConfiguration tagForChildDirectedTreatment:false];
}
requestConfiguration.tagForChildDirectedTreatment = [NSNumber numberWithInt:tagForChildDirectedTreatment];

if (tagForUnderAgeOfConsent == 1){
[requestConfiguration tagForUnderAgeOfConsent: true];
} else if (tagForUnderAgeOfConsent == 0){
[requestConfiguration tagForUnderAgeOfConsent:false];
}
requestConfiguration.tagForUnderAgeOfConsent = [NSNumber numberWithInt:tagForUnderAgeOfConsent];

NSMutableArray<NSString *> *testDeviceIdsArray = [NSMutableArray arrayWithCapacity:testDeviceIds.size()];
for (String deviceId : testDeviceIds) {
Expand Down
12 changes: 9 additions & 3 deletions PoingGodotAdMob/src/ads/PoingGodotAdMobInterstitialAd.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,26 @@
#define PoingGodotAdMobInterstitialAd_h

#include "core/object/class_db.h"
#import "converters/GodotDictionaryToObject.h"
#import "adformats/InterstitialAd.h"
#include <vector>


@class InterstitialAd;

@import GoogleMobileAds;

class PoingGodotAdMobInterstitialAd : public Object {

GDCLASS(PoingGodotAdMobInterstitialAd, Object);

static PoingGodotAdMobInterstitialAd *instance;
static void _bind_methods();

public:
int create(Dictionary adViewDictionary);
void load(int uid, Dictionary adRequestDictionary, PackedStringArray keywords);
std::vector<InterstitialAd*> interstitialAds;

int create();
void load(String adUnitId, Dictionary adRequestDictionary, PackedStringArray keywords, int uid);
void show(int uid);
void destroy(int uid);

Expand Down
76 changes: 76 additions & 0 deletions PoingGodotAdMob/src/ads/PoingGodotAdMobInterstitialAd.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,79 @@
// SOFTWARE.

#import "PoingGodotAdMobInterstitialAd.h"

PoingGodotAdMobInterstitialAd *PoingGodotAdMobInterstitialAd::instance = NULL;

PoingGodotAdMobInterstitialAd::PoingGodotAdMobInterstitialAd() {
ERR_FAIL_COND(instance != NULL);

instance = this;
}

PoingGodotAdMobInterstitialAd::~PoingGodotAdMobInterstitialAd() {
if (instance == this) {
instance = NULL;
}
}

PoingGodotAdMobInterstitialAd *PoingGodotAdMobInterstitialAd::get_singleton() {
return instance;
};

int PoingGodotAdMobInterstitialAd::create() {
NSLog(@"create interstitialAd");

int uid = (int)interstitialAds.size();
interstitialAds.push_back(nullptr);

return uid;
}

void PoingGodotAdMobInterstitialAd::load(String adUnitId, Dictionary adRequestDictionary, PackedStringArray keywords, int uid) {
NSLog(@"load_ad interstitialAd");
GADRequest *adRequest = [GodotDictionaryToObject convertDictionaryToGADRequest:adRequestDictionary withKeywords:keywords];

InterstitialAd *interstitialAd = [[InterstitialAd alloc] initWithUID:uid];
[interstitialAd load:adRequest withAdUnitId:[NSString stringWithUTF8String:adUnitId.utf8().get_data()]];
}

void PoingGodotAdMobInterstitialAd::destroy(int uid) {
if (is_vector_interstitial_valid(uid)){
InterstitialAd* interstitialAd = interstitialAds.at(uid);
if (interstitialAd) {
interstitialAds.at(uid) = nullptr; //just set to null in order to try to clean up memory
}
}
}

void PoingGodotAdMobInterstitialAd::show(int uid) {
NSLog(@"show interstitialAd");
if (is_vector_interstitial_valid(uid)){
InterstitialAd* interstitialAd = interstitialAds.at(uid);
if (interstitialAd) {
[interstitialAd show];
}
}
}


void PoingGodotAdMobInterstitialAd::_bind_methods() {
ClassDB::bind_method(D_METHOD("create"), &PoingGodotAdMobInterstitialAd::create);
ClassDB::bind_method(D_METHOD("load"), &PoingGodotAdMobInterstitialAd::load);
ClassDB::bind_method(D_METHOD("show"), &PoingGodotAdMobInterstitialAd::show);
ClassDB::bind_method(D_METHOD("destroy"), &PoingGodotAdMobInterstitialAd::destroy);

ADD_SIGNAL(MethodInfo("on_interstitial_ad_failed_to_load", PropertyInfo(Variant::INT, "UID"), PropertyInfo(Variant::DICTIONARY, "loadAdErrorDictionary")));
ADD_SIGNAL(MethodInfo("on_interstitial_ad_loaded", PropertyInfo(Variant::INT, "UID")));

ADD_SIGNAL(MethodInfo("on_interstitial_ad_clicked", PropertyInfo(Variant::INT, "UID")));
ADD_SIGNAL(MethodInfo("on_interstitial_ad_dismissed_full_screen_content", PropertyInfo(Variant::INT, "UID")));
ADD_SIGNAL(MethodInfo("on_interstitial_ad_failed_to_show_full_screen_content", PropertyInfo(Variant::INT, "UID"), PropertyInfo(Variant::DICTIONARY, "adErrorDictionary")));
ADD_SIGNAL(MethodInfo("on_interstitial_ad_impression", PropertyInfo(Variant::INT, "UID")));
ADD_SIGNAL(MethodInfo("on_interstitial_ad_showed_full_screen_content", PropertyInfo(Variant::INT, "UID")));
};


bool PoingGodotAdMobInterstitialAd::is_vector_interstitial_valid(int uid){
return interstitialAds.size() > 0 && uid >= 0 && uid < interstitialAds.size() && interstitialAds.at(uid) != nullptr;
}
10 changes: 9 additions & 1 deletion PoingGodotAdMob/src/ads/PoingGodotAdMobModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,28 @@
#include "PoingGodotAdMob.h"
#include "PoingGodotAdMobAdSize.h"
#include "PoingGodotAdMobAdView.h"
#include "PoingGodotAdMobInterstitialAd.h"

#include "core/config/engine.h"
#include "core/version.h"

PoingGodotAdMob *poing_godot_admob;
PoingGodotAdMobAdSize *poing_godot_admob_ad_size;
PoingGodotAdMobAdView *poing_godot_admob_ad_view;
PoingGodotAdMobInterstitialAd *poing_godot_admob_interstitial_ad;

void register_poing_godot_admob_ads_types() {
poing_godot_admob = memnew(PoingGodotAdMob);
Engine::get_singleton()->add_singleton(Engine::Singleton("PoingGodotAdMob", poing_godot_admob));

poing_godot_admob_ad_size = memnew(PoingGodotAdMobAdSize);
Engine::get_singleton()->add_singleton(Engine::Singleton("PoingGodotAdMobAdSize", poing_godot_admob_ad_size));

poing_godot_admob_ad_view = memnew(PoingGodotAdMobAdView);
Engine::get_singleton()->add_singleton(Engine::Singleton("PoingGodotAdMobAdView", poing_godot_admob_ad_view));

poing_godot_admob_interstitial_ad = memnew(PoingGodotAdMobInterstitialAd);
Engine::get_singleton()->add_singleton(Engine::Singleton("PoingGodotAdMobInterstitialAd", poing_godot_admob_interstitial_ad));
}

void unregister_poing_godot_admob_ads_types() {
Expand All @@ -55,4 +60,7 @@ void unregister_poing_godot_admob_ads_types() {
if (poing_godot_admob_ad_view) {
memdelete(poing_godot_admob_ad_view);
}
if (poing_godot_admob_interstitial_ad) {
memdelete(poing_godot_admob_interstitial_ad);
}
}
45 changes: 45 additions & 0 deletions PoingGodotAdMob/src/ads/adformats/InterstitialAd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// MIT License
//
// Copyright (c) 2023-present Poing Studios
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#ifndef InterstitialAd_h
#define InterstitialAd_h

#import "../converters/GodotDictionaryToObject.h"
#import "../converters/ObjectToGodotDictionary.h"
#import "../PoingGodotAdMobInterstitialAd.h"
#import "view_controller.h"
#import "app_delegate.h"

@import GoogleMobileAds;

@interface InterstitialAd : ViewController <GADFullScreenContentDelegate>

@property(nonatomic, strong) GADInterstitialAd *interstitial;
@property (nonatomic, strong) NSNumber *UID;

- (instancetype)initWithUID:(int)UID;
- (void)load:(GADRequest *)request withAdUnitId:(NSString*) adUnitId;
- (void)show;

@end

#endif /* InterstitialAd_h */
88 changes: 88 additions & 0 deletions PoingGodotAdMob/src/ads/adformats/InterstitialAd.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// MIT License
//
// Copyright (c) 2023-present Poing Studios
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#import "InterstitialAd.h"

@implementation InterstitialAd
- (instancetype)initWithUID:(int)UID{
if ((self = [super init])) {
self.UID = [NSNumber numberWithInt:UID];
}
return self;
}

- (void)load:(GADRequest *)request withAdUnitId:(NSString*) adUnitId{
[GADInterstitialAd loadWithAdUnitID:adUnitId
request:request
completionHandler:^(GADInterstitialAd *ad, NSError *error) {
if (error) {
NSLog(@"sailed to load interstitial ad with error: %@", [error localizedDescription]);
PoingGodotAdMobInterstitialAd::get_singleton()->emit_signal("on_interstitial_ad_failed_to_load",
[self.UID intValue],
[ObjectToGodotDictionary convertNSErrorToDictionaryAsLoadAdError:error]);
return;
}
self.interstitial = ad;
self.interstitial.fullScreenContentDelegate = self;

PoingGodotAdMobInterstitialAd::get_singleton()->interstitialAds.at([self.UID intValue]) = self;
NSLog(@"success to load interstitial");
PoingGodotAdMobInterstitialAd::get_singleton()->emit_signal("on_interstitial_ad_loaded", [self.UID intValue]);
}];
}

- (void)show {
if (self.interstitial){
[self.interstitial presentFromRootViewController:[AppDelegate viewController]];
}
else{
NSLog(@"interstitial ad wasn't ready");
}
}


- (void)adDidRecordImpression:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"interstitial adDidRecordImpression.");
PoingGodotAdMobInterstitialAd::get_singleton()->emit_signal("on_interstitial_ad_impression", [self.UID intValue]);
}
- (void)adDidRecordClick:(nonnull id<GADFullScreenPresentingAd>)ad{
NSLog(@"interstitial adDidRecordClick.");
PoingGodotAdMobInterstitialAd::get_singleton()->emit_signal("on_interstitial_ad_clicked", [self.UID intValue]);
}
- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad
didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
NSLog(@"interstitial Ad did fail to present full screen content.");
PoingGodotAdMobInterstitialAd::get_singleton()->emit_signal("on_interstitial_ad_failed_to_show_full_screen_content",
[self.UID intValue],
[ObjectToGodotDictionary convertNSErrorToDictionaryAsAdError:error]);
}
- (void)adWillPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"interstitial Ad will present full screen content.");
PoingGodotAdMobInterstitialAd::get_singleton()->emit_signal("on_interstitial_ad_showed_full_screen_content",
[self.UID intValue]);
}
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"interstitial Ad did dismiss full screen content.");
PoingGodotAdMobInterstitialAd::get_singleton()->emit_signal("on_interstitial_ad_dismissed_full_screen_content", [self.UID intValue]);
}

@end
5 changes: 5 additions & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ env.Append(CPPPATH=[

# tweak this if you want to use different folders, or more folders, to store your source code in.
sources = Glob('PoingGodotAdMob/src/' + env['plugin'] + '/*.mm')
sources.append(Glob('PoingGodotAdMob/src/' + env['plugin'] + '/adformats/*.mm'))
sources.append(Glob('PoingGodotAdMob/src/' + env['plugin'] + '/config/*.mm'))
sources.append(Glob('PoingGodotAdMob/src/' + env['plugin'] + '/helpers/*.mm'))
sources.append(Glob('PoingGodotAdMob/src/' + env['plugin'] + '/converters/*.mm'))


# lib<plugin>.<arch>-<simulator|ios>.<release|debug|release_debug>.a
library_platform = env["arch"] + "-" + ("simulator" if env["simulator"] else "ios")
Expand Down

0 comments on commit 0018ac9

Please sign in to comment.