資料儲存
載入多語系字串方法 NSString *NSLocalizedString(NSString *key, NSString *comment) NSString * NSLocalizedStringFromTable(NSString *key, NSString *tableName, NSString *comment) key:欲讀取的字串鍵名稱 tableName:多語系字串檔案名稱,若無指定字串檔 案,則會讀取預設字串檔案: Localizable.strings comment:註解 若鍵值不存在會回傳key字串
多國語系 專案中加入Localizable.strings 多國語言檔案
多國語系
多國語系
多國語系
預設語系
預設語系
增加多國語系
增加多國語系
多國語系
編輯各語言資料 設定key和對應語系的值
程式載入語言設定 (void)viewDidLoad { [super viewDidLoad]; self.label1.text=NSLocalizedString(@"string1",nil); self.label2.text=NSLocalizedString(@"string2",nil); }
應用程式名稱 專案中加入InfoPlist.strings檔案 在各國語言中設定key: CFBundleDisplayName的 值 CFBundleDisplayName="Language Test"; CFBundleDisplayName="多國語言測試";
取得語系 NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults]; NSArray *languages=[userDefault objectForKey:@"AppleLanguages"]; NSLog(@"%@",[languages objectAtIndex:0]);
日期格式化處理 NSDateFormatter - (void)setDateStyle:(NSDateFormatterStyle)style - (void)setTimeStyle:(NSDateFormatterStyle)style - (void)setLocale:(NSLocale *)locale 語言設定 - (NSString *)stringFromDate:(NSDate *)date A string representation of date formatted using the receiver’s current settings. typedef enum { NSDateFormatterNoStyle = kCFDateFormatterNoStyle, NSDateFormatterShortStyle = kCFDateFormatterShortStyle, NSDateFormatterMediumStyle = kCFDateFormatterMediumStyle, NSDateFormatterLongStyle = kCFDateFormatterLongStyle, NSDateFormatterFullStyle = kCFDateFormatterFullStyle } NSDateFormatterStyle;
NSDateFormatter *format=[[NSDateFormatter alloc]init]; [format setDateStyle:NSDateFormatterFullStyle]; [format setTimeStyle:NSDateFormatterFullStyle]; NSArray *locales=[NSLocale availableLocaleIdentifiers]; for(NSString *locale in locales){ [format setLocale:[[NSLocale alloc]initWithLocaleIdentifier:locale]]; NSLog(@"%@ in locale %@",[format stringFromDate:[NSDate date]],locale); }
開啟應用程式
UIApplication - (BOOL)canOpenURL:(NSURL *)url whether an app can open a given URL resource - (BOOL)openURL:(NSURL *)url Opens the resource at the specified URL.
URL 網頁 http:// https:// 電話,直接撥出電話 tel: 簡訊 sms:
URL Email 若url中有中文字,必須經過處理 mailto:收件者?subject=主題&cc=副本&bcc=密件副 本 多個收件者用,分隔 若url中有中文字,必須經過處理 使用NSString中的方法 - (NSString *)stringByAddingPercentEscapesUsingEncoding: (NSStringEncoding)encoding UIApplication *app=[UIApplication sharedApplication]; NSString *url=@"mailto:abc@cc.com,test@cc.com?subject=中文&cc=ww@dd.c"; url=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [app openURL:[NSURL URLWithString:url]];
資料存取
模擬器應用程式路徑 /Users/XXX/Library/
模擬器應用程式路徑 /Users/XXX/Library/ 應用程式目錄 可寫入資料 資源檔
資源檔 可寫入資料
讀取專案Resource圖片 直接指定圖片名稱即可 UIImage *image=[UIImage imageNamed:@"icon.jpeg"];
讀取專案Resource其他資料 找出Resource的路徑 讀取檔案內容 NSString *path=[[NSBundle mainBundle] pathForResource:@"檔案名稱" ofType:@"副檔名"]; NSError *error; NSString *data=[[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
NSString + (id)stringWithContentsOfFile:(NSString *) path encoding:(NSStringEncoding)enc error:(NSError **)error 利用檔案內容建立字串
NSStringEncoding enum { NSASCIIStringEncoding = 1, NSNEXTSTEPStringEncoding = 2, NSJapaneseEUCStringEncoding = 3, NSUTF8StringEncoding = 4, NSISOLatin1StringEncoding = 5, NSSymbolStringEncoding = 6, NSNonLossyASCIIStringEncoding = 7, NSShiftJISStringEncoding = 8, NSISOLatin2StringEncoding = 9, NSUnicodeStringEncoding = 10, NSWindowsCP1251StringEncoding = 11, NSWindowsCP1252StringEncoding = 12, NSWindowsCP1253StringEncoding = 13, NSWindowsCP1254StringEncoding = 14, NSWindowsCP1250StringEncoding = 15, NSISO2022JPStringEncoding = 21, NSMacOSRomanStringEncoding = 30, NSUTF16StringEncoding = NSUnicodeStringEncoding, NSUTF16BigEndianStringEncoding = 0x90000100, NSUTF16LittleEndianStringEncoding = 0x94000100, NSUTF32StringEncoding = 0x8c000100, NSUTF32BigEndianStringEncoding = 0x98000100, NSUTF32LittleEndianStringEncoding = 0x9c000100, NSProprietaryStringEncoding = 65536 };
找出可寫入檔案路徑 NSArray * NSSearchPathForDirectoriesInDomains ( NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde); 回傳搜尋到該資料夾的路徑 The list is in the order in which you should search the directories.
NSSearchPathDirectory enum { NSApplicationDirectory = 1, NSDemoApplicationDirectory, NSDeveloperApplicationDirectory, NSAdminApplicationDirectory, NSLibraryDirectory, NSDeveloperDirectory, NSUserDirectory, NSDocumentationDirectory, NSDocumentDirectory, NSCoreServiceDirectory, NSAutosavedInformationDirectory = 11,NSDesktopDirectory = 12, NSCachesDirectory = 13, NSApplicationSupportDirectory = 14, NSDownloadsDirectory = 15, NSInputMethodsDirectory = 16, NSMoviesDirectory = 17, NSMusicDirectory = 18, NSPicturesDirectory = 19, NSPrinterDescriptionDirectory = 20, NSSharedPublicDirectory = 21, NSPreferencePanesDirectory = 22, NSItemReplacementDirectory = 99, NSAllApplicationsDirectory = 100, NSAllLibrariesDirectory = 101 }; typedef NSUInteger NSSearchPathDirectory;
NSSearchPathDomainMask enum { NSUserDomainMask = 1, NSLocalDomainMask = 2, NSNetworkDomainMask = 4, NSSystemDomainMask = 8, NSAllDomainsMask = 0x0ffff, }; typedef NSUInteger NSSearchPathDomainMask; BOOL expandTilde :是否要有~ NSDocumentDirectory:Document directory NSUserDomainMask: The user’s home directory—the place to install user’s personal items (~).
建立檔案路徑 NSArray *paths=NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); NSString *path=[paths objectAtIndex:0]; NSString *filePath=[path stringByAppendingPathComponent: @"test.txt"];
NSFileManager 檔案操作 + (NSFileManager *)defaultManager - (BOOL)fileExistsAtPath:(NSString *)path 檢查該路徑的檔案或資料夾是否存在 - (NSArray *)URLsForDirectory: (NSSearchPathDirectory)directory inDomains: (NSSearchPathDomainMask)domainMask 回傳搜尋到該資料夾的路徑
NSArray - (id)initWithContentsOfFile:(NSString *) aPath 利用檔案內容建立陣列
NSArray - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag 將陣列內容寫入檔案 flag YES: auxiliary file, and then the auxiliary file is renamed to path. NO, the array is written directly to path
讀、寫入資料 寫入 if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){ NSArray *data=[[NSArray alloc] initWithObjects: @"data1",@"data2",@"data3",@"data4",@"data5",nil]; [data writeToFile:filePath atomically:YES]; } 讀出 if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){ NSArray *data=[[NSArray alloc]initWithContentsOfFile:filePath]; ….. }
NSFileManager *fmgr=[NSFileManager defaultManager]; NSArray *paths=[fmgr URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] ; NSURL *path=[paths objectAtIndex:0]; NSURL *fileUrl=[path URLByAppendingPathComponent:@"test.txt"]; NSString *filePath=[fileUrl path ]; NSLog(@"%@",filePath); if(![fmgr fileExistsAtPath:filePath]) { NSArray *data=[[NSArray alloc] initWithObjects:@"11",@"22",@"33",@"44",@"55",@"66", nil]; [data writeToFile:filePath atomically:YES]; }
NSArray NSDictionary
iPhone App和電腦分享檔案 編輯Info.plist 新增key : Application supports iTunes file sharing 可以存取app中Documents資料夾下的檔案