How to show iAd in all screens with single reference while using storyboard?

馋奶兔 提交于 2019-12-23 02:21:40

问题


How can one implement iAd in such manner that it is implemented in only AppDelegate and can be used in all screens.

I put code in appDelegate.Do i still need to code for it in each view controller?

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self addiAd];
    NSLog(@"window subview : %@",self.window.subviews);    //Here it shows no subview
    return YES;
}

-(void)addiAd
{
    self.adView.delegate = self;
    self.adView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifierLandscape,ADBannerContentSizeIdentifierPortrait, nil];
    self.adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    [self setBannerFrame:orientation];
    self.adView = [[ADBannerView alloc] init];
    self.adView.frame = CGRectMake(0, 938, 768, 66);
    [self.window addSubview:self.adView];

}
-(void)removeiAd
{
    [self.adView removeFromSuperview];
}
-(void)setBannerFrame:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        [self.adView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifierLandscape];
        self.adView.frame = CGRectMake(0, 634, 320, 50);
    }
    else {
        [self.adView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifierPortrait];
        self.adView.frame = CGRectMake(0, 890, 480, 32);
    }
}

All its delegate methods are also implemented in AppDelegate.m

What should be my next step? what am i doing wrong or missing?


回答1:


Ya. no need to write code in each view controll. here is my code

- (UIWindow *)viewControllerForPresentingModalView {

    return self;

}
- (void)adWhirlDidReceiveAd:(AdWhirlView *)adWhirlView {
    NSLog(@"Ad received");
    CGSize adSize = [adView actualAdSize];
    NSLog(@"Net work Name %@",[adWhirlView mostRecentNetworkName]);
    CGRect newFrame = adView.frame;
    newFrame.size = adSize;



    newFrame.origin.y=385;
    NSLog(@"frame size %f,%f",newFrame.origin.x,newFrame.origin.y);
    adView.frame = newFrame;




}
-(void)adWhirlDidFailToReceiveAd:(AdWhirlView *)adWhirlView usingBackup:(BOOL)yesOrNo
{
    NSLog(@"Ad not Received");
    NSLog(@"Net work Name %@",[adWhirlView mostRecentNetworkName]);
    CGRect tempFrame = adView.frame;
    tempFrame.origin.y = -adView.frame.size.height;
    adView.frame = tempFrame;


}

write all this methods in app delegate and in application didFinishedLaunching method just add the adwhirl view as a sub view of the window.

adView=[AdWhirlView requestAdWhirlViewWithDelegate:self];
    [self.window addSubview:adView];  

Its working fine for me.




回答2:


You don't need any of the iAd configuration in your app delegate, generally you configure it in the class for the view controller that it is displayed in - this means that usually you would need to place an ad view in every view controller that you wanted ads to be displayed in.

There are more simple methods, however - the answer to this question, for example, provides a link to a singleton class that seems to do what you want - it provides a single class that manages your iAd configuration which you can then drop into every view that you want the ad to appear in.




回答3:


Apple recommends creating it once in App Delegate, and sharing across all views.

https://developer.apple.com/library/ios/technotes/tn2286/_index.html



来源:https://stackoverflow.com/questions/12907803/how-to-show-iad-in-all-screens-with-single-reference-while-using-storyboard

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