iPhone学习笔记几个实例开发操作是本文要介绍的内容,主要有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学习笔记几个实例开发操作的内容介绍完了,希望通过本文的学习对你有所帮助!