iAd BannerView contentsizeIdentifier Portrait or 320x50?

无人久伴 提交于 2019-12-23 23:35:39

问题


So in viewdidload i have

adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;

for the banner of an iAd. i I build for iOS 4.2+ the debugger console tells me this:

ADBannerView: ADBannerContentSizeIdentifier320x50 is deprecated, please use ADBannerContentSizeIdentifierPortrait instead

so then i do:

adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;

and on 4.1 and lower the app crashes telling me EXC_BAD_ACCESS.

I dont know which one to take, but the one with 320x50 works on newer versions, but i get that notification/error from the console. Any suggestions?


回答1:


Ok i got it to work like this:

NSString *os = [[UIDevice currentDevice] systemVersion];
        NSString *ioss = [os stringByReplacingOccurrencesOfString:@"." withString:@""];
        if ([ioss length] == 2) {
            NSLog(@"length = 2");
            float x = ([ioss floatValue]*10);
            NSString *ios = [[NSString alloc] initWithFormat:@"%.f",x];
            if ([ios floatValue] > 419) {
                NSLog(@"Portrait");
                adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
                adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
            } else {
                NSLog(@"320x50");
                adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
                adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
            }

        }
        else if ([ioss length] == 3) {
            NSString *ios = ioss;
            NSLog(@"length = 3");
            if ([ios floatValue] > 419) {
                NSLog(@"Portrait");
                adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
                adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
            } else {
                NSLog(@"320x50");
                adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
                adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
            }

        }

Note that this method can be used for all kind of other stuff, what it does is it detects the ios version, and performs an action when it is above a given version and below.

but also this works in my case:

if ( &ADBannerContentSizeIdentifierPortrait != NULL ) {
    adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
    adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
    adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
    adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
}



回答2:


ADBannerContentSizeIdentifierPortrait doesn't exist on iOS versions before 4.2 so it is raising the error. What you can do is check if the symbol exists and set the properties based on that.

if ( &ADBannerContentSizeIdentifierPortrait != NULL ) {
    self.bannerView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
    self.bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
    self.bannerView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
    self.bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
}


来源:https://stackoverflow.com/questions/6698963/iad-bannerview-contentsizeidentifier-portrait-or-320x50

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!