Several instance development operations for iPhone learning notes

Source: Internet
Author: User

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:

 
 
  1. #import <AudioToolbox/AudioToolbox.h>   
  2. #import <UIKit/UIKit.h>   
  3. - (void)vibrate   
  4. {   
  5.     AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);   
  6. }  

Calls the vibrator to vibrate.

How to Use timer NSTimer in iPhone

 
 
  1. - (void) applicationDidFinishLaunching: {  
  2. NSTimer *_timer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES] retain];  
  3. }  
  4. // call function when time is up (in this exemple :1 second. to change it, change scheduledTimerWithTimeInterval: 1 to value in second)  
  5. - (void)onTimer  
  6. {  
  7. // schedule code..  

NSUserDefaults usage

 
 
  1. -(void)saveToUserDefaults:(NSString*)myString   
  2. {   
  3. NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];   
  4. if (standardUserDefaults)        
  5. {   
  6. [standardUserDefaults setObject:myString forKey:@"Prefs"];   
  7.        [standardUserDefaults synchronize];  
  8.      }   
  9. }     
  10. -(NSString*)retrieveFromUserDefaults   
  11. {   
  12. NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];    
  13. NSString *val = nil;   
  14. if (standardUserDefaults)         
  15. val = [standardUserDefaults objectForKey:@"Prefs"];   
  16. 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:

 
 
  1. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];       
  2.  
  3. NSData *myData = [NSData dataWithContentsOfFile:filePath];       
  4.  if (myData) {       
  5.     // do something useful       
  6.  }  

A complete example of reading the help text file into UIWebView:

 
 
  1. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HelpDoc" ofType:@"htm"];       
  2.  NSData *htmlData = [NSData dataWithContentsOfFile:filePath];       
  3.  if (htmlData) {       
  4.      [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://iphoneincubator.com"]];       
  5.  }  

If you want to read the file into a string, you can use UITextView to display it. For example:

 
 
  1. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"important" ofType:@"txt"];    
  2.  if (filePath) {    
  3.     NSString *myText = [NSString stringWithContentsOfFile:filePath];    
  4.      if (myText) {    
  5.          textView.text= myText;    
  6.     }    
  7.  }  

Summary:IPhoneI have finished the introduction of several instance development operations. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.