iPhone學習筆記幾個實例開發(fā)操作
iPhone學習筆記幾個實例開發(fā)操作是本文要介紹的內容,主要有ios震動、NSUserDefaults用法、如何讀取文件等內容,來看本文內容。
IOS震動
震動是聲音的一種,用如下方式:
- #import <AudioToolbox/AudioToolbox.h>
- #import <UIKit/UIKit.h>
- - (void)vibrate
- {
- AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
- }
調用到震動器震動。
iPhone中定時器NSTimer使用方法
- - (void) applicationDidFinishLaunching: {
- NSTimer *_timer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES] retain];
- }
- // call function when time is up (in this exemple :1 second. to change it, change scheduledTimerWithTimeInterval: 1 to value in second)
- - (void)onTimer
- {
- // schedule code..
- }
NSUserDefaults用法
- -(void)saveToUserDefaults:(NSString*)myString
- {
- NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
- if (standardUserDefaults)
- {
- [standardUserDefaults setObject:myString forKey:@"Prefs"];
- [standardUserDefaults synchronize];
- }
- }
- -(NSString*)retrieveFromUserDefaults
- {
- NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
- NSString *val = nil;
- if (standardUserDefaults)
- val = [standardUserDefaults objectForKey:@"Prefs"];
- return val;
- }
iPhone中如何從Application Bundle中讀取文件
首先必須將文件加入Xcode工程的Resources目錄。然后可以如下訪問文件,假設文件為MyFile.txt:
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];
- NSData *myData = [NSData dataWithContentsOfFile:filePath];
- if (myData) {
- // do something useful
- }
一段將help文本文件讀入UIWebView的完整示例:
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HelpDoc" ofType:@"htm"];
- NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
- if (htmlData) {
- [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://iphoneincubator.com"]];
- }
如果想將文件讀入字符串,則可以用UITextView顯示,例如:
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"important" ofType:@"txt"];
- if (filePath) {
- NSString *myText = [NSString stringWithContentsOfFile:filePath];
- if (myText) {
- textView.text= myText;
- }
- }
小結:iPhone學習筆記幾個實例開發(fā)操作的內容介紹完了,希望通過本文的學習對你有所幫助!