Show custom admob ads on iPhone

Is it possible to display admob ads with non-standard size in iphone, for example 280x50, and not 320x50?

+4
source share
7 answers

Custom Ad Size

In addition to standard AdMob ad units, DFP allows you to serve an ad unit of any size in the app. Please note that the ad size (width and height) defined for the ad request must match the size of the ad displayed in the application (i.e. DFPBannerView).

Example:

// Define custom GADAdSize of 280x30 for DFPBannerView GADAdSize customAdSize = GADAdSizeFromCGSize(280, 30); // Don't use autorelease if you are using ARC in your project self.adBanner = [[[DFPBannerView alloc] initWithAdSize:customAdSize] autorelease]; 

Note. DFP does not currently support smart banners.

Multiple Ad Sizes

DFP allows you to specify several ad sizes that may be eligible for service in DFPBannerView. To use this function, you must complete three steps:

In the DFP user interface, create a campaign that targets the same ad unit that’s associated with ads of different sizes. In your application, set the validAdSizes property to DFPBannerView:

 // Define an optional array of GADAdSize to specify all valid sizes that are appropriate // for this slot. Never create your own GADAdSize directly. Use one of the // predefined standard ad sizes (such as kGADAdSizeBanner), or create one using // the GADAdSizeFromCGSize method. // // Note: Ensure that the allocated DFPBannerView is defined with an ad size. Also note // that all desired sizes should be included in the validAdSizes array. GADAdSize size1 = GADAdSizeFromCGSize(CGSizeMake(120, 20)); GADAdSize size2 = GADAdSizeFromCGSize(CGSizeMake(250, 250)); GADAdSize size3 = GADAdSizeFromCGSize(CGSizeMake(320, 50)); NSMutableArray *validSizes = [NSMutableArray array]; [validSizes addObject:[NSValue valueWithBytes:&size1 objCType:@encode(GADAdSize)]]; [validSizes addObject:[NSValue valueWithBytes:&size2 objCType:@encode(GADAdSize)]]; [validSizes addObject:[NSValue valueWithBytes:&size3 objCType:@encode(GADAdSize)]]; bannerView_.validAdSizes = validSizes; 

Implement the GADAdizeDelegate method to determine the size of the ad.

 @protocol GADAdSizeDelegate <NSObject> - (void)adView:(GADBannerView *)view willChangeAdSizeTo:(GADAdSize)size; @end 

Remember to set the delegate using setAdSizeDelegate: before making a request for the declaration.

 [bannerView_ setAdSizeDelegate:self]; 

Be sure to set the GADBannerView adSizeDelegate property to nil before releasing the view:

 - (void)dealloc { bannerView_.adSizeDelegate = nil; // Don't release the bannerView_ if you are using ARC in your project [bannerView_ release]; [super dealloc]; } 
+5
source

I had the same problem. It used to be possible to change the frame of a UIView declaration object, but now it raises an immediate didFailToReceiveAdWithError:

In addition, calling DFPBannerView setSize: with a custom size has no effect.

My solution was to simply set the scaling of DFPBannerView :

  GADBannerView *retVal = [self.dfpAd]; float scaleX = w / (retVal.adSize.size.width - 0.5f); float scaleY = h / (retVal.adSize.size.height - 0.5f); float scaleFactor = MAX(scaleX, scaleY); retVal.transform = CGAffineTransformMakeScale( scaleFactor, scaleFactor); // [((DFPBannerView *) retVal) resize:GADAdSizeFromCGSize(CGSizeMake(w, h))]; // Of course not working.... // iPhone 6 and above fix. Won't rescale correctly if you don't layout. Do this after addSubView! // [retVal setNeedsLayout]; // [retVal layoutIfNeeded]; 

Also, please pay attention to iPhone 6 and above.

+4
source

According to iOS 5, kGADAdSizeBanner only allows changes to a specific size. See code below.

 bannerView_ = [[GADBannerView alloc]initWithFrame:CGRectMake(0.0,0.0, 320.0, 300.0)]; bannerView_.adUnitID = MY_BANNER_UNIT_ID; bannerView_.rootViewController = self; [self.view addSubview:bannerView_]; [bannerView_ loadRequest:[GADRequest request]]; 

Although you can check out this link ... my admob bonus is at the top

+1
source

TRY MY INSTRUCTION

In YourviewController Header File

 #import "GADBannerView.h" @interface YourviewController : UIViewController { GADBannerView *admob_view; } 

Once in the InViewcontroller implementation file:

 #import "MainViewController.h" #import <QuartzCore/QuartzCore.h> #define AdMob_ID @"a150349d7c43186" @implementation YourviewController { -(void)viewDidLoad { admob_view = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0,415.0,320,60)];//in this line mention your adview size admob_view.adUnitID = AdMob_ID; admob_view.rootViewController = self; [self.view addSubview:admob_view]; GADRequest *r = [[GADRequest alloc] init]; r.testing = YES; [admob_view loadRequest:r]; } } 

above code, pay attention to this line: admob_view = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0,415.0,320,60)];

you can use CGRectMake (x, y, width, height) to assign a modification to your view of admob ads, for example, your requirement is below code:

 admob_view = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0,415.0,280,50)]; 
+1
source

You can see this link https://developers.google.com/mobile-ads-sdk/docs/admob/intermediate#ios and according to this usual ad size admob is not possible on iphone

+1
source

In Swift 4, to display adMob with a custom frame (tested code in the simulator)

  class ViewController: UIViewController, GADBannerViewDelegate { override func viewDidLoad() { super.viewDidLoad() print("Google Mobile Ads SDK version: \(GADRequest.sdkVersion())") let bannerView = GADBannerView(adSize: kGADAdSizeMediumRectangle) //OR You can give custom frame //let bannerView = GADBannerView.init(frame:CGRect(x: 0, y: 0, width: 300, height: 250)) bannerView.adUnitID = "YOUR adUnitID" bannerView.rootViewController = self bannerView.delegate = self //bannerView.adSize = kGADAdSizeMediumRectangle let request = GADRequest() request.testDevices = [kGADSimulatorID]; bannerView.load(request) self.view.addSubview(bannerView) } //MARK : GADBannerView Delegates /// Tells the delegate an ad request loaded an ad. func adViewDidReceiveAd(_ bannerView: GADBannerView) { print("adViewDidReceiveAd") } /// Tells the delegate an ad request failed. func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) { print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)") } /// Tells the delegate that a full-screen view will be presented in response /// to the user clicking on an ad. func adViewWillPresentScreen(_ bannerView: GADBannerView) { print("adViewWillPresentScreen") } /// Tells the delegate that the full-screen view will be dismissed. func adViewWillDismissScreen(_ bannerView: GADBannerView) { print("adViewWillDismissScreen") } /// Tells the delegate that the full-screen view has been dismissed. func adViewDidDismissScreen(_ bannerView: GADBannerView) { print("adViewDidDismissScreen") } /// Tells the delegate that a user click will open another app (such as /// the App Store), backgrounding the current app. func adViewWillLeaveApplication(_ bannerView: GADBannerView) { print("adViewWillLeaveApplication") } } 

Note: in AppDelegate

 GADMobileAds.configure(withApplicationID: "YOUR ApplicationID") 
+1
source

just go to the GADBannerView.h file and you will find the following code

 #define GAD_SIZE_320x50 CGSizeMake(320, 50) 

just edit CGSizeMake(320, 50) to CGSizeMake(280, 50)

-1
source

Source: https://habr.com/ru/post/1438565/


All Articles