Admobs sometimes appear twice on different locations - objective-c

We are adding admobs as a backup ad-system for countries without iAd. But sometimes the ads appear twice, the second ad sometimes in the middle of the screen about 100-200 px above the other ad. It seems like the second ad only appears when visiting a view that already has an ad. It kinda feels like the ad-call is in viewDidAppear instead of viewDidLoad, but we don't. Or didFailToReceiveAdWithError gets called on outside viewDidLoad-call for ad. Anyone know anything about this?
EDIT:
They CAN appear both at once, without having to revisit a view that already has an ad.
EDIT(code)
basically:
-(void) viewDidLoad
{
[self createAdBannerView];
[self.view addSubview:bannerView];
}
-(void) createAdBannerView
{
bannerView = [[AdBannerView alloc] initWithFrame:CGRectZero];
CGRect bannerFrame = bannerView.frame;
bannerView.frame = bannerFrame;
bannerView.delegate = self;
bannerView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifiersPortrait,AdBannerContentSizeIdentifierLandscape,nil];
}
-(void)bannerView:(AdBannerView*)banner didFailToReceiveAdWithError:(NSError *)error
{
[self.bannerView removeFromSuperview];
GbannerView = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height-99, 320,50)];
self.GbannerView.adUnitID=#"xxxxxxx";
self.GbannerView.rootViewController = self;
[self.view addSubview:self.GbannerView];
[self.GbannerView loadRequest:[GADRequest request]];
}
Also, if someone knows: In views containing tableViews, the locations of the admobs is wrong. It gets stuck behind a tabbar, even though the location takes this into account. This does not happen in a normal UIView. whatz..

I'd recommend you pull out the tableView question into its own question. There's an example of how to do this here, but I don't know if that satisfies your specific case).
Is there a reason you're not just using AdMob mediation here? They've got support for iAd and setting it up with client code is pretty easy.
For your specific case, I wonder if bannerView:didFailToReceiveAdWithError: is getting continuously called even after you remove AdBannerView from its superview. Why don't you create one GADBannerView and AdBannerView at the start in viewDidLoad:, then check if it exists before instantiating any new ones?

Related

ios 8 : ABPeoplePickerNavigationController dismiss on implementing people picker delegate methods

