3

I placed my code for iAd/AdMob ads in...

-(void)viewWillAppear:(BOOL)animated{}

Ads work perfectly fine the way I have them now on all iOS devices. When I connected my iPhone to Xcode and clicked on Product -->Analyze a message states...

The viewWillAppear:instance method in UIViewController subclass 'iPhoneSIX' is missing a [super viewWillAppear:] call

I just accidentally stumbled upon this Product-->Analyze thing. Do I really need to add [super viewWillAppear] even though everything works perfectly fine on all devices as it currently is. Will Apple reject my app if I don't pay attention to the Product-->Analyze issue navigator?

Also, what does ...

[super viewWillAppear:YES];

What does calling this do?

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Jet
  • 555
  • 1
  • 7
  • 19

3 Answers3

12

According to Apple: (emphasis mine)

This method is called before the receiver's view is about to be added to a view hierarchy and before any animations are configured for showing the view. You can override this method to perform custom tasks associated with displaying the view. For example, you might use this method to change the orientation or style of the status bar to coordinate with the orientation or style of the view being presented. If you override this method, you must call super at some point in your implementation.

j.f.
  • 3,908
  • 2
  • 29
  • 42
7

Apple doesn't gets that specific when deciding to Accept or Reject your app. It only follows the guidelines, which doesn't get that much into the weeds of your specific methods.

Calling [super viewWillAppear:YES] is a best practice, and I would recommend it. Always including super ensures that any code in the super classes get called before executing any additional code. So if you or someone else coded a super class that expected some code to be executed, you are guaranteed to still execute it, rather than just overwriting the whole method in the subclass.

Say you have a view controller of type MyViewController which is a subclass of UIViewController. Then say you have another view controller of type MyOtherViewController, which is a subclass of MyViewController. Say you're coding now some things in viewWillAppear in MyOtherViewController. If you call super first, it will call viewWillAppear in MyViewController before executing any code. If viewWillAppear in MyViewController calls super first, then it will call viewWillAppear in UIViewController before executing any code.

Josh Gafni
  • 2,831
  • 2
  • 19
  • 32
  • You should always pass the parameters as delivered to your overridden method whenever possible, rather than concrete values like `YES`, unless you have a very intentional reason for doing so. `[super viewWillAppear:animated]` would therefore be the best practice. – devios1 Sep 21 '15 at 17:31
0

I'm quite certain Apple will not reject your app for failing to call super on an overridden method, primarily because there are cases where you may specifically want to avoid calling super.

That said, as Josh Gafni mentions it is definitely a best practice to do so, unless you have a very good reason for not. Also bear in mind some view controller subclasses (can't recall specifically which ones, but maybe UICollectionViewController) will only work properly if their view lifecycle methods get called appropriately, so not calling super can definitely break some classes (sometimes in subtle ways you may not realize).

Therefore my suggestion is add the call to super (generally as the first line in the method) and see if things continue to work fine. If not, spend a bit of time trying to understand what is happening differently and see if you can solve it in a different way. In general you should always (as a force of habit) provide calls to super on any view lifecycle methods you override whenever possible.

devios1
  • 36,899
  • 45
  • 162
  • 260