2014
Mar
04

初學 IOS 很多基本的功能都不會使用,我習慣把一些好行的程式,包成一個簡單使用的 function。

第一個功能,讀取檔案內容。

readFile
  1. NSError *error;
  2. NSString* content = [NSString stringWithContentsOfFile:filename
  3. encoding:NSUTF8StringEncoding
  4. error:&error];
  5. if(error) {
  6. NSLog(@"error = %@", error);
  7. }

取得當前目錄

NSString *curDir = [[NSFileManager defaultManager] currentDirectoryPath];

取得 app 執行目錄

NSString *myPath = [[NSBundle mainBundle] bundlePath];

儲存資料至 IOS

Example
  1. //存資料
  2. [[NSUserDefaults standardUserDefaults]
  3. setObject:[NSString stringWithFormat:@"test"] forKey:@"score"];
  4.  
  5. //同步
  6. [[NSUserDefaults standardUserDefaults] synchronize];
  7. //取資料
  8. NSString *score = [[NSUserDefaults standardUserDefaults]
  9. stringForKey:@"score"];
  10. NSLog(@"score = %@",score);

移除小鍵盤

Example
  1. [[[UIApplication sharedApplication] keyWindow] endEditing:YES];

Timer

Example
  1. [NSTimer scheduledTimerWithTimeInterval:1.0
  2. target:self
  3. selector:@selector(setTime)
  4. userInfo:nil
  5. repeats:YES];

自動念單字 (機器發音)

Example
  1. NSString *name = @"Michael Jordan";
  2. NSLog(@"%@", name);
  3. AVSpeechUtterance *utterance = [AVSpeechUtterance
  4. speechUtteranceWithString:name];
  5. AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
  6. [synth speakUtterance:utterance];

彈出一個視窗

UIAlertView

回應 (Leave a comment)