This is strange behavior noticed while accessing contact details from address book in ios 8.
My scenario is simple
Show contacts table
select a row that will invoke didSelectPerson method
in didSelectPerson method
push SecondViewController
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person;
{
SecondViewController *detailVC = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[detailVC.view setBackgroundColor: [UIColor redColor]];
// [peoplePicker.navigationController pushViewController:detailVC animated:YES];
[peoplePicker pushViewController:detailVC animated:YES];
}
but what happens is ABPeoplePickerNavigationController dismiss.
Please enlighten me on this.
I don't know the philosophy thing what happens under the hood of the "didSelectPerson" method, me was facing the same problem today. I found a simple solution to overcome this issue, i override the
"-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion" method of the "ABPeoplePickerNavigationController". Then implement it like somewhat following
bool dismissedEnabled;
-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
if (dismissedEnabled) {
[super dismissViewControllerAnimated:flag completion:completion];
}
}
then inside the "didSelectPerson" i have done the following
viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:[NSBundle mainBundle]];
dismissedEnabled = false;
[self presentViewController:viewController animated:YES completion:nil];
this works for me, hope you guys overcome it too :)
It automatically dismisses if you select a contact with a single email address for example.
If a contact has more than one email, you must specify a predicate that will force the ABPeoplePickerNavigationController to push a ABPersonViewController on the stack.
if ([picker respondsToSelector:#selector(setPredicateForSelectionOfPerson:)])
{
// The people picker will select a person that has exactly one email address and call peoplePickerNavigationController:didSelectPerson:,
// otherwise the people picker will present an ABPersonViewController for the user to pick one of the email addresses.
picker.predicateForSelectionOfPerson = [NSPredicate predicateWithFormat:#"emailAddresses.#count = 1"];
}
I believe the default behavior in iOS 8 is that the ABPeoplePickerNavigationController is automatically dismissed when didSelectPerson is called.
The reason that the SecondViewController is not displayed (I'm inferring that this is the problem symptom) is because you are trying to push the SecondViewController while the ABPeoplePickerNavigationController is being dismissed. This overlapping animation is a problem that the iOS core view management/animation system tries to avoid.
You may get a warning in the console when this happens.
#Ratul's solution works because it circumvents this default behavior.
In my scenario, my code sleeps a second before presenting a UIAlertController from within didSelectPerson. This is a hack that depends on the ABPeoplePickerNavigationController dismissal animation taking less than a second. For me, if this alert is not displayed, nobody would even notice this was a problem.
If you want something more robust, you may want to override viewDidAppear to handle this special case (using a flag in your presenting view controller). But this gets a bit clumsy as well.

Add subview to active viewController from AppDelegate

I'm creating a application with an sqlite database. I update the database with information retrieved from a web service. I call the web service from AppDelegate like this:
-(void)applicationDidBecomeActive:(UIApplication *)application{
// get new information from web service
// update sqlite database
}
The web service can take quite a long time so I want to add a UIActivityIndicatorView to whatever viewController that is active. And of course, remove it when then update is done.
Ideas?
- (void)applicationDidBecomeActive:(UIApplication *)application
{
testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 420, 480)];
testView.backgroundColor = [UIColor redColor];
[self.window.rootViewController.view addSubview:testView];
}
Then whenever you are done do:
[testView removeFromSuperview];
In each of your view controllers (or common super class view controller) listen for UIApplicationDidBecomeActiveNotification notification which is being sent when application becomes active.
This way you can have custom logic for each view controller without coupling app delegate to other parts of your code by using lots of ifs (if you need to distinguish which view controller is active one).
Because your view hierarchy can take any form, tracking the currently visible view controller is something you will have to do yourself. The exception: if everything in your app happens within a UINavigationController, you can always get the current view controller with this.
[navigationController topViewController];
If all you need to do is present an overlay, however, you can just add that view to the root view controller's view; all child view controllers are just presented as subviews anyway.
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self.window.rootViewController.view addSubview:activityIndicator];
[activityIndicator startAnimating];
Manually throwing views into the mix like this is not very elegant, though. I would consider posting notifications when various network events occur, and letting each view controller update itself appropriately.

Instant / Autosave in IOS

For background: I'm a Windows automation and data translation "expert" (or so they say grins) in my day job. I've been dabbling with Objective-C coding off and on since I bought my first Mac in 2004.
I'm working on an IOS app. My data container class knows how to save and load from disc, and each object responds to an instance method of -(void)saveToImpliedFilename{} or -(void)save:(NSString *)filename {}. There's a static call to load the data files from storage and create distinct data objects from them (they're fairly lightweight objects, so I'm not worried about loading several at a time). The app's domain is such that many of them won't ever be loaded at once anyway.
+(NSArray *)loadData {}
That's all working fine and wonderful. In storage the objects are stored as Xml and life is good.
Where I'm having trouble is when trying to modify the tutorials so that two things happen for me:
Quick note: I'm using the tutorial as a basis for POC coding, then I'll go back and start over with the "real" coding, reusing my data objects and some of the other utility I've built along the way.
Here's my list of goals and the issues:
I want the table view to tell the data objects to save at pretty much every "edit" event. The only one I can consistently get to work is reorganizing the table's order. (the save button and adding a new entry works fine)
entering a new entry into the list creates a nice modal editor with a save and a cancel button which work wonderfully. But if I edit an existing entry, I can't reproduce the save buttons' behaviors. Each time I try, the buttons' events no longer fire. I can't figure out where I'm going wrong.
I'm using the "Editable Table View" project from this tutorial series as my basis: http://www.aboutobjects.com/community/iphone_development_tutorial/tutorial.html
In the following code, the [self isModal] test is where the save/cancel buttons are made visible and wired up. Bringing up the new-entry screen is apparently the only time it's modal. I tried wiring this stuff up so that the buttons were created all the time, but again, the events never fire for either one. The next block below is where the editable table view is called explicitly with the NEW functionality, but the nonModal view of the same tableview is called by the select event on the selector table.
So...
// code snipped for the new/modal editor
- (void)viewDidLoad {
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
// If the user clicked the '+' button in the list view, we're
// creating a new entry rather than modifying an existing one, so
// we're in a modal nav controller. Modal nav controllers don't add
// a back button to the nav bar; instead we'll add Save and
// Cancel buttons.
//
if ([self isModal]) {
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self
action:#selector(save)];
[[self navigationItem] setRightBarButtonItem:saveButton];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancel)];
[[self navigationItem] setLeftBarButtonItem:cancelButton];
}
// do stuff here to display my object...
}
// this code is called from the selection table to explicitly add a new data object.
- (void)add {
vhAddVehicleViewController *controller = [[vhAddVehicleViewController alloc] initWithStyle:UITableViewStyleGrouped];
id vehicle = [[Vehicle alloc] init];
[controller setVehicle:vehicle];
[controller setListcontroller:self];
UINavigationController *newNavController = [[UINavigationController alloc] initWithRootViewController:controller];
[[self navigationController] presentViewController:newNavController animated:YES completion:nil];
}
// this is where it's called on the table selection to show the same view without the save/cancel buttons.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
vhAddVehicleViewController *controller = [[vhAddVehicleViewController alloc] initWithStyle:UITableViewStyleGrouped];
NSUInteger index = [indexPath row];
id vehicle = [[self vehicles] objectAtIndex:index];
[controller setVehicle:vehicle];
[controller setTitle:[vehicle Vehiclename]];
[[self navigationController] pushViewController:controller animated:YES];
}
I'm assuming the issue is that presenting it makes it modal, where as pushing it doesn't...? That's fine. But when I take out the test for modal to try to keep the buttons working, no joy. The buttons draw and click when tapped, but the events don't fire.
HALP! :-)
Thanks much.
-- Chris (I logged in with my Google account so at the top of the page I'm showing as "user1820796") shrug
You forgot to call [super viewDidLoad];
Update
Try removing the cancel button that goes on the left side when pushing the view controller. See if save starts working. I think the problem is you should not add a left button to the navigation bar when the view controller is pushed.
Which method signature are you using?
- (void)save
{
NSLog(#"Saving");
}
Or
- (void)save:(id)sender
{
NSLog(#"Saving");
}
I still think this was related to push/popping the view rather than presenting the view. I switched it all to presentation and it's working how I want now.
Thanks for the assistance guys. Quite a different paradigm than I'm used to on the GUI stuff, but I'm getting there.
thanks!

Which is the best implementation to allege code for all viewXXX selectors?

first of all: my question is very theoretical. Even if I post an example, I just want to know which implementation is the best one to solve this kind of problem. Maybe you will laugh when you read this question because it is very fundamentally - but I want to understand how to deal with such a situation.
Imagine the following: You have got an application which communicates with an extern API via XML. As a fact of this the view cannot appear immediately, because the API needs time to react. My idea was to implement a subview which contains a loading animation. When the API sends a response this subview is removed and the main view appears. Here is my example:
- (void)viewWillAppear:(BOOL)animated {
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[myView setBackgroundColor:[UIColor whiteColor]];
myView.tag = 1;
UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
loadingView.frame = CGRectMake(145, 160, 25, 25);
[myView addSubview:loadingView];
[self.view addSubview:myView];
[loadingView startAnimating];
}
- (void)viewDidAppear:(BOOL)animated
{
[self loadXMLData];
[self.tableView reloadData];
[[self.view viewWithTag:1] removeFromSuperview];
}
Everthing works fine. My problem is the following: it's not only this view where I have to do that. My applications consists of many views, so what is the best way to avoid repeating this code? I thought about following: I modify the (UIKit UIViewController) viewDidAppear selector and put the code in it. I don't know if Apple allows to change their frameworks. Furthermore it looks very dirty to me ;o) Is someone able to tell me how this is usually done? Thank you!
( hope you understand me, my first language is not English :-( )
Why not just make a subclass of UIViewController which has this code implemented in viewWillAppear and viewDidAppear, and then inherit every other view controller in the application from this "base" subclass rather than UIViewController? That will be much easier than trying to do anything to UIViewController directly.

UINavigationController stops pushing view

We use a navigation controller and a view controller to display a question to the user. Everything has been working fine but we made some UI adjustments so we can port the application to iPad, the only changes were to make the frame of the table view dynamic to be either on iphone or ipad. However now when we get to the 187 question out of 335 it doesn't push the new question anymore... it pushes a blank screen and the "viewDidLoad" method of the pushed view controller is never called, as it has been the past 187 times. We have setup break points to make sure the navigation controller and view controller are still be allocated in memory and they are.
Here is the viewDidLoad of the view controller that gets called every new push...
- (void)viewDidLoad {
_tableView = [[QuestionTableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
_tableView.center = CGPointMake(self.view.center.x, self.view.frame.size.height/2);
[_tableView setDataSource:self];
[_tableView setQuestionDelegate:self];
[_tableView setDelegate:self];
_tableView.scrollEnabled = YES;
[_tableView setBackgroundView:[[[UIView alloc] init] autorelease]];
_tableView.directionalLockEnabled = YES;
_tableView.delaysContentTouches = NO;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.opaque = NO;
[self.view addSubview:_tableView];
}
We push the the view controller by...
[questionsNavigationController pushViewController:viewController animated:YES];
Thanks in advanced! :)
If you make the tableview smaller you can get through the entire set of 335 questions ? Are you creating a ViewController per question ?
You could run the project with instruments to check for a memory leak.
Do you really need all questions on the stack? How about a popViewControllerAnimated:NO before pushing the next one, also with animated:NO?
It works on the simulator because it's memory is the PC's memory. Put an NSLog into your -didReceiveMemoryWarning method to see it running out of memory.
Probably you shouldn't use NavigationController this way. It's.. ugly.
I would do one of the following:
"pop" ViewControllers that are 5-10 views behind (using setViewController). In other words - maintain 5-10 views behind, the others will be freed (and the result saved). Once the user decides to get back (and there are 3-4 views in the stack), reconstruct few more.
implement the NavigationController behaviour yourself - just replace the views instead of stacking them. Once the user gets back, reload the view with the needed data.
If you realy think that your implementation is ok - try to free as much possible memory from your view, once things get hot.