IPhoneThis article describes how to develop several examples of learning notes, including iosVibration,NSUserDefaultsUsage and how to read files.
IOS Vibration
VibrationIs a type of sound, using the following method:
- #import <AudioToolbox/AudioToolbox.h>
- #import <UIKit/UIKit.h>
- - (void)vibrate
- {
- AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
- }
Calls the vibrator to vibrate.
How to Use timer NSTimer in iPhone
- - (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 usage
- -(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;
- }
How to read files from Application Bundle on iPhone
First, you must add the file to the Resources directory of the Xcode project. You can set the file to myfile.txt, as shown in the following figure:
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];
-
- NSData *myData = [NSData dataWithContentsOfFile:filePath];
- if (myData) {
- // do something useful
- }
A complete example of reading the help text file into 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"]];
- }
If you want to read the file into a string, you can use UITextView to display it. For example:
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"important" ofType:@"txt"];
- if (filePath) {
- NSString *myText = [NSString stringWithContentsOfFile:filePath];
- if (myText) {
- textView.text= myText;
- }
- }
Summary:IPhoneI have finished the introduction of several instance development operations. I hope this article will help you!