Showing posts with label ios. Show all posts
Showing posts with label ios. Show all posts

Monday, March 11, 2013

Data persistence in iOS Development

Ways to persist data
*NSUserDefaults -- save user data in property list. Non-sensitive info, e.g. user settings
*SQLite 3 -- embedded data in iOS
*Core Data -- object oriented for SQLite3
*Keychain access -- for sensitive information

----------------------------
NSUserDefaults

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Read from NSUserDefaults object
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    _myTextField.text = [defaults objectForKey:@"TextFieldText"];
    
    NSNumber *switchNumValue = [defaults objectForKey:@"SwitchValue"];
    _mySwitch.on = [switchNumValue boolValue];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)saveButtonPressed:(id)sender {
    
    // Get a reference to NSUserDefaults object
    // alloc init doesn't work because NSUserDefaults is a singleton
    // every single app only has 1 instance of NSUserDefaults
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    [defaults setObject:_myTextField.text forKey:@"TextFieldText"];
    [defaults setObject:[NSNumber numberWithBool:_mySwitch.on] forKey:@"SwitchValue"];
    // need to cast the switch value to number because NSUserDefaults only accept string or int
    
    [defaults synchronize]; // this is to write changes to disk
}
@end


---------------------------------
Keychain access

References:
http://software-security.sans.org/blog/2011/01/05/using-keychain-to-store-passwords-ios-iphone-ipad/
http://developer.apple.com/library/ios/#documentation/Security/Conceptual/keychainServConcepts/02concepts/concepts.html%23//apple_ref/doc/uid/TP30000897-CH204-TP9 

"KeyChainItemWrapper" package downloadable from Apple
http://developer.apple.com/library/ios/#samplecode/GenericKeychain/Listings/Classes_KeychainItemWrapper_h.html

After the login button is pressed

KeychainItemWrapper *keychainItem = [AppDelegate keychainItem];
[keychainItem setObject:username forKey:(__bridge id)(kSecAttrAccount)];
[keychainItem setObject:password forKey:(__bridge id)(kSecValueData)];

AppDelegate.h
// declare static method for KeyChainItemWrapper class
+ (KeychainItemWrapper *)keychainItem;


AppDelegate.m
// Instantiate KeyChainItemWrapper class as a singleton through AppDelegate
static KeychainItemWrapper *_keychainItem;

@implementation AppDelegate

+ (KeychainItemWrapper *)keychainItem{
    return _keychainItem;
}



Sunday, March 10, 2013

Delegation

UIScrollViewDelegate


The methods declared by the UIScrollViewDelegate protocol allow the adopting delegate to respond to messages from the UIScrollView class and thus respond to, and in some affect, operations such as scrolling, zooming, deceleration of scrolled content, and scrolling animations.

--------------------------


@implementation ViewController{
    UIImageView *myImageView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    
    // Create UIImageView to store image
    myImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nature-wallpaper-hd1.jpg"]];
    
    // Set up scroll view content size to the size of the image
    self.myScrollView.contentSize = myImageView.frame.size;
    
    // Set yourself as delegate
    self.myScrollView.delegate = self;
    
    // Set the max zoom scale
    self.myScrollView.maximumZoomScale = 4.0f;
    
    // Add the image view onto scrollview
    [self.myScrollView addSubview:myImageView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIScrollViewDelegate methods
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    NSLog(@"The x offset of scroll is %f and y offset is %f", scrollView.contentOffset.x, scrollView.contentOffset.y);
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return myImageView;
}

iOS Development - Navigation, Tab, Scroll

Intro to Navigation Controller
// Create instance of navigation controller
self.navController = [[UINavigationController alloc] init];
    
// Create instance of First View Controller
FirstViewController *firstVC = [[FirstViewController alloc]init];
    
// Push firstVC onto navigation stack
// animated set to NO so that you don't see the page sliding on first load
[self.navController pushViewController:firstVC animated:NO];
    
// Push entire stack onto window (canvas)
[self.window setRootViewController:self.navController];

----------------------------------------

Going to another page from a button in firstVC

- (IBAction)buttonPressed:(id)sender {
    
    // Push in 2nd view controller
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:secondVC animated:YES];
    
}

----------------------------------------

Intro to Tab Bar Controller

