Skip to content

Commit

Permalink
#27 iOS Plugin ready for 3.3 version
Browse files Browse the repository at this point in the history
Former-commit-id: 50f6a91
  • Loading branch information
gumaciel committed Apr 8, 2021
1 parent 2eeec64 commit d0acf05
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 81 deletions.
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ ios/bin

*.a
*.o
*.d
*.d
*.hmap
*.yaml
*.dia
*.xcbuild
*.db
*.LinkFileList
*BuildDescriptionCacheIndex*
*.xcuserstate
*.dblite
6 changes: 3 additions & 3 deletions ios/SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ opts.Add(BoolVariable('simulator', "Compilation platform", 'no'))
opts.Add(BoolVariable('use_llvm', "Use the LLVM / Clang compiler", 'no'))
opts.Add(PathVariable('target_path', 'The path where the lib is installed.', 'bin/'))
opts.Add(EnumVariable('plugin', 'Plugin to build', '', ['', 'admob_plugin']))
opts.Add(EnumVariable('version', 'Godot version to target', '', ['', '3.2', '4.0']))
opts.Add(EnumVariable('version', 'Godot version to target', '', ['', '3.3', '4.0']))

# Updates the environment with the option variables.
opts.Update(env)
Expand Down Expand Up @@ -60,7 +60,7 @@ env.Append(CCFLAGS=["-fmodules", "-fcxx-modules"])
xcframework_directory = ''

