Friday, September 28, 2012

Local and Push Notifications

Difference between Local notifcation and Push notification

Local notifications are scheduled by an application and delivered by ios on the same device.
Its available only in iOS.

Push notifications are sent by application's remote server to Apple Push Notification Service (APNS) which pushes notifications to device on which application is installed.

Refer:  Tutorial on http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12  about how push notification works.

Wednesday, September 26, 2012

openURL for SMS,Mail,

openURL helps you launch Maps, SMS, Browser, Phone, and even other applications.  

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8004664411"]];

To check out the URL Schemes for ios
http://wiki.akosma.com/IPhone_URL_Schemes

Animation using GIF Images

The standard [UIView addSubview:gifAnimatingImages] wont do the animation.To achieve this there is a tweak in UIImageView properties.Code goes by this:


// create the view that will execute our animation
UIImageView* campFireView = [[UIImageView alloc] initWithFrame:self.view.frame];
 
// load all the frames of our animation
campFireView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"campFire01.gif"],
[UIImage imageNamed:@"campFire02.gif"],
[UIImage imageNamed:@"campFire03.gif"],
[UIImage imageNamed:@"campFire04.gif"],
[UIImage imageNamed:@"campFire05.gif"],
[UIImage imageNamed:@"campFire06.gif"],
[UIImage imageNamed:@"campFire07.gif"],
[UIImage imageNamed:@"campFire08.gif"],
[UIImage imageNamed:@"campFire09.gif"],
[UIImage imageNamed:@"campFire10.gif"],
[UIImage imageNamed:@"campFire11.gif"],
[UIImage imageNamed:@"campFire12.gif"],
[UIImage imageNamed:@"campFire13.gif"],
[UIImage imageNamed:@"campFire14.gif"],
[UIImage imageNamed:@"campFire15.gif"],
[UIImage imageNamed:@"campFire16.gif"],
[UIImage imageNamed:@"campFire17.gif"], nil];
 
// all frames will execute in 1.75 seconds
campFireView.animationDuration = 1.75;
// repeat the annimation forever
campFireView.animationRepeatCount = 0;
// start animating
[campFireView startAnimating];
// add the animation view to the main window 
[self.view addSubview:campFireView];
[campFireView release]; 

Refer :
http://www.alterplay.com/ios-dev-tips/2010/12/making-gif-animating-on-ios.html
http://appsamuck.com/day2.html

Tuesday, September 18, 2012

Localization folder for 2 different chinese versions

Refer : http://stackoverflow.com/questions/10125241/ios-localization-particularly-of-folders

For Traditional chinese folder is zh-Hant.lproj
For Simplified chinese folder is zh-Hans.lproj

Monday, September 17, 2012

Status bar hidden

To make the status bar hidden initially on loading of the app,we have to do three steps
1.open the urAppname.info-plist and add a row with key "Status bar is initially hidden".set its value to YES.

2.open the viewController.xib file and select the view then go to attributes and make "Status Bar" to None value.

3.Final step is in AppDelegate.m add the following line under application : didFinishLaunchingWithOptions:
  [[UIApplication sharedApplication]setStatusBarHidden:YES];

Thursday, September 13, 2012

Web service request in applicationDidEnterBackground

applicationDidEnterBackground: will always be called when the home button is pressed.I had an issue of indicating the server that "user is not using the app right now"  anytime whenever user presses home button.To do this i need to call web service request inside applicationDidEnterBackground: i used NSURLConnection inside it.
Look at the following method to get a clear pic on it


