Friday, October 19, 2012

iOS 資料保存 Data Persistence - NSUserDefaults

NSUserDefaults (Setting)
Property List (plist)

  1. - (void)applicationDidEnterBackground:(UIApplication *)application
  2. {
  3.     NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  4.     [userDefault setObject:@"白彼得" forKey:@"name"];
  5.     [userDefault synchronize];
  6. }
  7. - (void)applicationWillEnterForeground:(UIApplication *)application
  8. {
  9.    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
  10.    NSLog(@"name %@", name);
  11. }


Thursday, October 18, 2012

iOS 資料保存 Data Persistence - Property List

NSUserDefaults (Setting)
Property List (plist)



2.由writeToFile:atomically寫出Property List

property list: 一種xml的資料格式
writeToFile: 寫檔動作,儲存plist到模擬器document資料夾的動作
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  : 模擬器/實機 的document資料夾位置
NSPropertyListSerialization: coverts Property List to/from NSData

iPhone 開發教學 - 使用 Property List 和 SQLite 處理資料儲存

Introduction to Property Lists (Apple Official)

相關指令  //codes are from 彼得潘app教學書

讀取專案裡附的檔案 (除圖片之外,如 txt 檔或影音檔)


encodeObject, decodeObjectForKey 是物件裡面的encode實作
[NSKeyedArchiver archiveRootObject: toFile:];
[NSKeyedUnArchiver unarchiveObjectWithFile:];

非特權物件的寫檔 (使用arhiver)
用NSMutableData *data來當作archiver,然後用writeToFile寫檔

// Writing
- (BOOL)writeToFile:(NSString *)aPath atomically:(BOOL)flag;
- (BOOL)writeToURL:(NSURL *)aURL atomically:(BOOL)flag;

// Reading
- (id)initWithContentsOfFile:(NSString *)aPath;
- (id)initWithContentsOfURL:(NSURL *)aURL;

特權物件可以直接用WriteToFile寫檔(只有NSString, NSData, NSDictionary, NSArray可以),而非特權物件需要先archive才可以寫檔。


    Differences between property list, NSUserDefaults

    how to save plist to NSUserDefaults

    "You can think of NSUserDefaultsas an invisible .plist that you can read and write to, without ever being able to actually see the file. Using NSUserDefaults, you will be able to restore saved values even if the app has been killed in multitasking.
    However, how you choose between .plist and NSUserDefaults should be based off of how much data you need to save. Apple recommends only saving small amounts of data to NSUserDefaults. If you need to save a lot of information then .plist is the way to go. Either that or of course Core-Data."


    Tuesday, October 16, 2012


    # ECSlidingViewController `ECSlidingViewController` is a view controller container for iOS that presents its child view controllers in two layers. It provides functionality for sliding the top view to reveal the views underneath it. This functionality is inspired by the Path 2.0 and Facebook iPhone apps.
    ECSlidingViewController Demo from EdgeCase on Vimeo.
    This project is an example app that showcases the uses for `ECSlidingViewController`.