How to share a iAd adbanner across view's? - iphone

I am making a iPhone app, I am currently using iAd's in my view's. I've read something about apple recommending to share the adbanner's through out the view's. How can i do this because I have read the apple documentation and it was not clear enough and i was confused, please help me, if their are any tutorials please tell me or just answer and point me in the right way. Currently I have a ad for every view controller and it runs nice but it comes with a error: Too many active banners (10). Creation of new banners will be throttled.
Please help, i am using storyboards and the latest Xcode :) thanks

The way I do it is use a Singleton class that creates the adView, this way you only ever get 1 adView. Then in your viewDidAppear methods of all your ViewControllers you simply add the adView to your view.
You can see my code in the accepted answer here although that is for an AdWhirl ad view, it shouldn't be too difficult to change it for an iAd ad view.

This is the perfect case for a singleton. Many have simply made a subclass of ADBannerView, which can then be added to your view in -viewDidLoad (or -viewDidAppear with the proper precautions taken) and removed in -viewDidDisappear for each View Controller.

Related

iPhone Cocos2D - where to load the code?

I am starting to learning Cocos2D for iPhone. I see the basic template has a viewController but when it runs it loads HelloWorldLayer, instead of the viewController view.
When you work with a viewController, you used to have the main logic for that controller on the .m, but as Cocos2D works in another way I wonder where should I put the main logic code.
What I mean is this. Developing with Cocos2D I will have a bunch of these "layer" files and as I pass the scenes, one of these files will be active at one time but now imagine that I am building an app that has in-app purchases or coredata. I have to have one central place where this common logic to handle sales and the database should reside. Where is the best practice? To put it on the viewController Cocos2D creates or what? How do you do that?
Where do I put the code so it will initialize when the app runs and before showing the first scene?
thanks
The startup code in Cocos2D is in the app delegate applicationDidFinishLaunching method. So you may want to initialize your loading code there.
The issue you're experiencing may be due to the incorrect setup of the view controller by Cocos2D, which leads to the view controller's viewDidLoad method not to be called. You can call that manually from applicationDidFinishLaunching however, after the line: rootViewController.view = glView
You'll find plenty of Tutorials that shed more light on this here: http://www.raywenderlich.com/

How to get/set the rootViewController?

Now,I gonna Develop an App ,which wants to switch from many different Views irregularly,also the views need to load large resources,AKA,it's hard to manage memory.Are there any good solustion?
PS:I created a ViewController as RootViewController,and When a button was Touch,run the code as
"ViewController=newController"
.The problem came,The new View loaded wrong way,it rotate so that couldn't show in a correct way.
I google for the solution,some one said ,I should replace the rootViewController,just like that,
[UIApplication sharedApplication].delegate.window.rootViewController=newController;
But I can't get/set the rootViewController in other class though it's a singleton.
Why not having a class that handles all the view switches ?
This article describes an architecture that might be helpfull: http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/comment-page-1/#comment-607

Point of Reference: Replacing the Root View Controller / App Delegate?

