Sunday, September 23, 2012

iOS 6.0 Orientation

In iOS6,UINavigation Controller does not consult their children to determine whether they should autorotate.In another word iOS6 handle orientation by top-most controller.

The shouldAutorotateToInterfaceOrientation: method of UIViewController is deprecated. In its place, you should use the supportedInterfaceOrientations and shouldAutorotate methods.

iOS6 has three new method to handle orientation
// what we support
- (NSUInteger)supportedInterfaceOrientations 
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

// should autorotate or not
- (BOOL) shouldAutorotate 
{
    return YES;
}

//initial orientation we want to have
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{
    return UIInterfaceOrientationPortrait;
}
But where should we write those methods?

Saturday, September 22, 2012

How to use NSUserDefaults


AppDelegate.m
//Use NSDate to determine that App first run or not
NSString *dateKey = @"dateKey";
NSDate *lastRead = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:dateKey];

// If lastRead is nil,App first run: set up user defaults.
if (lastRead == nil)
{
    NSDictionary *appDefaults  = [NSDictionary dictionaryWithObjectsAndKeys:[NSDate date], dateKey, nil];
        
    // do any other initialization you want to do here - e.g. the starting default values.
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"should_play_sounds"];
        
    // sync the defaults to disk
    [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
    [[NSUserDefaults standardUserDefaults] synchronize];
 }

[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:dateKey];