Skip to content

Instantly share code, notes, and snippets.

@Dirk-Sandberg
Last active March 6, 2023 12:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Dirk-Sandberg/e20854cc833b8a13708892f4ef0909b3 to your computer and use it in GitHub Desktop.
Save Dirk-Sandberg/e20854cc833b8a13708892f4ef0909b3 to your computer and use it in GitHub Desktop.
Snippet to add to main.m in kivy-ios project
UIView *gView;
UIViewController *gViewColtroller;
@interface myBanner : NSObject <GADBannerViewDelegate>
@property (nonatomic) BOOL show_ads;
@property (strong, nonatomic) GADBannerView *gbanner;
@property (strong, nonatomic) GADRequest *request;
@end
static myBanner *vbanner = nil;
@implementation myBanner
-(id)init {
// admob allocation
NSLog(@"Creating google banner object");
self.request = [GADRequest request];
// I'm not sure this is even necessary
self.request.testDevices = @[@"Simulator"];
//SDLUIKitDelegate *sdldelegate = [SDLUIKitDelegate sharedAppDelegate];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIViewController *rootViewController = window.rootViewController;
gViewColtroller = rootViewController;//[[SDLLaunchScreenController alloc] init];
gView = rootViewController.view; ///gViewColtroller.view;
// Create a view of the standard size at the top of the screen.
// Available AdSize constants are explained in GADAdSize.h.
self.gbanner = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
[self.gbanner setDelegate:self];
// Specify the ad's "unit identifier." The default ID is for Google’s test banner ad. If everything works and you see a google test ad, you have implemented everything correctly.
self.gbanner.adUnitID = @"ca-app-pub-3940256099942544/6300978111"; // google's test id for banner ads
// ------------- Height and position of the banner ad
//CGRect screenBounds = [[UIScreen mainScreen] bounds];
//[self.gbanner setFrame:CGRectMake(0, 0, screenBounds.size.width, 1.5*self.gbanner.bounds.size.height)];
//self.gbanner.center = CGPointMake(screenBounds.size.width / 2, screenBounds.size.height - (self.gbanner.bounds.size.height / 2));
self.gbanner.hidden = TRUE;
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
self.gbanner.rootViewController = gViewColtroller;
[gView addSubview:self.gbanner];
[self.gbanner loadRequest:self.request];
self.show_ads = TRUE;
return self;
}
// Called before ad is shown, good time to show the add
- (void)adViewDidReceiveAd:(GADBannerView *)view
{
NSLog(@"Admob load");
self.gbanner.hidden = !self.show_ads;
}
// An error occurred
- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error
{
NSLog(@"Admob error: %@", error);
self.gbanner.hidden = TRUE;
}
-(void)dealloc {
NSLog(@"Freeing ads");
if (self.gbanner) {
[self.gbanner removeFromSuperview];
[self.gbanner release];
self.gbanner.delegate = nil;
self.gbanner = nil;
}
[super dealloc];
}
- (void)showAds:(int)ontop {
self.show_ads = TRUE;
NSLog(@"Displaying banner object ontop:%d.", ontop);
CGSize AdSize = kGADAdSizeBanner.size;
CGRect frame = self.gbanner.frame;
frame.origin.x = (gViewColtroller.view.bounds.size.width - AdSize.width) / 2 ;
if (ontop)
frame.origin.y = 0.0f;
else
frame.origin.y = gViewColtroller.view.bounds.size.height - AdSize.height;
self.gbanner.frame = frame;
}
@end
@interface adSwitch : NSObject
@end
@implementation adSwitch
-(id)init {
if (!vbanner)
{
vbanner = [[myBanner alloc] init];
[vbanner showAds:0];
}
return self;
}
-(void) show_ads {
if (!vbanner)
vbanner = [[myBanner alloc] init];
[vbanner showAds:0];
}
-(void) hide_ads {
if (vbanner)
{
[vbanner release];
vbanner = nil;
}
}
@end
@Dirk-Sandberg
Copy link
Author

Dirk-Sandberg commented Apr 7, 2019

This code is NOT THE ENTIRE FILE. You need to copy/paste this code into the main.m file in your Xcode project. This code snippet can be placed directly after the imports in your main.m file.

@Dirk-Sandberg
Copy link
Author

This code snippet is part of the instructions to integrating AdMob with kivy-ios, which is documented in both a public Google Doc and a YouTube tutorial (by me).

@dbrova15
Copy link

How to integrate interstitial ads into an iOS app using kivy-ios?

@mbrockman1
Copy link

Do you know of a way to move the banner to the top of the screen? I am not getting any banner movement when I adjust line 41-43.

@Batuhank8931
Copy link

Dear Erik, It is a beautiful guideline. However, the Xcode has been changed a lot. The code that you have used in the video gives error in the first line. And Xcode Build Settings are different. Everything is for swift but not For Kivy. Please see this link; https://developers.google.com/admob/ios/quick-start#swift

could you tell me about the differences ? Because all I got is framework errors.

@mbrockman1
Copy link

mbrockman1 commented Jun 19, 2022

Easiest way by far. This guy created a cross talk between swift and python. It works amazing, and have deployed it on multiple apps. Can't recommend it enough.

https://discord.com/channels/913144015044636702/980507747038658640

@Batuhank8931
Copy link

Batuhank8931 commented Jun 19, 2022

Dear Michael, thank you for your promp reply. However the link that you have send is emty. Could you give me another information about that guy.

@mbrockman1
Copy link

It is the discord channel, KivySwiftLink. There is a thread 'Admob talk' which explains how to add ad support. Also a GUI implementation is nearing completion which is essentially a single click to add Admob.

https://github.com/psychowasp/KivySwiftLink

@Batuhank8931
Copy link

It is the discord channel, KivySwiftLink. There is a thread 'Admob talk' which explains how to add ad support. Also a GUI implementation is nearing completion which is essentially a single click to add Admob.

https://github.com/psychowasp/KivySwiftLink

Thank you very much. I have read most of them. I think I will figure it out.

@shirubei
Copy link

Dear Erik, It is a beautiful guideline. However, the Xcode has been changed a lot. The code that you have used in the video gives error in the first line. And Xcode Build Settings are different. Everything is for swift but not For Kivy. Please see this link; https://developers.google.com/admob/ios/quick-start#swift

could you tell me about the differences ? Because all I got is framework errors.

I've made it runnable by fork this snippet.

@Chemik-srem
Copy link

Bottom, Top - change line 99-102
if (ontop)
frame.origin.y = gViewColtroller.view.bounds.size.height - AdSize.height;
else
frame.origin.y = 0.0f;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment