Admob Interstitial isReady is False - ios

I am trying to implement Admob Interstitial ads on ios.
Here is what I have so far, this is my first time ever touching objective-c so please be kind.
// Simple Admob Interstitial support for Monkey - IOS
// admobInterstitial.ios.h
#import "GADInterstitial.h"
class AdmobInterstitial {
// the kind of "singleton"
static AdmobInterstitial *_admob;
// the ad
GADInterstitial *_interstitialAd;
// ad Unit ID
NSString *adUnitId;
public:
AdmobInterstitial();
// creates an instance of the object and start the thread
static AdmobInterstitial *GetAdmobInterstitial(String adUnitId);
// displays the ad to the user if it is ready
void ShowAd();
};
// admobInterstitial.ios.cpp
AdmobInterstitial *AdmobInterstitial::_admob;
AdmobInterstitial::AdmobInterstitial():_interstitialAd(0) {
}
AdmobInterstitial *AdmobInterstitial::GetAdmobInterstitial(String adUnitId) {
if( !_admob ) _admob=new AdmobInterstitial();
_admob->adUnitId = adUnitId.ToNSString();
return _admob;
}
void AdmobInterstitial::ShowAd() {
// create ad (should this go here or earlier?)
_interstitialAd = [[GADInterstitial alloc] init];
if (_interstitialAd) {
_interstitialAd.adUnitID = adUnitId;
[_interstitialAd loadRequest:[GADRequest request]];
if (_interstitialAd.isReady) {
BBMonkeyAppDelegate *appDelegate=(BBMonkeyAppDelegate*)[[UIApplication sharedApplication] delegate];
UIViewController *rootViewController = appDelegate->viewController;
[_interstitialAd presentFromRootViewController:rootViewController];
}
}
}
I am calling ShowAd() in my game after a player dies and clicks the restart button.
Currently, _interstitialAd.isReady is not coming back as true.
This is the documentation I used to get started
https://developers.google.com/mobile-ads-sdk/docs/admob/advanced#ios
It says that "You may invoke loadRequest: at any time, however, you must wait for GADInterstitialDelegate's interstitialDidReceiveAd: to be called before displaying the creative."
I assume this is the problem I am running into. I think I am calling loadRequest before interstitialDidReceiveAd. However, the document doesn't show an example of how I would wait for this method to be called.
Can anyone help with this?
EDIT: Now works and shows the Ad the 1st time I call ShowAd(), however doesn't show the ad any time after the 1st time this function is called
// Simple Admob Interstitial support for Monkey - IOS
// admobInterstitial.ios.h
#import "GADInterstitial.h"
class AdmobInterstitial {
// the kind of "singleton"
static AdmobInterstitial *_admob;
// the ad
GADInterstitial *_interstitialAd;
// ad Unit ID
NSString *adUnitId;
void loadAd();
public:
AdmobInterstitial();
// creates an instance of the object and start the thread
static AdmobInterstitial *GetAdmobInterstitial(String adUnitId);
// displays the ad to the user if it is ready
void ShowAd();
};
// admobInterstitial.ios.cpp
AdmobInterstitial *AdmobInterstitial::_admob;
AdmobInterstitial::AdmobInterstitial():_interstitialAd(0) {
}
AdmobInterstitial *AdmobInterstitial::GetAdmobInterstitial(String adUnitId) {
if( !_admob ) _admob=new AdmobInterstitial();
_admob->adUnitId = adUnitId.ToNSString();
_admob->loadAd();
return _admob;
}
void AdmobInterstitial::loadAd() {
// testing
_interstitialAd = [[GADInterstitial alloc] init];
if (_interstitialAd) {
_interstitialAd.adUnitID = adUnitId;
[_interstitialAd loadRequest:[GADRequest request]];
}
// end testing
}
void AdmobInterstitial::ShowAd() {
// create ad (should this go here or earlier?)
//_interstitialAd = [[GADInterstitial alloc] init];
if (_interstitialAd) {
//_interstitialAd.adUnitID = adUnitId;
//[_interstitialAd loadRequest:[GADRequest request]];
if (_interstitialAd.isReady) {
BBMonkeyAppDelegate *appDelegate=(BBMonkeyAppDelegate*)[[UIApplication sharedApplication] delegate];
UIViewController *rootViewController = appDelegate->viewController;
[_interstitialAd presentFromRootViewController:rootViewController];
}
}
}

