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:
- 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:
- - (void)handleXMLData {
- NSString *myString = @"<addresses owner='swilson'><person><lastName>Doe</lastName><firstName>John</firstName></person></addresses>";
- NSData *myRequestData = [ NSData dataWithBytes: [myString UTF8String] length:[myString length]];
- NSXMLParser *myParser = [[NSXMLParser alloc] initWithData:myRequestData];
- [myParser setDelegate:self];
- [myParser setShouldProcessNamespaces:YES];
- [myParser setShouldReportNamespacePrefixes:YES];
- [myParser setShouldResolveExternalEntities:NO];
- BOOL success = [myParser parse];
- [myParser release];
2. Embedded developer content when WebView is enabled for iphone development to access webpages
Code
- NSString *strUrl=[textField text];
- NSString *urlString=[NSString stringWithFormat:strUrl];
- NSURL *url=[NSURL URLWithString:urlString];
- NSURLRequest *urlRequest=[NSURLRequest requestWithURL:url
- cachePolicy:NSURLRequestReturnCacheDataElseLoad
- timeoutInterval:60];
- NSData *urlData;
- NSURLResponse *response;
- NSError *error;
- urlData=[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
- NSString *dataStr =[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
- dataStr = [dataStr substringToIndex:[dataStr length] - 16];
- dataStr = [dataStr stringByAppendingString:@"<p>hello world navy did it </p></body>
- NSLog(@"%@",dataStr);
- const char *cString = [dataStr UTF8String];
- NSData *myData= [[NSData alloc]initWithBytes:cString length:strlen(cString)+1];
- [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.
- for (int c=0; c<[classNames count]; c++) {
- NSString *className=[classNames objectAtIndex:c];
- id class=[[NSClassFromString(className) alloc] init];
- for (int i=0; i<[params count]; i++) {
- [class performSelector:NSSelectorFromString([NSString stringWithFormat:@"setA%i",i])];
- }
- }
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 ".
- - (void)loadDocument:(NSString*)docName {
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSString *path = [documentsDirectory stringByAppendingPathComponent:docName];
- NSURL *url = [NSURL fileURLWithPath:path];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
-
- self.myWebView.scalesPageToFit = YES;
-
- [self.myWebView loadRequest:request];
- }
If you load files in the App, you need to modify the code.
- NSString
- *mainBundleDirectory = [[NSBundle mainBundle] bundlePath];
- NSString
- *path = [mainBundleDirectory stringByAppendingPathComponent:docName];
Summary:IPhone DevelopmentI have completed the analysis by using several cases. I hope this article will help you!