I'm new to iOS development, obviously, and I'm running into a bit of an issue with many of the tutorials that I find online. While I understand the majority of the code that is going into these programs, I cannot figure out how to translate this one fact, which is probably simple.
Many tutorials either use the Navigation-based template or View-based template, but I would like to try building tab bar applications. Tutorials either use the App Delegate or rootViewController (being the navigation controller), but since my tab bar is my rootViewController, I always run into an issue. I'm also unable to use the applicationDidFinishLaunching method in most of my code, because it technically only applies to the first tab at launch.
These are my questions:
Is there a way to "translate" these files into new classes (for example, creating an instance of NSObject for the App Delegate code for each individual tab or creating instances of UINavigationViewController)?
If I can create the instance of NSObject, how do I ensure that the code links up to the objects that I create in my viewController?
If I can create a file that uses UINavigationController, how do I trick the program into temporarily allowing the Nav Controller to be the rootViewController.
Many thanks in advance!
Right off the top of my head, I can only answer number 3 for you, you could do that in one of two ways that I am aware, and if anyone sees either of these is incorrect or bad practice, I hope someone corrects me, as this is what I have been doing.
[appDelegate.window addSubview:appDelegate.newRootViewController.view];
appDelegate.window.rootViewController = appDelegate.newRootViewController;
or
[self.navigationController pushViewController:appDelegate.newRootViewController animated:YES];
Both should accomplish the same thing, though if you can help it, the second version is ideal. Its also worth noting that for the first one, there is no tricking, you are actually setting the rootViewController of your app to something different.
I'll do some digging and see if I can answer any of your other questions for you too.
Edit: So after re-reading and thinking a bit more about it, I think the other two questions can be answered by maybe clarifying a tab bar application. Unfortunately, I'm fairly new to iOS as well, and I haven't had to opportunity to create a tab bar application, so I don't want to give you incorrect info. I would recommend checking out http://www.techotopia.com/index.php/Creating_an_iOS_4_iPhone_Multiview_Application_using_the_Tab_Bar_%28Xcode_4%29 and hopefully it can give you a little bit of a better idea of how Tab Bar Apps work. I've been using that eBook along with another from that site combined with Ray Wenderlich's tutorials to teach myself.
Anyways, I hope this helps to some extent, if you want me to try to clarify or go into detail on anything just comment and I'll see if I can help.
Good Luck!
-Karoly
You're misunderstanding how the app delegate works.
It's the delegate for the application, not a controller. Your controllers may be loaded from a xib by the time applicationDidFinishLaunching is called, but there's no connection between the two events.
applicationDidFinishLaunching is just the place where you do final setup before the app is ready for use.
If you don't instantiate your tab bar controller in your main xib file, you can instantiate it here, then instantiate all of it's controllers and add them to to the tab bar controller. While you're doing that, you can load plists, set properties on the controllers, etc.
If you do instantiate your tab bar controller in your main xib file, you can still get access to its controllers here. You can edit those controllers or throw them out and create new ones. You can even throw out your tab bar controller and switch to a navigation controller.
I can't answer questions 1 and 2 because the assumptions behind them are invalid.
Rather than ask how you work around perceived problems with the app delegate and tab bar controllers, describe what you're trying to accomplish.

How do I hide an infoButton in a utility app from the MainViewController when the infoButton is in the RootViewController?

How would I go about updating an object declared in the RootViewController from my MainViewController?
I'm attempting to hide my info button when my iAd is tapped, I have all the relevant pieces of code for the iAd in place, but can't figure out how to code the action. I saw an example of a similar situation online that was like this:
((MainViewController *)parentViewController).infoButton.hidden = #"";
I haven't been able to get that to work though, I just need this one value modifiable from the MVC, can anyone give me a simple suggestion?
P.S. I'm a total n00b and a snippet of code would help a great deal, I'm kind of learning as I go, thanks!
I figured this out through use of the NSNotificationCenter

How to implement admob on view controller?

In my app, I want to implement admob and now only I have started to learn about it. So any one please guide me and is there any nice tutorial or sample?
It's extremely simple. Follow the guide here: http://developer.admob.com/wiki/IPhone#AdMob_iPhone_SDK
Once you have the SDK up, you simply do:
AdMobView *ad = [AdMobView requestAdWithDelegate:<your delegate>]; // start a new ad request
ad.frame = CGRectMake(0, 432, 320, 48); // set the frame, in this case at the bottom of the screen
[self.window addSubview:ad]; // attach the ad to the view hierarchy; self.window is responsible for retaining the ad
Download the Admob SDK for iPhone and look at the example projects. It's very simple.
If you choose to use the Interface Builder approach and follow their steps
there is one step missing: Add an AdViewController object to your UIViewController.h/m file and use that as the referencing object in IB. Otherwise you'll get the error:
"Must implement required method
-currentViewControllerForAd:(AdMobView *)adView in your delegate"
I realize this is a basic assumption, but it caught me for a minute and i've implemented AdMob 3 times in the past (using different methods, mind you).
Add an ad to a view using Interface Builder
Add AdViewController.h and AdViewController.m to your project (located in the IBSupport subdirectory).
Open Interface Builder.
Place a 320x48 UIView where you want the ad to appear.
Add an Object, and change its type to AdViewController.
Set the view outlet of the AdViewController to your UIView.
Set the currentViewController outlet of the AdViewController
to the UIViewController owning the xib.
Edit AdViewController.m to make sure that your publisher ID and
other options are set correctly.
Good thing to know, Mike. They also left out one step in Interface Builder. You need to select File's Owner, and drag from Ad View Controller to the new Ad View Object. I simply cannot comprehend why Admob would leave out these details, especially when many new programmers join they're community all the time. They really need help in the documentation & tutorials department.