Monday, 16 May 2011

A Better NSLog 'ing

i have always found the built in NSLog(fmt, ...) a bit too limited in functionality.  it is really a pain if one use it in a bit moderate project with Console full of swinging lines in a debug mode.
so i decided to search and use a better NSLog method. i found many alternatives but to me the following 2 stand out of the crowd as a winners.
  1. Objective-C Logging with SOSmax
  2. NSLogger
The main killing feature of these two projects was to use it on networking. But you may wonder how this can be the the feature. but let me tell you it could be the life saver.
Consider a scenario where you are using threads in the app and you are starting and stopping threads at your will. The threads are stopping in the debug binary but when you prepare a adHoc binary and install it using iTunes on the phone, somehow the apps start misbehaving. on further investigation you suspect  that threads are not being stopped normally. at that time you will badly want to debug the binary in the adHoc distribution. but its not possible using the built in NSLog. at that time the Networking debug mode will come at rescue.
i was once in this scenario and at that time i wrote my own little logging utility which sends the logs over the network to the little mac app. but these two logging frameworks are too good to compare to that.

Here is the overview so some of the great features it offers

  • Debugging over network.
  • Different levels of logging.
  • Turning on/off logging in release vs debug mode.
  • Color highlighting for different levels of logging in SOSmax.
  • UIImage Logging in NSLogger, yes yo listened it right image logging.
  • File specific scope for logging in SOSmax.

i personally prefer SOSmax because it offers too much but one feature that i still want in SOSmax  is the UIimage/binary data logging of NSLogger. Expect that its perfect !

Sunday, 17 April 2011

Advance Localization in ios apps

Localization in ios is usually done using this way. But the main problem with this approach is that to view the application in other language the language has to be changes from the settings of the ios device.
In this post i will present an approach in which you can set the language of the application from within the application.
but before starting lets understand the current localization procedure. we use NSLocalizedString() to get the localized version of any string, which acts as a key to match the key value pair in the localized version of Localizable.strings file.
what infact NSLocalizedString() calls in background is [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil] where [NSBundle mainBundle] is the default main bundle.
The trick to use is to get the value of localizedStringForKey from the bundle of your choice. So what we will do is that depending on the language selected we will select the specific bundle and use the [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil] to get the localized value of the string.
so lets get started

1- Localize your application using the method describe here. i recommend using this method just so that you can use "genstrings" to create string table.
2- put the localized version of string values in the localized Localizable.strings.
3- Replace the NSLocalizedString with any function name of your choice, i will be using  languageSelectedStringForKey .
4- Write a method definition and implementation of -(NSString*) languageSelectedStringForKey:(NSString*) key where it is accessible in the scope where the localized string is needed.
5- in the languageSelectedStringForKey method select the appropriate bundle depending on the language selected and call [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil] on the bundle.
for NSString *path= [[NSBundle mainBundle] pathForResource:@"fr" ofType:@"lproj"];  
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
and then get the string like this
NSString* str=[languageBundle localizedStringForKey:key value:@"" table:nil];
str here is the localized string.

again all which i described above have been put in this code and i have placed it on github for any reference.