Download presentation
Presentation is loading. Please wait.
1
資料儲存
2
載入多語系字串方法 NSString *NSLocalizedString(NSString *key, NSString *comment) NSString * NSLocalizedStringFromTable(NSString *key, NSString *tableName, NSString *comment) key:欲讀取的字串鍵名稱 tableName:多語系字串檔案名稱,若無指定字串檔 案,則會讀取預設字串檔案: Localizable.strings comment:註解 若鍵值不存在會回傳key字串
3
多國語系 專案中加入Localizable.strings 多國語言檔案
4
多國語系
5
多國語系
6
多國語系
7
預設語系
8
預設語系
9
增加多國語系
10
增加多國語系
11
多國語系
12
編輯各語言資料 設定key和對應語系的值
13
程式載入語言設定 (void)viewDidLoad { [super viewDidLoad];
}
14
應用程式名稱 專案中加入InfoPlist.strings檔案 在各國語言中設定key: CFBundleDisplayName的 值
CFBundleDisplayName="Language Test"; CFBundleDisplayName="多國語言測試";
15
取得語系 NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults]; NSArray *languages=[userDefault objectAtIndex:0]);
16
日期格式化處理 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;
17
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]]; in locale stringFromDate:[NSDate date]],locale); }
18
開啟應用程式
19
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.
20
URL 網頁 電話,直接撥出電話 tel: 簡訊 sms:
21
URL Email 若url中有中文字,必須經過處理 mailto:收件者?subject=主題&cc=副本&bcc=密件副 本
多個收件者用,分隔 若url中有中文字,必須經過處理 使用NSString中的方法 - (NSString *)stringByAddingPercentEscapesUsingEncoding: (NSStringEncoding)encoding UIApplication *app=[UIApplication sharedApplication]; NSString url=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [app openURL:[NSURL URLWithString:url]];
22
資料存取
23
模擬器應用程式路徑 /Users/XXX/Library/
24
模擬器應用程式路徑 /Users/XXX/Library/ 應用程式目錄 可寫入資料 資源檔
25
資源檔 可寫入資料
26
讀取專案Resource圖片 直接指定圖片名稱即可
UIImage *image=[UIImage
27
讀取專案Resource其他資料 找出Resource的路徑 讀取檔案內容
NSString *path=[[NSBundle mainBundle] NSError *error; NSString *data=[[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
28
NSString + (id)stringWithContentsOfFile:(NSString *) path encoding:(NSStringEncoding)enc error:(NSError **)error 利用檔案內容建立字串
29
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 = 0x , NSUTF16LittleEndianStringEncoding = 0x , NSUTF32StringEncoding = 0x8c000100, NSUTF32BigEndianStringEncoding = 0x , NSUTF32LittleEndianStringEncoding = 0x9c000100, NSProprietaryStringEncoding = };
30
找出可寫入檔案路徑 NSArray * NSSearchPathForDirectoriesInDomains (
NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde); 回傳搜尋到該資料夾的路徑 The list is in the order in which you should search the directories.
31
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;
32
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 (~).
33
建立檔案路徑 NSArray *paths=NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES); NSString *path=[paths objectAtIndex:0]; NSString *filePath=[path stringByAppendingPathComponent: @"test.txt"];
34
NSFileManager 檔案操作 + (NSFileManager *)defaultManager
- (BOOL)fileExistsAtPath:(NSString *)path 檢查該路徑的檔案或資料夾是否存在 - (NSArray *)URLsForDirectory: (NSSearchPathDirectory)directory inDomains: (NSSearchPathDomainMask)domainMask 回傳搜尋到該資料夾的路徑
35
NSArray - (id)initWithContentsOfFile:(NSString *) aPath 利用檔案內容建立陣列
36
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
37
讀、寫入資料 寫入 if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){ NSArray *data=[[NSArray alloc] initWithObjects: [data writeToFile:filePath atomically:YES]; } 讀出 if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){ NSArray *data=[[NSArray alloc]initWithContentsOfFile:filePath]; ….. }
38
NSFileManager *fmgr=[NSFileManager defaultManager];
NSArray *paths=[fmgr URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] ; NSURL *path=[paths objectAtIndex:0]; NSURL *fileUrl=[path NSString *filePath=[fileUrl path ]; if(![fmgr fileExistsAtPath:filePath]) { NSArray *data=[[NSArray alloc] nil]; [data writeToFile:filePath atomically:YES]; }
39
NSArray NSDictionary
40
iPhone App和電腦分享檔案 編輯Info.plist
新增key : Application supports iTunes file sharing 可以存取app中Documents資料夾下的檔案
Similar presentations