Sunday, January 12, 2014

NSInvalidArgumentException that propagated all the way to my main class.

If you are here, you  probably got a NSInvalidArgumentException. Below is what I got, and even if your one is not the same, hopefully the explanation will help you solve yours:



Here is why this came all the way up:
- My application uses a documentInteractionController

  • It resides in a class
  • Helps to figure out 3rd party applications on my iOS device I can use to open certain files from my application
  • once it does and I go back to my application... bang ! my app exists, mugshot of the culprit is above
This is because the documentInteractionController fires callbacks, and unfortunately in my application the class was released and no longer available. Since the class is no where to be found, the OS cannot call the callback methods and we get the above error. (callbacks are methods that fire after certain events, in this case the method "didEndSendingToApplication" fires to let us know that the 3rd party application got my file)

-To rectify:
  • I had to retain the documentInteractionController (and also the parent view due to some specifics of my application)
  • Release it after the callbacks were fired.
Read a bit on memory management in iOS and ARC (automatic reference counting)


You basically the 'retain' keyword to keep your objects around and then 'release' it after you are done. 
Make sure you cover all paths if you do so, you are interfering with the memory management, so cover your tracks well and avoid memory leaks. Even though you might not see any visible side effects,  we should always write quality code :) 

Hope this helps someone :)

iOS 7 has added a background colour to your table cell


In iOS 7 we noticed a white background for the UITableView we were using. Whereas on iOS 6 the same code gave us a transparent background, thus enabling the background image to be visible as required by the App's owner.
This was a change done in iOS 7, so if you want to support it, do a version check and something similar to the following:  

//  (iOS 6 cell is transparent by default not on iOS 7).

      //do a version check, if it is iOS 7 run the below code
        cell.backgroundColor = [UIColor clearColor];
    


You can use a callback method such as the one below to add your code if you are not sure where to place it:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
     if([AppDelegate iSIOS7OrAbove]){  // iSIOS7OrAbove is not a method that comes out of the box. write the logic you want in a custom method to do the check.
        cell.backgroundColor = [UIColor clearColor];
    }

}


Hope this helped someone :)