I know there is at least one question asking how to integrate iAd into a sprite kit game, but that is not what I am looking for. I am looking for a portrait version of how to do it. There seem to be absolutely 0 tutorials online as to how to do this, so I came here. Can someone please tell me how to simply enable iAd in a Sprite Kit game? I have enabled canDisplayBannerAds and have used the originalContentView property for my UIView, but I keep getting a crash that says
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController originalContentView]: unrecognized selector sent to instance 0x10a0a7410'
any help would be appreciated, and here is my code in my view controller
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.originalContentView;
//skView.showsFPS = YES;
//skView.showsNodeCount = YES;
// Create and configure the scene.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
self.canDisplayBannerAds = YES;
// Present the scene.
[skView presentScene:scene];
}
For those that don't understand, and those that are for future references, here is how I did this to my Sprite Kit game.
I had my game project created using the SpriteKit template in Xcode, then went to the project settings as so:
In the "Link Binary With Libraries" section, just make sure to hit the + button, and add the iAd framework.
After doing that, go to your view controller for your Sprite Kit game, and then type this:
// at the top
#import <iAd/iAd.h>
// do this in .m, above #implementation
#interface YourViewControllerClassName ()
#property (nonatomic, strong) ADBannerView *banner;
#end
// after the implementation line
// if you're needing to do it horizontally, do:
- (void) viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.banner = [[ADBannerView alloc] initWithFrame:CGRectZero];
self.banner.delegate = self;
[self.banner sizeToFit];
self.canDisplayBannerAds = YES;
SKView *view = (SKView *)self.originalContentView;
SKScene *scene = [[YourScene alloc] initWithSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];
[view presentScene:scene];
}
If you're doing the iAd in just a normal, portrait, you can do the above code, but you can also just use - (void) viewDidLoad instead...
Now is the delegate methods for the iAd to appear...
Go to the code above the #implementation line, and edit it
// do this in .m, above #implementation
#interface YourViewControllerClassName () <ADBannerViewDelegate>
#property (nonatomic, strong) ADBannerView *banner;
#end
Now, go under the implementation line, and enter this:
// NOTE: THIS CODE CAME FROM APPLE MOSTLY
// I DID EDIT IT, BUT THE CREDIT GOES TO APPLE'S DOCUMENTATION
// ON IAD
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (banner.isBannerLoaded) {
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// Assumes the banner view is placed at the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
}
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!banner.isBannerLoaded) {
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// Assumes the banner view is just off the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
[UIView commitAnimations];
}
}
That is all that is required from the iAd to actually work in a SpriteKit game. I hope that I helped those that are going to read this.
Related
Hey guys!
I want to change the alpha of a UIButton which sounds actually pretty simple. However, I want to change that alpha from a different class(ViewControllerB.m/.h). I got a class called "ViewController.h/.m" and from this class I call a view which appears on the ViewController like a popup. Now I got a UIButton which displays the popup, when it's touched. If the button was touched its alpha will change to "0.0" and the popup will be shown. I got the code from the popup in another class and I want to change the alpha of the button to "1.0" if the pupup was dismissed. I already got a method, that is called when the popup was dismissed. I tried everything, but it didn't worked so far. Maybe because I'm a beginner at iOS ^^.
I hope you understood what I am trying to say. I will leave the code as it is (clean) to do not confuse you with the ways I tried before.
Here you got the codes of my classes:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
//....
IBOutlet UIButton *myBtn;
//...
}
- (IBAction)OpenPopUp:(id)sender;
Now in my ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
//...viewDidLoad....everything else
- (IBAction)OpenPopUp:(id)sender {
[UIView animateWithDuration: 1.0
animations:^{
myBtn.alpha = 0.0;
}];
}
#end
In my ViewControllerB.h:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface ViewControllerB : UIViewController {
//...some unneccesary outlets
}
//Displaying popup.....
- (void)presentInParentViewController:(UIViewController *)parentViewController;
#end
ViewControllerB.m :
#import "ViewControllerB.h"
#interface ViewControllerB ()
#end
#implementation
//...ViewDidload...and more
- (IBAction)close:(id)sender {
//The close button
[self dismissFromParentViewController];
}
- (void)dismissFromParentViewController {
//Removes the nutrition view from the superview
[self willMoveToParentViewController:nil];
//Removes the view with or without animation
if (!self.shouldAnimateOnDisappear) {
[self.view removeFromSuperview];
[backgroundGradientView removeFromSuperview];
[self removeFromParentViewController];
return;
}
else {
[UIView animateWithDuration:0.4 animations:^ {
CGRect rect = self.view.bounds;
rect.origin.y += rect.size.height;
self.view.frame = rect;
backgroundGradientView.alpha = 0.0f;
}
completion:^(BOOL finished) {
[self.view removeFromSuperview];
[backgroundGradientView removeFromSuperview];
[self removeFromParentViewController];
}];
}
//THE PLACE I WANT TO CHANGE THE ALPHA BACK!
}
I really would appreciate anyones help and if you can please show it me on code examples.
Thank you,
Noah
NOTE: I already looked up this posts but tried unsuccsessfully.
Passing Data between View Controllers
Passing data between view controllers in IOS
Modifying UIButton's alpha property from another class
Change alpha and enabled UIButton of ClassA from ClassB in object c
There are 2 ways of doing it,
In ViewController *viewDidAppear:* method check myBtn alpha. if alpha is zero then set the alpha back to 1.
You can add ViewController as a delegate in ViewControllerB and ViewControllerB notifies ViewController when it is dismissed and then you set the alpha of myBtn back to 1
In my ViewControllerB.h:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#protocol ViewControllerBDelegate <NSObject>
- (void)didHidePopoverController
#end
#interface ViewControllerB : UIViewController {
//...some unneccesary outlets
}
#property(readwrite,weak)id <ViewControllerBDelegate>delegate;
//Displaying popup.....
- (void)presentInParentViewController:(UIViewController *)parentViewController;
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "ViewControllerB.h"
#interface ViewController : UIViewController<ViewControllerBDelegate> {
//....
IBOutlet UIButton *myBtn;
//...
}
- (IBAction)OpenPopUp:(id)sender;
#end
Now in ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
//...viewDidLoad....everything else
- (IBAction)OpenPopUp:(id)sender {
[UIView animateWithDuration: 1.0
animations:^{
myBtn.alpha = 0.0;
}];
}
- (void)didHidePopoverController {
[UIView animateWithDuration: 1.0
animations:^{
myBtn.alpha = 1.0;
}];
}
#end
ViewControllerB.m :
#import "ViewControllerB.h"
#interface ViewControllerB ()
#end
#implementation
//...ViewDidload...and more
- (IBAction)close:(id)sender {
//The close button
[self dismissFromParentViewController];
}
- (void)dismissFromParentViewController {
//Removes the nutrition view from the superview
[self willMoveToParentViewController:nil];
//Removes the view with or without animation
if (!self.shouldAnimateOnDisappear) {
[self.view removeFromSuperview];
[backgroundGradientView removeFromSuperview];
[self removeFromParentViewController];
return;
}
else {
[UIView animateWithDuration:0.4 animations:^ {
CGRect rect = self.view.bounds;
rect.origin.y += rect.size.height;
self.view.frame = rect;
backgroundGradientView.alpha = 0.0f;
}
completion:^(BOOL finished) {
[self.view removeFromSuperview];
[backgroundGradientView removeFromSuperview];
[self removeFromParentViewController];
}];
}
[self.delegate didHidePopoverController];
//THE PLACE I WANT TO CHANGE THE ALPHA BACK!
}
When you are showing the popup just assign ViewController instance as a delegate of ViewControllerB instance
Update: I seached for it and found it. The awser from Anshul Join didn't worked for me but what worked for me was this: Update a label through button from different view
It is much less complicated and easy to implement. There are no delegates. Hope I could help anyone with this.
But summed up: Thanks to Anshul Jain for supporting me and trying to solve this for me. Thank you bro!
I am building an app in which I want to us iAd banners. the viewcontroller in which I want to show the ad is a UIViewController. I am using storyboards. I implemented the iAd banner exactly as apple shows/demonstrates here:
add iAd framework
import "<iAd/iAd.h>" header
inserted self.canDisplayBannerAds = YES;
but when I run my app, no iAd is showing up. do I forget something? Thank you so much in advance
I had a bit different issue, but the answer fits to your question as well. I updated my old xib file (was from XCode 5.1) to use Size Classes in XCode 6.0. After the update ads showed only in iPhone 4 - 5 and 5s. iPhone 6 and iPhone 6 Plus was without banners (simulators showed the same results as the external devices). When using xib file and using
self.canDisplayBannerAds = YES;
in my situation I moved this command from viewDidLoad to viewDidAppear and banner showed up. Now I can see banners in all devices.
In your .h :
#interface MyViewController : UIViewController <ADBannerViewDelegate>
Add a banner view in your xib file and link this banner view with an IBOutlet
#property (nonatomic, strong) IBOutlet ADBannerView *iAdBannerView;
add the following code to display your ad :
#pragma mark - ADBannerViewDelegate
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
NSLog(#"banner loaded");
// Display BannerView
_iAdBannerView.hidden = NO;
[UIView animateWithDuration:0.4f
animations:^{
_iAdBannerView.alpha = 1.0f;
}];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
// Print error
NSLog(#"error banner failed :\n%#", error);
// Hide BannerView
[UIView animateWithDuration:0.4f
animations:^{
_iAdBannerView.alpha = 0.0f;
} completion:^(BOOL finished) {
_iAdBannerView.hidden = YES;
}];
}
and normally your adbanner will appear when you receive and ad and disappear otherwise.
The problem was that I had to sign the iAd contract in itunes connect, which I had not done yet. it works fine now!
I want to add iAd in my app. And i have done lot of research and seen video that display how to add iAd in your project. But all video and tutorial display code for adding iAd in project none of them said any thing about if we have to do something in iTunes connect or not.
I have seen this link in apple doc. which is nice to add iAd. and in that link it will display tab iTunes Connect Developer Guide which display some setting we do have to make in iTunes connect to add iAd in app.
So as my understanding we have to first do that setting in iTunes connect account for iAd and then implement code to add iAd in project.
But i m confuse at this line in apple doc After you have enabled at least one app for iAd ads, you see the iAd Network module on your iTunes Connect homepage. What does it mean that enable app for iAd?
itunesconnect -> manage your apps
and this code Display the Default Screen of iAd network
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#interface iAdExViewController : UIViewController <ADBannerViewDelegate>
{
ADBannerView *adView;
BOOL bannerIsVisible;
}
#property (nonatomic,assign) BOOL bannerIsVisible;
#end
Then modify viewDidLoad method in iAdExViewController.m
- (void)viewDidLoad
{
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, -50);
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
[self.view addSubview:adView];
adView.delegate=self;
self.bannerIsVisible=NO;
[super viewDidLoad];
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// banner is invisible now and moved out of the screen on 50 px
banner.frame = CGRectOffset(banner.frame, 0, 50);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// banner is visible and we move it out of the screen, due to connection issue
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
NSLog(#"Banner view is beginning an ad action");
BOOL shouldExecuteAction = YES;
if (!willLeave && shouldExecuteAction)
{
// stop all interactive processes in the app
// [video pause];
// [audio pause];
}
return shouldExecuteAction;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
// resume everything you've stopped
// [video resume];
// [audio resume];
}
Go to itunes connect -> manage your apps and pick the app you want, then click on Set Up iAd Network, just that.
But i m confuse at this line in apple doc After you have enabled at least one app for iAd ads, you see the iAd Network module on your iTunes Connect homepage. What does it mean that enable app for iAd?
This means, when you activate iAd (as I told you), a new module appears called iAd Network, and there you can see the statistics (impresions, revenue, etc)
This seems like the dumbest question ever, but after wading through all of Apple's documentation and the useless online tutorials, I still can't figure out how to properly implement iAds into my application. So, my app starts off in a table view controller, and I have an iAd object underneath the navigation bar and above the table.
Now, in my code: (I also have the iAd framework added)
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#interface MasterTableViewController : UITableViewController <ADBannerViewDelegate>
{
IBOutlet ADBannerView *iAd;
}
#property(nonatomic, readonly, getter=isBannerLoaded) BOOL bannerLoaded;
#end
then in the .m file
#import "MasterTableViewController.h"
#interface MasterTableViewController ()
#end
#implementation MasterTableViewController
#synthesize bannerLoaded;
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
if (!willLeave)
{
// nothing in this case thanks to ARC
}
return YES;
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
iAd.hidden = NO;
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
iAd.hidden = YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
for (int i = 0; i > 0; i++)
{
if (bannerLoaded)
{
iAd.hidden = NO;
}
else
{
iAd.hidden = YES;
}
}
}
Now, the problem is, when I test the app without internet connection the iAd does not load (obviously) BUT it also does not hide. So, at the top of the screen I'm left with a big white rectangle. Otherwise, the ad works fine when a connection is available. Does anyone have any ideas? Also - I just added the endless loops to see if they made a difference, those were completely on purpose lol.
I'm assuming you have added your ADBannerView via the Storyboard as I can't see where you initialise the banner position.
In storyboard, set the initial location just off screen.
In "bannerViewDidLoadAd", animate the banner into view.
In "bannerView: didFailToReceiveAdWithError:", animate the banner out of view.
There is a good example here https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/iAd_Guide/WorkingwithBannerViews/WorkingwithBannerViews.html
Hope this helps. I have just implemented this but don't have access to my code at the moment.
It does bring up some errors in the simulator but works fine on a device.
I have tested this on iOS6 and works ok even if an iAd is displayed then the user loses the connection (so it looks like "bannerView: didFailToReceiveAdWithError:" is being called for me).
You have a property bannerLoaded for saving the state of your Ad, which is good.
At the very first, in your viewDidLoad method, iAd can't be loaded, so you have to set your property accordingly : self.bannerLoaded = NO;
Then, when you receive / fail to receive your ad, you need to update this property.
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerLoaded) {
iAd.hidden = NO;
self.bannerLoaded = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerLoaded) {
iAd.hidden = YES;
self.bannerLoaded = NO;
}
}
I am trying to use Burstly to serve iAds and adMob ads into my iPhone app. I followed their guide
http://docs.burstly.com/guides/ad-serving-quick-start-guide.html
Here's a snippet of what I have...
MyViewController.h
#interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, OAIAdManagerDelegate> {
OAIAdManager *adManager;
IBOutlet UIView *mainView;
}
#property (nonatomic, retain) UIView *mainView;
#end
MyViewController.m
#import "MyViewController.h"
#implementation MyViewController
#synthesize mainView;
- (void)viewDidLoad {
[super viewDidLoad];
adManager = [[OAIAdManager alloc] initWithDelegate:self];
[self.view addSubview:adManager.view];
[adManager requestRefreshAd];
}
- (UIViewController *)viewControllerForModalPresentation {
return self;
}
- (CGFloat)defaultSessionLife {
return 35.0f;
}
- (Anchor)anchor {
return Anchor_Bottom;
}
- (CGPoint)anchorPoint {
return CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height);
}
- (NSString *)publisherId {
return #"ENTER YOUR PUBLISHER ID HERE";
}
- (NSString *)getZone {
return #"ENTER YOUR ZONE ID HERE";
}
- (UIViewController *)viewControllerForModalPresentation {
return self;
}
// resize mainView (pull up) when ad is displayed
- (void)adManager:(OAIAdManager*)manager didLoad:(NSString*)aNetwork {
[UIView beginAnimations:#"AdResize" context:nil];
[UIView setAnimationDuration:0.7];
CGRect mainViewNewFrame = mainView.frame;
mainViewNewFrame.size.height = 460 - adSize;
mainViewNewFrame.size.width = 320;
mainViewNewFrame.origin.x = 0;
mainViewNewFrame.origin.y = 0;
mainView.frame = mainViewNewFrame;
[UIView commitAnimations];
}
// resize MainView when ad is not available
- (void)adManager:(OAIAdManager*)manager failedToLoad:(NSString*)aNetwork {
[UIView beginAnimations:#"AdResize" context:nil];
[UIView setAnimationDuration:0.7];
CGRect mainViewNewFrame = mainView.frame;
mainViewNewFrame.size.height = 460;
mainViewNewFrame.size.width = 320;
mainViewNewFrame.origin.x = 0;
mainViewNewFrame.origin.y = 0;
mainView.frame = mainViewNewFrame;
[UIView commitAnimations];
}
I'm having issues knowning exactly when an ad is being displayed so that I can know to rearrange my view. I have a table view touching the bottom of the screen, so I need to pull it up (resize my the view) when an ad is displayed.
I tried using the OAIAdManagerDelegateProtocol "adManager:didLoad:" method, which gives mixed results. That method is always called when the admanager thinks it has an ad, but sometimes an ad is not displayed (especially the first call for an iAd). So, I'm always resizing my main view to make room for an ad when that method is called, but sometimes an ad is not there to be displayed, so I end up showing a white space the size of the ad.
I also tried using the "adManager:didLoadView:" method, but it is never called.
So, is there any other way to be notified when the adManager.view is shown on screen so I can know when to resize my view?
this is Alex with Burstly. This was in fact an issue with our code and iOS 4.1. We've resolved it and you can find an updated SDK on github: http://github.com/burstly/Burstly-iPhone/downloads