// Initialisation
    self.tabBarController = [[UITabBarController alloc] init];
    RedViewController *redVC = [[RedViewController alloc]init];
    BlueViewController *blueVC = [[BlueViewController alloc]init];

    NSArray *vcArray = [NSArray arrayWithObjects:redVC, blueVC, nil];
    [self.tabBarController setViewControllers:vcArray];
    [self.window setRootViewController:self.tabBarController];

----------------------------------------

Putting a Nav View Controller in a Tab Bar Controller

AppDelegate.m
    self.tabBarController = [[UITabBarController alloc] init];
    self.whiteNavController = [[UINavigationController alloc] init];
    
    RedViewController *redVC = [[RedViewController alloc]init];
    redVC.tabBarItem.title = @"Red";
    redVC.tabBarItem.image = [UIImage imageNamed:@"all.png"];
    
    BlueViewController *blueVC = [[BlueViewController alloc]init];
    blueVC.tabBarItem.title = @"Blue";
    blueVC.tabBarItem.image = [UIImage imageNamed:@"search.png"];
    
    WhiteViewController *whiteVC = [[WhiteViewController alloc]init];
    whiteVC.tabBarItem.title = @"White";
    whiteVC.tabBarItem.image = [UIImage imageNamed:@"faves.png"];
    [self.whiteNavController pushViewController:whiteVC animated:NO];
    
    NSArray *vcArray = [NSArray arrayWithObjects:redVC, blueVC, self.whiteNavController, nil];
    [self.tabBarController setViewControllers:vcArray];
    [self.window setRootViewController:self.tabBarController];

-------------------------------------

Scroll View
    // Create UIImageView to store image
    UIImageView *myImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nature-wallpaper-hd1.jpg"]];
    
    // Set up scroll view content size to the size of the image
    self.myScrollView.contentSize = myImageView.frame.size;
    
    // Add the image view onto scrollview
    [self.myScrollView addSubview:myImageView];


Monday, March 4, 2013

Objective-C 101


 NSString *myString = @"This is my NSString";
    NSString *myString2 = @"This is another NSString";

    NSLog(@"I have just NSLogged this string: %@ ++ %@", myString, myString2);
    
    int i = 10;
    float x = 17.6;
    NSLog(@"This is another NSLog string with %i and %f", i + 200, x);
    
    NSString *userString = @"28";
    NSLog(@"The sum of userString and 10 is %i", [userString intValue] + 10);
    
    NSString *fourthObject = @"Fourth";
    NSArray *myArray = [NSArray arrayWithObjects:@"First", @"Second", @"Third", fourthObject, nil];
    NSLog(@"My array is %@", myArray);
    NSLog(@"The 2nd item in the array is %@", [myArray objectAtIndex:1]);
    
    NSMutableArray *myMutantArray = [NSMutableArray arrayWithObject:@"One"];
    [myMutantArray addObject:@"Two"];
    NSLog(@"My updated array becomes %@", myMutantArray);
    NSLog(@"Location of 'two' is %i",[myMutantArray indexOfObject:@"Two"]);
    [myMutantArray addObjectsFromArray:myArray];
    NSLog(@"My updated array becomes %@", myMutantArray);
    
    NSDictionary *gameData = [NSDictionary dictionaryWithObjectsAndKeys:
                              @"9340", @"highScore",
                              @"10", @"numLivesLeft",
                              @"plasma rifle", @"currentWeapon", nil];
    NSLog(@"Printing game data...%@", gameData);
    NSLog(@"What is my current weapon? %@", [gameData objectForKey:@"currentWeapon"]);

- (IBAction)buttonPressed:(id)sender {
    
    int r = rand();
    
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Hello World Title!"
                          message:[NSString stringWithFormat:@"Congrats! I have launched the msg!This is my random number: %i", r]
                          delegate:nil
                          cancelButtonTitle: @"CancelButton"
                          otherButtonTitles: nil];
    
    [alert show];
}


@interface Zombie : NSObject

@property NSString *name;
@property int age;
@property NSString *characteristics;
    
@end


Zombie *aZombie = [[Zombie alloc] init];
    [aZombie setName:@"Ted"];
    aZombie.age = 12;
    //[aZombie setAge:12];
    [aZombie setCharacteristics:@"likes to eat plants"];
    NSLog(@"The name of the Zombie is %@!", [aZombie name]);
    NSLog(@"%@ is %i years old - as such the hit points are %i", aZombie.name, aZombie.age, [aZombie getHitPoints]);
    NSLog(@"%@", [aZombie characteristics]);