With just a few lines of code, you can read/write collections to/from files. The code below shows examples for writing and reading back both arrays and dictionaries.
Read and Write Collections to File
|
With just a few lines of code, you can read/write collections to/from files. The code below shows examples for writing and reading back both arrays and dictionaries.
|
http://ipgames.wordpress.com/tutorials/writeread-data-to-plist-file/
First of all add a plist to your project in Xcode. For example “data.plist”.
Next, look at this code which creates path to plist in documents directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1 NSString *documentsDirectory = [paths objectAtIndex:0]; //2 NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"]; //3 NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath: path]) //4 { NSString *bundle = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; //5 [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6 } |
1) Create a list of paths.
2) Get a path to your documents directory from the list.
3) Create a full file path.
4) Check if file exists.
5) Get a path to your plist created before in bundle directory (by Xcode).
6) Copy this plist to your documents directory.
Read data:
1 2 3 4 5 6 7 8 9 |
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; //load from savedStock example int value int value; value = [[savedStock objectForKey:@"value"] intValue]; [savedStock release]; |
Write data:
1 2 3 4 5 6 7 8 9 10 11 |
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; //here add elements to data file and write data to file int value = 5; [data setObject:[NSNumber numberWithInt:value] forKey:@"value"]; [data writeToFile: path atomically:YES]; [data release] |
Remember about two things:
1) You must create a plist file in your Xcode project.
2) To optimize your app, better is to save all the data when application (or for example view) is closing. For instance in applicationWillTerminate. But if you are storing really big data, sometimes it cannot be saved in this method, because the app takes too long to close and the system will terminate it immediately.
No need to code. It can all be done from the plist:
(1) Set “View controller-based status bar appearance” to “NO”
(2) Set “Status bar style” to “Opaque black style“.
Raw values are:
UIViewControllerBasedStatusBarAppearance = NO
UIStatusBarStyle = UIStatusBarStyleBlackOpaque
http://stackoverflow.com/questions/17678881/how-to-change-status-bar-text-color-in-ios-7
In the calling controller (meaning, the parent controller where you will push the new controller), add this in viewdidload:
1 2 3 4 |
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Custom Back Title" style:UIBarButtonItemStyleBordered target:nil action:nil]; |
http://programming.mvergel.com/2012/11/change-back-button-title-in-ios.html#.UseyJXk9XfM
First check for iOS 7 then implement: self.edgesForExtendedLayout = UIRectEdgeNone;
1 2 3 4 5 |
float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; if (systemVersion >= 7.0) { self.edgesForExtendedLayout = UIRectEdgeNone; } |
Create the alertView with style UIAlertViewStylePlainTextInput:
1 2 3 4 5 6 |
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Hello!" message:@"Please enter your name:" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput; UITextField * alertTextField = [alert textFieldAtIndex:0]; alertTextField.keyboardType = UIKeyboardTypeNumberPad; alertTextField.placeholder = @"Enter your name"; [alert show]; |
This produces an alertView:
When pressing any buttons, the regular delegate methods will be called and you can extract the textInput there like so:
1 2 3 |
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"Entered: %@",[[alertView textFieldAtIndex:0] text]); } |
View rotation methods for iOS5 and iOS6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#pragma mark - View rotation methods // iOS5 support - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Support portrait only orientation for iPhone iOS5 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ // The device is an iPad return YES; }else{ // The device is an iPhone or iPod touch return (interfaceOrientation == UIInterfaceOrientationPortrait | interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown); } } // Disable autorotation for iPhone iOS6 and enable autorotation for iPad iOS6 // iOS6 support -(BOOL)shouldAutorotate { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ // The device is an iPad return YES; }else{ // The device is an iPhone or iPod touch return NO; } } // Support portrait only orientation for iPhone iOS6 and all orientations for iPad iOS6 // iOS6 support -(NSUInteger)supportedInterfaceOrientations { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ // The device is an iPad return (UIInterfaceOrientationMaskAll); }else{ // The device is an iPhone or iPod touch return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown); } } |
Make sure to give the segue an ID so that it can be found. In this example the segue ID would be “mySegueID”.
1 2 3 4 5 6 7 8 9 10 11 12 |
// prepareForSegue -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSLog(@"prepareForSegue: %@", segue.identifier); if ([segue.identifier isEqualToString:@"mySegueID"]) { // do something here... } } |
Device / Screen | File Name (PNG) | Icon Size (pixels) |
iPhone and iPod | ||
Application Icon for iPhone (retina display) | Icon@2x.png | 114 x 114 |
Application Icon icon for iPhone | Icon.png | 57 x 57 |
Settings / Spotlight icon for iPhone (retina display) | Icon-Small@2x.png | 58 x 58 |
Settings / Spotlight icon for iPhone | Icon-Small.png | 29 x 29 |
Launch image Portrait (retina display) | Default@2x.png | 640 x 960 |
Launch image Portrait | Default.png | 320 x 480 |
iPhone 5 | ||
IPhone 5 Launch image for Portrait (retina display) | Default-568h@2x.png | 640 x 1136 |
iPad | ||
Application Icon for the new iPad (retina display) | Icon-72@2x.png | 144 x 144 |
Application Icon for the iPad | Icon-72.png | 72 x 72 |
Settings / Spotlight icon for iPad | Icon-Small-50@2x.png | 100 x 100 |
Settings / Spotlight icon for iPad | Icon-Small-50.png | 50 x 50 |
Launch image Portrait (retina display) | Default-Portrait@2x.png | 1536 x 2008 |
Launch image Portrait | Default-Portrait.png | 768 x 1004 |
Launch image Landscape (retina display) | Default-Landscape@2x.png | 2048 x 1496 |
Launch image Landscape | Default-Landscape.png | 1024 x 748 |
iTunes App Store | ||
App icon for the App Store (retina display) | iTunesArtwork@2x.png | 1024 x 1024 |
App icon for the App Store | iTunesArtwork.png | 512 x 512 |
In your first view controller (where you need to be notified of some event) you implement code to register and remove notification.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
- (void)viewDidLoad { [super viewDidLoad]; // add an observer to listen for the notification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomeAction) name:@"DisplayModalControllerNotification" object:nil]; } // this action fires when we receive a notification - (void)doSomeAction { // do something here when the notification is received } // be sure to remove the notification observer - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } |
Now with the code above in place in your first view controller you will simply call it whenever you need it with this line of code which posts the notification:
1 2 |
// post a notification [[NSNotificationCenter defaultCenter] postNotificationName:@"DisplayModalControllerNotification" object:nil]; |