Implementation Analysis of iPhone development applications

Source: Internet
Author: User

IPhone DevelopmentThe implementation analysis of several cases is the content to be introduced in this article.Iphone DevelopmentThe implementation of several small cases is explained and analyzed. The specific content is detailed in this article.

1. parse the NSString xml Code

Question raised:

 
 
  1. NSString *xmlString = @"<person><name>Jack</name><age>13< /age></person>"; 

How to construct an NSXML file for the xmlString and parse the constructed NSXML file.

Solution: first convert to NSData and then use NSXMlParser for parsing. Code:

 
 
  1. - (void)handleXMLData {       
  2.     NSString *myString = @"<addresses owner='swilson'><person><lastName>Doe</lastName><firstName>John</firstName></person></addresses>";   
  3.     NSData *myRequestData = [ NSData dataWithBytes: [myString UTF8String]  length:[myString length]];     
  4.     NSXMLParser *myParser = [[NSXMLParser alloc] initWithData:myRequestData];   
  5.     [myParser setDelegate:self];   
  6.     [myParser setShouldProcessNamespaces:YES];   
  7.     [myParser setShouldReportNamespacePrefixes:YES];   
  8.     [myParser setShouldResolveExternalEntities:NO];   
  9.     BOOL success = [myParser parse];   
  10.     [myParser release];  

2. Embedded developer content when WebView is enabled for iphone development to access webpages
 
Code

 
 
  1. NSString *strUrl=[textField text];  
  2.   NSString *urlString=[NSString stringWithFormat:strUrl];  
  3.   NSURL *url=[NSURL URLWithString:urlString];  
  4.   NSURLRequest *urlRequest=[NSURLRequest requestWithURL:url  
  5.                                             cachePolicy:NSURLRequestReturnCacheDataElseLoad  
  6.                                         timeoutInterval:60];  
  7.   NSData *urlData;  
  8.   NSURLResponse *response;  
  9.   NSError *error;  
  10.   urlData=[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];  
  11.   NSString *dataStr =[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];  
  12.   dataStr = [dataStr substringToIndex:[dataStr length] - 16];  
  13.   dataStr = [dataStr stringByAppendingString:@"<p>hello world navy did it </p></body>
  14.   NSLog(@"%@",dataStr);  
  15.   const char *cString = [dataStr UTF8String];  
  16.   NSData *myData= [[NSData alloc]initWithBytes:cString length:strlen(cString)+1];  
  17.   [self.myWebView loadData:myData MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:url]; 

This code is added to WebViewController in UICatalog. If someone asks for this function, the student will take the opportunity to learn it. It is too troublesome and the UICatalog code will be used directly. Sorry.

This code function adds the hello world navy did it in the lower left corner of the webpage you visit.

You do not want to use the rogue function.

3. Dynamic call classes and methods during iPhone Development

Here is a simple example:

A company has 1000 employees, and each employee has a different salary. it takes a lot of time and energy to manually send a pay-as-you-go instance. therefore, the finance department will create a form for the bank and entrust the bank to transfer funds.

From the bank's perspective, if there are 1000 Companies that entrust bank transfers to pay, what should they do? It needs to use the electronic transfer system, enter the company name, the number of wages of each employee, you can achieve automatic transfer.

Okay. Let's go back to iPhone development:

We are now facing the situation that there are 10 classes and each class has n methods (the premise is that the method names are regular, such as setA0, setA1 ...) If you go to the init class one by one and then call the method one by one, you don't have to do anything else in a day.

In Objective-C, we can achieve this:

Array: classNames with the class name

The method names start with setA.

 
 
  1. for (int c=0; c<[classNames count]; c++) {  
  2. NSString *className=[classNames objectAtIndex:c];  
  3. id class=[[NSClassFromString(className) alloc] init];  
  4. for (int i=0; i<[params count]; i++) {  
  5. [class performSelector:NSSelectorFromString([NSString stringWithFormat:@"setA%i",i])];  
  6. }  

Two important macros are marked in a larger font, and you can try again, such as passing parameters.

4. Code for loading local html files to uiwebview in the iPhone Development Project

If you want to load files in Documents in your iPhone project, try the Code provided by the coachina moderator "lvyile ".

 
 
  1. - (void)loadDocument:(NSString*)docName {   
  2.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  3.     NSString *documentsDirectory = [paths objectAtIndex:0];      
  4.     NSString *path = [documentsDirectory stringByAppendingPathComponent:docName];      
  5.     NSURL *url = [NSURL fileURLWithPath:path];  
  6.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  7.    
  8.     self.myWebView.scalesPageToFit = YES;  
  9.    
  10.     [self.myWebView loadRequest:request];  

If you load files in the App, you need to modify the code.

 
 
  1. NSString   
  2. *mainBundleDirectory = [[NSBundle mainBundle] bundlePath];  
  3. NSString   
  4. *path = [mainBundleDirectory  stringByAppendingPathComponent:docName]; 

Summary:IPhone DevelopmentI have completed the analysis by using several cases. 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.