Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Last active December 14, 2015 05:40
Show Gist options
  • Select an option

  • Save lamprosg/5037295 to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/5037295 to your computer and use it in GitHub Desktop.
(iOS) - Property Lists
/******************************************************************************/
//Get Documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
//Create a file (ex. data.plist) and get a full path in the documetns directory
NSString *filename = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
//Get file path function
-(NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
}
/******************************************************************************/
/******************************************************************************/
//Writting array to the file plist
[array writeToFile:filePath atomically:YES];
/******************************************************************************/
/******************************************************************************/
//Get the tmp directory
NSString *tempPath = NSTemporaryDirectory();
//Create a file in tmp directory
NSString *tempFile = [tempPath stringByAppendingPathComponent:@"tempFile.txt"];
/******************************************************************************/
Every application has 3 folders in it.
1. Documents: Where data are stored, except NSUserDefaults–based preference settings.
2. Library: NSUserDefaults–based preference settings are stored here
3. tmp: Temporary files are stored here. Files written into tmp will not be backed up by iTunes when your iOS device syncs.
(You still need to delete these files to avoid filling up the file system.)
/******************************************/
Property lists use only these objects:
Other (or custom) objects cannot be serialized in Property Lists
Data model must use only these objects to use Property Lists (.plist files)
 NSArray
 NSMutableArray
 NSDictionary
 NSMutableDictionary  NSData
 NSMutableData
 NSString
 NSMutableString
 NSNumber
 NSDate
#import <UIKit/UIKit.h>
@interface BIDViewController : UIViewController
//Will hold all fields
@property (strong, nonatomic) IBOutletCollection (UITextField) NSArray *lineFields;
@end
@implementation BIDViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/**********************************************************************/
//Load the data every time the view is initalised
//Create and return the file path of data.plist
NSString *filePath = [self dataFilePath];
//If the file exists
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
//Start an array with the contents of data.plist
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
//Copy the contents of data.plist to the 4 text fields
for (int i = 0; i < 4; i++)
{
UITextField *theField = self.lineFields[i];
theField.text = array[i];
//This won't work
//self.lineFields[i].text=array[i];
}
}
//Get a reference to our application
UIApplication *app = [UIApplication sharedApplication];
//Subscribe to UIApplicationWillResignActiveNotification, using the default NSNotificationCenter instance
//and a method called addObserver:selector:name:object:
//addObserver: self - BIDViewController should be notified
//selector: - Call that method when the notification is posted
//name: - name of the notification that we’re interested in receiving
//object: - the object we’re interested in getting the notification from
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:app];
}
//Our application needs to save its data before the application is terminated or sent to the background
//That's what this notification is for
//If you force quit the application you won't receive this notification
- (void)applicationWillResignActive:(NSNotification *)notification
{
//Get the path
NSString *filePath = [self dataFilePath];
//Get an array with the text from each of the four fields by using valueForKey:
//on an array of instances all responding to the text method
NSArray *array = [self.lineFields valueForKey:@"text"];
//Write the contents of that array out to a plist file.
[array writeToFile:filePath atomically:YES];
}
/*******************************************************
//Methods triggered
application:didFinishLaunchingWithOptions:
applicationWillResignActive:
applicationDidBecomeActive:
applicationDidEnterBackground:
applicationWillEnterForeground:
applicationWillTerminate:
//Notification Names Respectevily
UIApplicationDidFinishLaunchingNotification
UIApplicationWillResignActiveNotification
UIApplicationDidBecomeActiveNotification
UIApplicationDidEnterBackgroundNotification
UIApplicationWillEnterForegroundNotification
UIApplicationWillTerminateNotification
*******************************************************/
-(NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment