在iPhone中显示具有自定义尺寸的admob广告

时间:2012-10-09 06:14:43

标签: iphone admob

我可以在iphone中显示自定义尺寸的adob广告,例如280x50,而不是320x50吗?

7 个答案:

答案 0 :(得分:5)

自定义广告尺寸

除了标准的AdMob广告单元,DFP广告管理系统还允许您将任意规模的广告单元投放到应用程序中。请注意,为广告请求定义的广告尺寸(宽度,高度)应与应用程序上显示的广告视图的尺寸(即DFPBannerView)相匹配。

示例:

// 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];

注意:DFP广告管理系统目前不支持智能横幅广告。

多个广告尺寸

DFP允许您指定多个可能有资格投放到DFPBannerView的广告尺寸。要使用此功能,需要三个步骤:

在DFP广告管理系统界面中,创建一个定位与不同尺寸广告素材相关联的广告单元的订单项。 在您的应用程序中,在DFPBannerView上设置validAdSizes属性:

// 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;

实施GADAdSizeDelegate方法以检测广告尺寸更改。

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

请记住在发出广告请求之前使用setAdSizeDelegate设置委托。

[bannerView_ setAdSizeDelegate:self];

在发布视图之前,请务必将GADBannerView的adSizeDelegate属性设置为nil:

- (void)dealloc {
 bannerView_.adSizeDelegate = nil;

 // Don't release the bannerView_ if you are using ARC in your project
 [bannerView_ release];
 [super dealloc];
}

答案 1 :(得分:4)

我遇到了同样的问题。过去,尽可能更改广告的UIView对象的框架,但现在它会立即导致didFailToReceiveAdWithError:

此外,使用非标准尺寸调用DFPBannerView setSize:无效。

我的解决方案是简单地设置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];

另外 - 请注意iPhone 6及以上版本。

答案 2 :(得分:1)

根据iOS 5版本,kGADAdSizeBanner仅允许更改特定尺寸。请参阅下面的代码。

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]];

虽然您可以查看此链接... my admob banner shows in top

答案 3 :(得分:1)

尝试我的指示

在YourviewController头文件

#import "GADBannerView.h"

@interface YourviewController : UIViewController 
{
  GADBannerView *admob_view;
}

在YourViewController实现文件之后:

#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];

 }

}

上面的代码注意到这一行:admob_view = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0,415.0,320,60)];

您可以使用 CGRectMake(x,y,width,height)来指定修改您的admob广告视图,例如您的要求,如下代码:

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

答案 4 :(得分:1)

您可以看到此链接https://developers.google.com/mobile-ads-sdk/docs/admob/intermediate#ios 并根据自定义尺寸的adob广告在iphone中无法实现

答案 5 :(得分:1)

在Swift 4中使用自定义框架显示adMob(在模拟器中测试代码)

@client.command(pass_context = True , aliases=['purge', 'clean', 'delete'])

注意:在AppDelegate中

    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")
    }
}

答案 6 :(得分:-1)

转到GADBannerView.h文件,您会找到以下代码

#define GAD_SIZE_320x50     CGSizeMake(320, 50)

只需将CGSizeMake(320, 50)修改为CGSizeMake(280, 50)

即可