OK. Firstly isAdReady==false will occur when there is no ad available to show. Ie it is a natural condition. You can mitigate it by using mediation.
But in your case it is likely that no ad was available to show because there had no been enough time to send the ad request and receive a response before asking that question.
What you need to do is to call loadAd early. Ie when you game first starts. Then at a natural break point in your game (after the player dies) check is isAdReady is true, and if so show the ad.
When you show the ad or when the game starts up again, call loadAd again so that you will have an ad ready again the next time you want to show it.

Related

PKPaymentAuthorizationController delegate does not called

I have NSObject class in that i need to show apple pay. So I am using PKPaymentAuthorizationController instead of PKPaymentAuthorizationViewController.
Apple pay sheet is displayed but delegate method does not called. How should i set delegate to my controller. My iOS version is above 11
Here is my code.
if ([PKPaymentAuthorizationController canMakePaymentsUsingNetworks:#[PKPaymentNetworkVisa, PKPaymentNetworkMasterCard, PKPaymentNetworkAmex]]) {
NSLog(#"Can make apple pay payment");
PKPaymentRequest *paymentRequest = [self paymentRequest:#"10"];
paymentContr = [[PKPaymentAuthorizationController alloc] initWithPaymentRequest:paymentRequest];
paymentContr.delegate = self; // here self is MyClass: NSObject
[paymentContr presentWithCompletion:^(BOOL success) {
NSLog(#"present");
}];
}

Swift 3.0 GameCenter matchmaking keeps cancelling after matching (issue converting objective-c to swift)

I'm following the RayWenderlich GameCenter Tutorial: Raywend GameCenter Tutorial but I'm trying to convert it into Swift 3.0. Basically all it does is authenticate the local player, open the matchmaking interface, and then it should call the matchmakerViewController function once two players find each other. The player is successfully authenticated, and the matchmakerViewController is presented, and the players find each-other. However, after displaying the opponent that it found and putting the blue checkmark next to their name, the app calls matchmakerViewControllerWasCancelled instead of matchmakerViewController(viewController: GKMatchmakerViewController,didFindMatch match: GKMatch) This is weird to me since I'm pretty sure that method is only called based on user interaction (it's not calling the error method). Running the objective-c version of the project works, but my swift conversion has this problem. Here are the two versions of the findMatchWithMinPlayers() method. I'm assuming I converted something wrong here:
Swift
func findMatchWithMinPlayers(minPlayers: Int, maxPlayers: Int, viewController: UIViewController, delegate: GameKitHelperDelegate) {
if !enableGameCenter {
return
}
self.matchStarted = false
self.match = nil
self.delegate = delegate
viewController.dismiss(animated: false, completion: { _ in })
var request = GKMatchRequest()
request.minPlayers = minPlayers
request.maxPlayers = maxPlayers
var mmvc = GKMatchmakerViewController(matchRequest: request)
mmvc?.matchmakerDelegate = self
viewController.present(mmvc!, animated: true, completion: { _ in })
}
Objective-C
- (void)findMatchWithMinPlayers:(int)minPlayers maxPlayers:(int)maxPlayers
viewController:(UIViewController *)viewController
delegate:(id<GameKitHelperDelegate>)delegate {
if (!_enableGameCenter) return;
_matchStarted = NO;
self.match = nil;
_delegate = delegate;
[viewController dismissViewControllerAnimated:NO completion:nil];
GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = minPlayers;
request.maxPlayers = maxPlayers;
GKMatchmakerViewController *mmvc =
[[GKMatchmakerViewController alloc] initWithMatchRequest:request];
mmvc.matchmakerDelegate = self;
[viewController presentViewController:mmvc animated:YES completion:nil];
}
The only other thing I can thing of that may have been different was this:
Objective-C
+ (instancetype)sharedGameKitHelper
{
static GameKitHelper *sharedGameKitHelper;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedGameKitHelper = [[GameKitHelper alloc] init];
});
return sharedGameKitHelper;
}
Swift
And instead I just used a global var called sharedGameKitHelper and anytime in the code where they used [GameKitHelper sharedGameKitHelper] I used sharedGameKitHelper. They described the purpose of that method as creating a "singleton object" of the GameKitHelper class.
If you find nothing wrong with those, here's a full Pastebin of both of my "GameKitHelper" classes, which contain all the GameKit code.
objectice-c swift I've checked every method multiple times and restarted the project from scratch, so I must have some hole in my knowledge of objective-c conversions. Thanks for any help you can give!

Admob bannerView Request Error: No ad to show

I'm having this issue on one of my ads for an ios game
Here is my code, the odd thing is that if I add the device on the request.testDevices list it displays the demo banner, if I remove from testDevices, it does not show a real banner, but if I change my bundleIdentifier on XCODE, it shows a real banner,so I believe its something with my admob account, does anyone ever got something like it?
Its always failing with this error:
AdView didFailToReceiveAdWithError --------------------------- : Error Domain=com.google.ads Code=1 "Request Error: No ad to show." UserInfo={NSLocalizedDescription=Request Error: No ad to show., NSLocalizedFailureReason=Request Error: No ad to show.}
On my AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Use Firebase library to configure APIs
[FIRApp configure];
[[FIRAnalyticsConfiguration sharedInstance] setAnalyticsCollectionEnabled:YES];
// Initialize Google Mobile Ads SDK
[GADMobileAds configureWithApplicationID:#"ca-app-pub-xx~xx"];
/* other stuff here... */
}
on my rootViewController.m
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
bannerViewAdded = NO;
interstitialViewAdded = NO;
[self addBanner];
// ..... more stuff here;
}
- (void)addBanner{
NSLog(#"CALL ADD BANNER ROOTVIEWCONTROLLER");
if(!bannerViewAdded && ![MKStoreManager isFeaturePurchased:kFeatureAId]){
NSLog(#"ADD BANNER ROOTVIEWCONTROLLER");
CGSize size = [[CCDirector sharedDirector] winSize];
// Create adMob ad View (note the use of various macros to detect device)
if (IS_IPAD || IS_IPADHD) {
bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeLeaderboard];
bannerView.center = CGPointMake(size.width/2, (size.height-CGRectGetHeight(bannerView.frame)/2)-2);
}
else if (IS_IPHONE6) {
bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
bannerView.center = CGPointMake(size.width/2, (size.height-CGRectGetHeight(bannerView.frame)/2)-2);
}
else if (IS_IPHONE6P) {
bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
bannerView.center = CGPointMake(size.width/2, (size.height-CGRectGetHeight(bannerView.frame)/2)-2);
}
else {
// boring old iPhones and iPod touches
bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
bannerView.center = CGPointMake(size.width/2, (size.height-CGRectGetHeight(bannerView.frame)/2)-2);
}
//[bannerView setBackgroundColor:[UIColor blueColor]];
// Need to set this to no since we're creating this custom view.
//bannerView.translatesAutoresizingMaskIntoConstraints = NO;
// Note: Edit SampleConstants.h to provide a definition for kSampleAdUnitID
// before compiling.
// Replace this ad unit ID with your own ad unit ID.
bannerView.adUnitID = #"ca-app-pub-xx/xx";
bannerView.rootViewController = self;
bannerView.delegate = self;
[self.view addSubview:bannerView];
GADRequest *request = [GADRequest request];
//request.testDevices = #[ kGADSimulatorID ];
//request.testDevices = #[ #"xx", #"xx" , kGADSimulatorID ];
[bannerView loadRequest:request];
bannerViewAdded = YES;
}
}
- (void)removeBanner {
//admob
if(bannerViewAdded){
bannerViewAdded = NO;
[bannerView removeFromSuperview];
[bannerView release];
bannerView = nil;
}
//No AdMOB
if(localBannerAdded){
localBannerAdded = NO;
[localBannerButton removeFromSuperview];
[localBannerButton release];
localBannerButton = nil;
}
}
- (void)addInterstitial{
if(!interstitialViewAdded && ![MKStoreManager isFeaturePurchased:kFeatureAId]){
NSLog(#"INIT INTERSTITIAL ROOTVIEWCONTROLLER");
interstitialView = [[GADInterstitial alloc] initWithAdUnitID:#"ca-app-pub-xx/xx"];
GADRequest *request = [GADRequest request];
// Requests test ads on devices you specify. Your test device ID is printed to the console when
// an ad request is made. GADBannerView automatically returns test ads when running on a
// simulator.
//request.testDevices = #[ kGADSimulatorID, #"xxx", #"xxx" ];
[interstitialView loadRequest:request];
[interstitialView setDelegate:self];
}
}
- (void)adView:(GADBannerView *)gadBannerView didFailToReceiveAdWithError:(GADRequestError *)error{
NSLog(#"AdView didFailToReceiveAdWithError --------------------------- : %#", error);
[self removeBanner];
if(!localBannerAdded){
CGSize size = [[CCDirector sharedDirector] winSize];
localBannerButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
localBannerButton.frame = CGRectMake(0.0, 0.0, 320.0, 50.0);
[localBannerButton setTitle:#"DOWNLOAD MORE FREE GAMES" forState:UIControlStateNormal];
localBannerButton.backgroundColor = [UIColor whiteColor];//[UIColor clearColor];
[localBannerButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal ];
[self.view addSubview:localBannerButton];
[localBannerButton setCenter:CGPointMake(self.view.center.x,(size.height-CGRectGetHeight(localBannerButton.frame)/2)-2)];
// Add Target-Action Pair
[localBannerButton addTarget:self action:#selector(openAppStore:) forControlEvents:UIControlEventTouchUpInside];
localBannerAdded = YES;
}
}
I just had this error today, the problem for me was simple, it was because the adUnitID is basically still new. I had to wait more than 2 hours after creating the adUnitID in order for the ads to be served.
If you have this error, and some of your adUnitIDs serve ads and some don't. You're highly likely having the same issue, and the only way to fix it is to wait.
The ad server will return this message mainly because of the following:
There are no ads for your ad unit id.
Check whether your ad unit id is proper or not.
You will get the same error if either your banner width/height is 0.
Make sure that your adUnitID is perfect.
or
check following links its may help you
https://groups.google.com/forum/#!topic/google-admob-ads-sdk/ioXU2nX9W28
AdMob Legacy Publisher ID not showing ads
I had just created a new account and seen that problem. When checking my account there was a message shown on the top of admob page: "Your ad units are not displaying ads because you haven't provided your account payments information yet.". Click on the button Fix it, fill the form and the ads will be shown within few hours
I met this error too. Both my banner and interstitial ads failed with this error. I found that it is my mistake to change UserAgent globally, after I change UserAgent to default it works well.
Make sure you are using the test ad IDs when in a debug session.
https://developers.google.com/admob/ios/banner?hl=en-US
The easiest way to load test ads is to use our dedicated test ad unit ID for iOS banners: ca-app-pub-3940256099942544/2934735716
This error could happen if you have not setup your payment and billing information in your AdMob account.
As per Admob after you setup the payment details, it can take up to 2 hours before it would be completely functional
Billing and Payments
This solved my problem
changing banner id to example id.
run the app then .
changing id back to production id.
For me, the reason why it started showing this is that I didn't have any payment methods set up on my AdMob account.
Once I have set this up, the error has disappeared and ads started showing instantly.
Hope this helps someone!
One possible reason is a Constraint bug.
you should guarantee your bannerView's constraint to other views is
right
I had the same issue when testing the sample application on my phone. I fixed with the following steps:
Reset Advertising identifier on iPhone 6, Go to:
Settings -> Privacy -> Advertising -> Reset Ad identifier
After create adUnitID, Wait for 1-2 hours and then try it out.
Because I had the same issue and it gets solved just after 1-2 hours.
Enjoy coding :)
If you are using your app in debug or testing mode make sure you are using test AppID and BannerID provided by google admob which would be as:-
Google Test AdMobID:
ca-app-pub-3940256099942544~1458002511
Google Test BannerID
ca-app-pub-3940256099942544/2934735716
In my case, ads are working fine but suddenly it stop showing ad with error: No ad to show.
I noticed console is throwing error about test device identifier. Somehow the test device identifier get changed. I don't know how it happen, may be due to Xcode upgrade. I am not sure. But once I changed the test device identifier, it start working again.
Do not make any modifications in code if this error pops, just wait about 3-4 days. Google takes time to send adds for new accounts
Make sure not to make an other variable of GADBannerView. Use one variable in every controller by making it global and just change unit id.
e.g this line should be out of any controller (global)
var bannerView: GADBannerView!
then use it wherever you want and just change id accordingly
bannerView.adUnitID = "ca-app-exampleid-abcabc938-4e8"
I was used to init the banner like this:
adBannerView = GADBannerView()
and it was working.
I can't say precisely from which AdMob library version, but suddently the banner wasn't show anymore.
Init the banner with the size as parameter, fixed the problem:
adBannerView = GADBannerView(adSize: kGADAdSizeBanner)
I solve this by adding request.testDevice = #[#"xxxxxxxxxxxx"] before load request.(I get this tip and test device number from console)
The best way I think should be used the test ad Ids from the integration guide from Google, after that change to your Ad Ids when it goes live.
I faced with this issue even with google test ad units.
In my scenario, problem was, I modified the WkWebView UserAgent directly without concatenating with the real one.
I had to change from;
static func arrangeUserAgent() {
let webView = WKWebView()
webView.evaluateJavaScript("navigator.userAgent") { (result, error) in
webView.customUserAgent = "Some Awesome User Agent"
let dictionary = Dictionary(dictionaryLiteral: ("UserAgent", webView.customUserAgent!))
UserDefaults.standard.register(defaults: dictionary)
}
}
to;
static func arrangeUserAgent() {
let webView = WKWebView()
webView.evaluateJavaScript("navigator.userAgent") { (result, error) in
let mergedUserAgent = "\(result ?? "") Some Awesome UserAgent"
webView.customUserAgent = mergedUserAgent
let dictionary = Dictionary(dictionaryLiteral: ("UserAgent", mergedUserAgent))
UserDefaults.standard.register(defaults: dictionary)
}
}
Had rough times to find the problem.
Be careful with the UserAgent you change.
Happy coding.
In the Settings of your iOS device: Settings > Privacy > Advertising: Turn on and then turn off "Limit Ad Tracking" ("Reset of Ad Identifier" doesn't always help. Try "Reset of Ad Identifier" first and comment if this action doesn't help you).
But! It will help only get ads on your own device and will not solve the problem that matching rate of your ad is low.

CTCallCenter doesn't update the currentCalls property after I set callEventHandler

I'm trying to see if there are any ongoing calls, but I'm having trouble with keeping an instance of CTCallCenter as a property. Here's basically what I'm doing now that I'm debugging (everything is in MyClass):
-(void)checkForCurrentCalls
{
CTCallCenter *newCenter = [[CTCallCenter alloc] init];
if (newCenter.currentCalls != nil)
{
NSLog(#"completely new call center says calls in progress:");
for (CTCall* call in newCenter.currentCalls) {
NSLog(call.callState);
}
}
if (self.callCenter.currentCalls != nil) {
NSLog(#"property-call center says calls in progress:");
for (CTCall* call in self.callCenter.currentCalls) {
NSLog(call.callState);
}
}
}
My self.callCenter is a #property (nonatomic, strong) CTCallCenter *callCenter;. It has synthesized setter and getter. It's initialized in MyClass' init method:
- (id)init
{
self = [super init];
if (self) {
self.callCenter = [[CTCallCenter alloc] init];
}
return self;
}
If I call another of my methods before checkForCurrentCalls, then my self.callCenter.currentCalls stops updating the way it should. More precisely, the phonecalls I make (to myself) just keep piling on, so that if I've dialed and hung up three phonecalls I get printouts of three CTCalls being in the "dialing" state. The newCenter works as expected.
All I have to do to break it is call this:
- (void)trackCallStateChanges
{
self.callCenter.callEventHandler = ^(CTCall *call)
{
};
}
I have come across answers that say that CTCallCenter has to be alloc-init'd on the main queue. I have since then taken care to only call my own init on the main queue, using dispatch_async from my app delegate:
dispatch_async(dispatch_get_main_queue(), ^{
self.myClass = [[MyClass alloc] init];
[self.myClass trackCallStateChanges];
}
My checkForCurrentCalls are later called in applicationWillEnterForeground.
I don't understand why just setting the callEventHandler-block breaks it.
Everything is tested on iphone 5 ios 7.1.2.
This has been reproduced in a new project. Filed a bug report on it.

Displaying admob interstitals multiple times how?

I have small gaming app which has one storyboard and inside it creates scenes like start menu-gamin area-scores I have added admob banner view and interstitals into it.My banner view is working fine and but my interstitial only works for one time.
I load my interstitial on my viewdidload and fire it in the function which calls the gaming sessions end and as I say it works but only for one time when user starts another game and fails this time there is no interstital(error below).So what should I do to fix it I want my game to show interstitials multiple times whenever I want.
Error :
Request Error: Will not send request because interstitial object has been used.
Header:
#import "GADBannerView.h"
#import "GADInterstitial.h"
#class GADInterstitial;
#class GADRequest;
////////////code UIviewcontroller//////////
GADBannerView *bannerView_;
GADInterstitial *interstitial_;
Implementation
-(void)viewdidload
{
//////////////////gaming code///////////
interstitial_ = [[GADInterstitial alloc] init];
interstitial_.delegate = self;
interstitial_.adUnitID = #"ca-app-pub-6280395701552972/5217388242";
GADRequest *request = [GADRequest request];
[interstitial_ loadRequest:request];
}
Implementaion
-(void)failgame
{
//////////////////gaming code///////////
[interstitial_ presentFromRootViewController:self];
}
On googleadmob SDK page it says that interstials are one time use objects so I am %100 sure that is the problem but there is nothing there to explain how to call them multiple time's so as long as you point the answer please don't tell go read it I have read it 5 times.
Well no one gave an answer but I am sure that there are some others out there who had the same problem so for those who want to call interstitals multiple times here is the trick.
Put it into its own method and call method from your main method (which replies many times)
Keep the interstitals on your viewdidload (or where you want fire first) because if you don't then you miss the first fire the others will work.
The full code for that.
- (void) callint
{
int rNumber1 = arc4random() % 45 + 1;
int rNumber2 = arc4random() % 45 + 1;
if((rNumber1%2==1) && (rNumber1%1==0))
{
[interstitial_ presentFromRootViewController:self];
interstitial_ = [[GADInterstitial alloc] init];
interstitial_.delegate = self;
interstitial_.adUnitID = #"ca-app-pub-6280395701552972/5217388242";
GADRequest *request = [GADRequest request];
[interstitial_ loadRequest:request];
}
}
I put random number creation and if there because I don't want users to see those intersitials every time even call int is fired every time there is 4/1 chance for it to fire so it shows 1 interstitial for 4-5 fire.
- (void)viewDidLoad {
[super viewDidLoad];
self.interstitial = [self createAndLoadInterstitial];
}
- (GADInterstitial *)createAndLoadInterstitial {
GADInterstitial *interstitial = [[GADInterstitial alloc] initWithAdUnitID:#"ca-app-pub-3940256099942544/4411468910"];
interstitial.delegate = self;
[interstitial loadRequest:[GADRequest request]];
return interstitial;
}
- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial {
self.interstitial = [self createAndLoadInterstitial];
}
GADInterstitial is a one time use object. To request another interstitial, you'll need to allocate a new GADInterstitial object.
The best place to allocate another interstitial is in the interstitialDidDismissScreen: method on GADInterstitialDelegate, so that the next interstitial starts loading as soon as the previous one is dismissed. reference: admob site link
If you follow the Admob recommended code, as Sonu VR showed, you will have no issue. Just note that whilst there will be a logging error despite of using the best practice Admob code, that is a red herring. That's probably a bug in the Admob logging, but not a bug in the Admob code to display an Ad. You can test for this by using a non-test phone and a different ad will be served at different times.

Resources