if env['simulator']:
xcframework_directory = 'ios-arm64_i386_x86_64-simulator'
xcframework_directory = 'ios-arm64_x86_64-simulator'
sdk_name = 'iphonesimulator'
env.Append(CCFLAGS=['-mios-simulator-version-min=10.0'])
env.Append(LINKFLAGS=["-mios-simulator-version-min=10.0"])
Expand Down Expand Up @@ -99,7 +99,7 @@ env.Append(LINKFLAGS=["-arch", env['arch'], '-isysroot', sdk_path, '-F' + sdk_pa
if env['arch'] == 'armv7':
env.Prepend(CXXFLAGS=['-fno-aligned-allocation'])

if env['version'] == '3.2':
if env['version'] == '3.3':
env.Prepend(CFLAGS=['-std=gnu11'])
env.Prepend(CXXFLAGS=['-DGLES_ENABLED', '-std=gnu++14'])

Expand Down
4 changes: 2 additions & 2 deletions ios/admob_plugin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
"$(inherited)",
"\"$(SRCROOT)/lib/GoogleMobileAds.xcframework\"/**",
"\"$(SRCROOT)/lib/GoogleUtilities.xcframework\"/**",
"\"$(SRCROOT)/lib/GoogleAppMeasurement.framework\"",
"\"$(SRCROOT)/lib/GoogleAppMeasurement.xcframework\"/**",
"\"$(SRCROOT)/lib/nanopb.xcframework\"/**",
"\"$(SRCROOT)/lib/PromisesObjC.xcframework\"/**",
"\"$(SRCROOT)/lib/UserMessagingPlatform.xcframework\"/**",
Expand Down Expand Up @@ -328,7 +328,7 @@
"$(inherited)",
"\"$(SRCROOT)/lib/GoogleMobileAds.xcframework\"/**",
"\"$(SRCROOT)/lib/GoogleUtilities.xcframework\"/**",
"\"$(SRCROOT)/lib/GoogleAppMeasurement.framework\"",
"\"$(SRCROOT)/lib/GoogleAppMeasurement.xcframework\"/**",
"\"$(SRCROOT)/lib/nanopb.xcframework\"/**",
"\"$(SRCROOT)/lib/PromisesObjC.xcframework\"/**",
"\"$(SRCROOT)/lib/UserMessagingPlatform.xcframework\"/**",
Expand Down
2 changes: 1 addition & 1 deletion ios/admob_plugin/Banner.mm
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ - (void)adViewDidReceiveAd:(GADBannerView *)adView {

/// Tells the delegate an ad request failed.
- (void)adView:(GADBannerView *)adView
didFailToReceiveAdWithError:(GADRequestError *)error {
didFailToReceiveAdWithError:(NSError *)error {
NSLog(@"adView:didFailToReceiveAdWithError: %@", [error localizedDescription]);
Object *obj = ObjectDB::get_instance(instanceId);
obj->call_deferred("_on_AdMob_banner_failed_to_load", (int) error.code);
Expand Down
6 changes: 3 additions & 3 deletions ios/admob_plugin/Interstitial.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
//


#import <GoogleMobileAds/GADInterstitial.h>
#import <GoogleMobileAds/GADInterstitialAd.h>
#import <GoogleMobileAds/GADExtras.h>
#import "app_delegate.h"
#import "view_controller.h"
#include "object.h"

@class Interstitial;

@interface Interstitial: NSObject <GADInterstitialDelegate> {
GADInterstitial *interstitial;
@interface Interstitial: NSObject<GADFullScreenContentDelegate> {
GADInterstitialAd * interstitial;
bool initialized;
int instanceId;
NSString *adUnitId;
Expand Down
72 changes: 34 additions & 38 deletions ios/admob_plugin/Interstitial.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
@implementation Interstitial

- (void)dealloc {
interstitial.delegate = nil;
}

- (instancetype)init: (int)instance_id{
Expand All @@ -34,14 +33,29 @@ - (void) load_interstitial:(NSString*)ad_unit_id {
NSLog(@"%@", ad_unit_id);
}

interstitial = [[GADInterstitial alloc] initWithAdUnitID:ad_unit_id];
NSLog(@"interstitial created with the id");
NSLog(@"%@", ad_unit_id);

interstitial.delegate = self;

GADRequest *request = [GADRequest request];
[interstitial loadRequest:request];

[GADInterstitialAd loadWithAdUnitID:ad_unit_id
request:request
completionHandler:^(GADInterstitialAd *ad, NSError *error) {
if (error) {
NSLog(@"interstitial:didFailToReceiveAdWithError: %@", [error localizedDescription]);
Object *obj = ObjectDB::get_instance(self->instanceId);
obj->call_deferred("_on_AdMob_interstitial_failed_to_load", (int) error.code);

return;
}
else{
NSLog(@"interstitial created with the id");
NSLog(@"interstitialDidReceiveAd");
Object *obj = ObjectDB::get_instance(self->instanceId);
obj->call_deferred("_on_AdMob_interstitial_loaded");
}
self->interstitial = ad;
self->interstitial.fullScreenContentDelegate = self;

}];

}

Expand All @@ -50,54 +64,36 @@ - (void) show_interstitial {
return;
}

if (interstitial.isReady) {
if (interstitial) {
[interstitial presentFromRootViewController:rootController];
} else {
NSLog(@"Interstitial ad wasn't ready");
}
}


/// Tells the delegate an ad request succeeded.
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad {
NSLog(@"interstitialDidReceiveAd");
Object *obj = ObjectDB::get_instance(instanceId);
obj->call_deferred("_on_AdMob_interstitial_loaded");
}
/// Tells the delegate that the ad failed to present full screen content.
- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
NSLog(@"Ad did fail to present full screen content.");
Object *obj = ObjectDB::get_instance(self->instanceId);
obj->call_deferred("_on_AdMob_interstitial_failed_to_load", (int) error.code);

/// Tells the delegate an ad request failed.
- (void)interstitial:(GADInterstitial *)ad
didFailToReceiveAdWithError:(GADRequestError *)error {
NSLog(@"interstitial:didFailToReceiveAdWithError: %@", [error localizedDescription]);
Object *obj = ObjectDB::get_instance(instanceId);
obj->call_deferred("_on_AdMob_interstitial_failed_to_load", (int) error.code);
}

/// Tells the delegate that an interstitial will be presented.
- (void)interstitialWillPresentScreen:(GADInterstitial *)ad {
/// Tells the delegate that the ad presented full screen content.
- (void)adDidPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"interstitialWillPresentScreen");
Object *obj = ObjectDB::get_instance(instanceId);
obj->call_deferred("_on_AdMob_interstitial_opened");
}

/// Tells the delegate the interstitial is to be animated off the screen.
- (void)interstitialWillDismissScreen:(GADInterstitial *)ad {
NSLog(@"interstitialWillDismissScreen");
}
/// Tells the delegate that the ad dismissed full screen content.
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"Ad did dismiss full screen content.");
Object *obj = ObjectDB::get_instance(instanceId);
obj->call_deferred("_on_AdMob_interstitial_closed");

/// Tells the delegate the interstitial had been animated off the screen.
- (void)interstitialDidDismissScreen:(GADInterstitial *)ad {
NSLog(@"interstitialDidDismissScreen");
Object *obj = ObjectDB::get_instance(instanceId);
obj->call_deferred("_on_AdMob_interstitial_closed");
}

/// Tells the delegate that a user click will open another app
/// (such as the App Store), backgrounding the current app.
- (void)interstitialWillLeaveApplication:(GADInterstitial *)ad {
NSLog(@"interstitialWillLeaveApplication");
Object *obj = ObjectDB::get_instance(instanceId);
obj->call_deferred("_on_AdMob_interstitial_left_application");
}

@end
2 changes: 1 addition & 1 deletion ios/admob_plugin/Rewarded.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@class Rewarded;


@interface Rewarded: NSObject <GADRewardedAdDelegate> {
@interface Rewarded: NSObject <GADFullScreenContentDelegate> {
GADRewardedAd *rewarded;
bool initialized;
int instanceId;
Expand Down
73 changes: 41 additions & 32 deletions ios/admob_plugin/Rewarded.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,30 @@ - (void) load_rewarded:(NSString*) ad_unit_id {
NSLog(@"rewarded will load with the id");
NSLog(@"%@", ad_unit_id);
}

rewarded = [[GADRewardedAd alloc] initWithAdUnitID:ad_unit_id];
NSLog(@"rewarded created with the id");
NSLog(@"%@", ad_unit_id);


GADRequest *request = [GADRequest request];

[rewarded loadRequest:request completionHandler:^(GADRequestError * _Nullable error) {
if (error) {
[GADRewardedAd
loadWithAdUnitID:ad_unit_id
request:request
completionHandler:^(GADRewardedAd *ad, NSError *error) {
if (error) {
NSLog(@"Rewarded ad failed to load with error: %@", [error localizedDescription]);
NSLog(@"error while creating reward");
Object *obj = ObjectDB::get_instance(self->instanceId);
obj->call_deferred("_on_AdMob_rewarded_ad_failed_to_show", (int) error.code);
} else {
NSLog(@"reward successfully loaded");
Object *obj = ObjectDB::get_instance(self->instanceId);
obj->call_deferred("_on_AdMob_rewarded_ad_loaded");

return;
}
else{
NSLog(@"reward successfully loaded");
Object *obj = ObjectDB::get_instance(self->instanceId);
obj->call_deferred("_on_AdMob_rewarded_ad_loaded");

}
self->rewarded = ad;
self->rewarded.fullScreenContentDelegate = self;
}
}];
];

}

Expand All @@ -55,8 +61,18 @@ - (void) show_rewarded {
return;
}

if (rewarded.isReady) {
[rewarded presentFromRootViewController:rootController delegate:self];
if (rewarded) {
[rewarded presentFromRootViewController:rootController userDidEarnRewardHandler:^{
GADAdReward *rewardAd = self->rewarded.adReward;
NSLog(@"rewardedAd:userDidEarnReward:");
NSString *rewardMessage = [NSString stringWithFormat:@"Reward received with currency %@ , amount %lf",
rewardAd.type, [rewardAd.amount doubleValue]];
NSLog(@"%@", rewardMessage);
Object *obj = ObjectDB::get_instance(self->instanceId);
obj->call_deferred("_on_AdMob_user_earned_rewarded", [rewardAd.type UTF8String], rewardAd.amount.doubleValue);

}];

Object *obj = ObjectDB::get_instance(instanceId);
obj->call_deferred("_on_AdMob_rewarded_ad_opened");
} else {
Expand All @@ -65,29 +81,22 @@ - (void) show_rewarded {
}


/// Tells the delegate that the user earned a reward.
- (void)rewardedAd:(GADRewardedAd *)rewardedAd userDidEarnReward:(GADAdReward *)reward {
// TODO: Reward the user.
NSLog(@"rewardedAd:userDidEarnReward:");
NSString *rewardMessage = [NSString stringWithFormat:@"Reward received with currency %@ , amount %lf",
reward.type, [reward.amount doubleValue]];
NSLog(@"%@", rewardMessage);
Object *obj = ObjectDB::get_instance(instanceId);
obj->call_deferred("_on_AdMob_user_earned_rewarded", [reward.type UTF8String], reward.amount.doubleValue);
}
/// Tells the delegate that the rewarded ad failed to present.
- (void)rewardedAd:(GADRewardedAd *)rewardedAd didFailToPresentWithError:(NSError *)error {
/// Tells the delegate that the ad failed to present full screen content.
- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
NSLog(@"rewardedAd:didFailToPresentWithError");
Object *obj = ObjectDB::get_instance(instanceId);
obj->call_deferred("_on_AdMob_rewarded_ad_failed_to_show", (int) error.code);

}

/// Tells the delegate that the rewarded ad was dismissed.
- (void)rewardedAdDidDismiss:(GADRewardedAd *)rewardedAd {
NSLog(@"rewardedAdDidDismiss:");
Object *obj = ObjectDB::get_instance(instanceId);
obj->call_deferred("_on_AdMob_rewarded_ad_closed");

/// Tells the delegate that the ad dismissed full screen content.
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"Ad did dismiss full screen content.");
Object *obj = ObjectDB::get_instance(instanceId);
obj->call_deferred("_on_AdMob_rewarded_ad_closed");
}



@end

0 comments on commit d0acf05

Please sign in to comment.