Wednesday, May 28, 2014

How to change status bar appearance in iOS 7

If your iOS applications minimum deployment target is iOS7.0 and above and if you have a dark background in your app then you can’t see the status bar text, since in iOS 7.0 and above status bar is transparent by default.
If the background of your view is black(or any dark color) then your status bar will not be visible, and looks something like below snapshot,

So here is a way to make your status bar text visible in dark background,
1) Add "View controller-based status bar appearanceproperty (key is UIViewControllerBasedStatusBarAppearance) to your applications info.plist and set the value to NO.

2) Go to your applications AppDelegate.m file and add the below code snippet in didFinishLaunchingWithOptions method,
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

If you have done with the above 2 points then you are 90% done except your status bar will not be visible during the time of splash screen display, 
To make your status bar visible when splash screen is displaying with dark background, just do the below mentioned step,

3) Add "Status Bar Style" property (key is UIStatusBarStyle) to your applications info.plist and set the value to “Transparent black style (alpha of 0.5)” (key is UIStatusBarStyleLightContent).

and we are 100% done now,,, :)

Now if you run your app you can able to see status bar as shown in the below snapshot,

Hope this post is helpful,any comments or suggestions are acceptable.

Wednesday, May 21, 2014

How to manage memory and enhance performance of an iOS app

One of the major things in any mobile application development is a memory management and performance enhancement,

Here is an instant guide or list of few crucial points w.r.t. enhancing our apps memory management, in turn performance optimizations in our iOS application, which I came across in my daily life and spent lot of time in fixing it, thought it would be great or it may save someones valuable time and energy if I share my note on this regard,

Here we go,
1) Better to make all IBOutlet objects as __weak reference in ARC environment. Except for those from file’s owner to top-level objects in a xib(nib or a storyboard scene) file which should be strong. Outlets that you create typically should be weak by default.

2) Instead of calling init constructor method every time when pushing/navigating to a view controller/view call init only once and until and unless the viewController object reference exists in memory use the same reference to navigate.

Ex:-  Suppose we are navigating from Home screen to a DetailViewController then create an iVar of DetailViewController globally and in your push method check for this iVar reference in memory and navigate like,
            detailVCObj = [[DetailViewController alloc] initWithNibName:@”DetailViewController” bundle:nil];//if object reference is not their in memory then only alloc and initilize
[self.navigationController pushViewController:detailVCObj animated:YES];

and suppose if you want to set some property values before pushing a view controller then create your custom getter and setter methods for those properties and call those methods before pushing view whenever you are following the above protocol.
By doing this(by following this protocol) it will optimize memory consumption during runtime. Follow this protocol even in ARC enabled projects.

        3) In ARC enabled projects, assign nil to all re-creatable objects and iVars after making use of these objectsso as to handle memory in an optimized manner.

In MRC (Manual Reference Counting) type of projects release re-creatable objects and iVars and then assign nil, after making use of these objects.
Assigning nil is equivalent to release and dereference in ARC.
a) In ARC enabled environment,  myiVar = nil;
b) In MRC environment,
[myiVar release];//releasing  object
myiVar = nil;//dereferencing the object

4)  Avoid using images up to the maximum extent in your app, and try to achieve the design by using colors wherever and whenever it is possible for you.

Since this will optimize both time complexity (load time for image is more compared to a UIColor) and space complexity (size of .ipa file will be reduced drastically if we avoid images).
If you are using images then based on your requirement and particular instance which UIImage method to use? refer this link

5) Make use of Singleton design pattern in your application wherever it is required and can be used.

Ex:- Suppose if you are calling some of the methods of a class frequently in your app, like a database model class in your app will be called frequently for either to insert or to fetch or to update different tables and its entities. Make this class as a singleton class so that even if you make n number of calls to this class methods single object will manipulate the functionality.
For more information on important design patterns in iOS refer this link

6) Whenever you are making a core data fetch call use includesPropertyValues where ever possible, refer this link for further details on this API.

7) Keep track of temporary memory buffers and reuse it, and don’t keep unused memory, free it up as soon as you are done using it.

8) iOS will automatically kills the background processes and apps which consumes more memory and causes memory pressure during low memory conditions, its every programmer responsibility to handle low memory conditions inside our app as-well.

During low memory conditions iOS UIKit framework will send few notifications to our app,
- Implement the applicationDidReceiveMemoryWarning: method of your application delegate.
- Override the didReceiveMemoryWarning method in your custom UIViewController subclass.
- Register to receive the UIApplicationDidReceiveMemoryWarningNotification notification.

Upon receiving any of these notifications, your handler method should respond by immediately removing strong references to objects
View controllers automatically remove references to views that are currently offscreen, but you should also override the didReceiveMemoryWarning method and use it to remove any additional references that your view controller does not need.

9) Use Instruments and Static Analyzer Xcode tools to check your app specific memory stats, and take further actions w.r.t. memory allocations, memory pressure and memory leaks.

Hope this post was helpful, any comments or suggestions are acceptable.