- (void)applicationDidEnterBackground:(UIApplication *)application
{
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    
    [request setURL:[NSURL URLWithString:urlString];
    
    [request setHTTPMethod:@"POST"];
    
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    
    NSDictionary *results = [json_string JSONValue];
    
    NSLog(@"BG task:%@",results);

}

response will have the result of the request handled.

Note:

sendSynchronousRequest:returningResponse:error:

Performs a synchronous load of the specified URL request.
A synchronous load is built on top of the asynchronous loading code made available by the class. The calling thread is blocked while the asynchronous loading system performs the URL load on a thread spawned specifically for this load request. No special threading or run loop configuration is necessary in the calling thread in order to perform a synchronous load.






Wednesday, September 12, 2012

Localization of ios app

So far the apps i have written are available only in English,what if a korean user who wants to see and understand my app in korean language.Only two options are available for this user either he should learn English to use this app or choose a similar app which shows korean language too.He picks the most easy option of preferring second option.I don't want to lose the user just because am not showing korean nativity in app then lets include korean,chinese,spanish,french,u name the language we have in our app.

This is called localization of app.Making ios app work with multiple languages is precisely called Localization or rather  with localization we can say let's display text in user's preferred language.

Method 1:
The user can change the language by going to Settings -> General -> International -> Language. When the language is changed, the iPhone also changes the name of the application, only if the application supports it.
Notes from the http://www.sortedbits.com/localization-of-your-io-app/ are given below:
CREATED LANGUAGE SPECIFIC DIRECTORIES
In my folder, where my project was located, I created 3 directories. For each language 1:
  • en.lproj : This is where the English localization files go
  • nl.lproj : This is where the Dutch localizations files go
  • de.lproj : This is where the German localization files go

CREATED STRINGS FILES

In each of those directories I created 2 strings files, I did this by right-clicking the resources folder in Xcode, select Add -> New File. Then click the Resourcetag under Mac OS X and select Strings file.
When the New File dialog comes up, make sure you select the right location. You want to create 2 files per language directory you created with the step mentioned above. Name the files:
  • InfoPlist.strings : This file I created to be able to localize my application name
  • Localizable.strings : This file contains all the text my application is showing, in the appropriate language
Mind the capitalization, this is very important because we are running on a case sensitive file system.

FILL YOUR LOCALIZABLE.STRING FILE

What you need to do now is maybe a little bit of hassle, but you need to copy all the string you used in your application to this application and give them keys by which you can identify them. With keys I mean unique keys.
For example, my localization file in the directory nl.lproj looked a bit like this:
"Add new product" = "Voeg product toe";
While my localization file in the de.lproj looked like this:
"Add new product" = "Neue produkt";
I think you get the point.

ACTUALLY DISPLAY THE STRINGS

Now, after you spend hours filling your localization files and translating it to every language known to man you want to actually display some text in your application. Well that is pretty easy.
- (void) doSomeStuff
{
 myLabel.text = NSLocalizedString(@"Add new product", @"");
}
Whenever a language file for the users language could not be found, it will default back to the key and show that instead. So make sure your keys also say what you want your button/label/etc to say. That’s it! There isn’t more to it than that.

BUT WHAT ABOUT MY APPLICATION NAME?

I almost forgot. Remember that you created 2 files for each language? Well, just put this line in your InfoPlist.strings files (ofcourse with the translated application names).
CFBundleDisplayName = "My App Name";
Hope this helps you out.


Method 2:
Set the language of the application with in the application
Current procedure is we usNSLocalizedString() 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.
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.

This is how we do it:

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.
e.g.
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.

Monday, September 10, 2012

Singleton Design Pattern

Singleton Pattern is a design pattern that restricts the instantiation of a class to one object.
This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects.
Refer the link: http://www.galloway.me.uk/tutorials/singleton-classes/

Frame and Bounds

Difference between Frame and Bounds

courtesy: http://stackoverflow.com/questions/1210047/cocoa-whats-the-difference-between-the-frame-and-the-bounds


The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).
The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.
So, imagine a view that has a size of 100x100 (width x height) positioned at 25,25 (x,y) of its superview. The following code prints out this view's bounds and frame:
// This method is in the view controller of the superview
- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"bounds.origin.x: %f", label.bounds.origin.x);
    NSLog(@"bounds.origin.y: %f", label.bounds.origin.y);
    NSLog(@"bounds.size.width: %f", label.bounds.size.width);
    NSLog(@"bounds.size.height: %f", label.bounds.size.height);

    NSLog(@"frame.origin.x: %f", label.frame.origin.x);
    NSLog(@"frame.origin.y: %f", label.frame.origin.y);
    NSLog(@"frame.size.width: %f", label.frame.size.width);
    NSLog(@"frame.size.height: %f", label.frame.size.height);
}
And the output of this code is:
bounds.origin.x: 0
bounds.origin.y: 0
bounds.size.width: 100
bounds.size.height: 100

frame.origin.x: 25
frame.origin.y: 25
frame.size.width: 100
frame.size.height: 100
So, we can see that in both cases, the width and the height of the view is the same regardless of whether we are looking at the bounds or frame. What is different is the x,y positioning of the view. In the case of the bounds, the x and y coordinates are at 0,0 as these coordinates are relative to the view itself. However, the frame x and y coordinates are relative to the position of the view within the parent view (which earlier we said was at 25,25).
To know more about view's frame and bounds must-refer-presentation slides here:
http://www.slideshare.net/onoaonoa/cs193p-lecture-5-view-animation

Sunday, September 9, 2012

Atomic vs Non-atomic

Atomic:
Is DEFAULT Behaviour
NOT Fast

Non-atomic:
NOT Default Behaviour
Fast

Example:

 Suppose there is an atomic string property called "name", and if you call [self setName:@"A"] from thread A, call [self setName:@"B"] from thread B, and call [self name] from thread C, then all operation on different thread will be performed serially which means if one thread is executing setter or getter, then other threads will wait. This makes property "name" read/write safe but if another thread D calls [name release] simultaneously then this operation might produce a crash because there is no setter/getter call involved here. Which means an object is read/write safe (ATOMIC) but not thread safe as another threads can simultaneously send any type of messages to the object. Developer should ensure thread safety for such objects.
If the property "name" was nonatomic, then all threads in above example - A,B, C and D will execute simultaneously producing any unpredictable result. 
In case of atomic, Either one of A, B or C will execute first but D can still execute in parallel.