iPhone開發(fā)應用幾個案例實現(xiàn)分析
iPhone開發(fā)應用幾個案例實現(xiàn)分析是本文要介紹的內容,主要是來學習對iphone開發(fā)中幾個小案例的實現(xiàn)進行來講解并分析,具體內容來看本文詳解。
1、解析NSString形式xml的代碼
提出的問題:
- NSString *xmlString = @"<person><name>Jack</name><age>13< /age></person>";
如何對這個xmlString構造一個NSXML,以及如何解析構造的NSXML.
解決方法:先轉換成NSData,然后用NSXMlParser進行解析。代碼:
- - (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、iphone開發(fā)中讓用戶WebView訪問網(wǎng)頁時嵌入開發(fā)者自己的內容
代碼
- 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></html>"];
- 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];
這個代碼我是在UICatalog中WebViewController中添加的,有人要這功能,小生就乘機學習了下,嫌麻煩,直接用的UICatalog代碼.見諒.
此代碼功能在于:在你訪問的網(wǎng)頁左下角加了hello world navy did it.幾個字.
不希望做流氓功能.一切以用戶為主.
3、iPhone開發(fā)中,動態(tài)調用類和方法
舉一個很簡單的例子:
某公司的有1000名員工, 每個員工的工資都不一樣. 發(fā)工資的時候, 這要是人工去發(fā), 耗費的時間和精力是非常大的. 所以財務會打一個表格給銀行, 委托銀行轉賬.
站在銀行的角度, 如果有1000個公司, 委托銀行轉賬發(fā)工資. 它應該怎么做呢? 它需要通過電子轉賬系統(tǒng), 輸入公司名字, 每個員工的工資數(shù), 就可以實現(xiàn)自動轉賬了.
好, 我們回到 iPhone 開發(fā)上來:
我們現(xiàn)在面臨的情況是, 有10個類, 每個類里頭都有n個方法(前提是方法名有規(guī)律可循,比如 setA0,setA1…) 如果挨個去init類, 然后挨個調用方法,這樣你一天就不用干別的了.
Objective-C里 面,我們可以這樣實現(xiàn):
有數(shù)組: classNames, 存著 類的名字
方法名都是 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])];
- }
- }
兩個重要的宏 我加大字體標出來了,然后你可以再發(fā)揮一下, 比如傳參數(shù)。
4、iPhone開發(fā)項目中加載本地html文件到uiwebview的代碼
如果您想在iPhone項目中加載Documents里面的文件,可以嘗試CocoaChina版主“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];
- }
如果加載App內部的文件,需要改一下代碼
- NSString
- *mainBundleDirectory = [[NSBundle mainBundle] bundlePath];
- NSString
- *path = [mainBundleDirectory stringByAppendingPathComponent:docName];
小結:iPhone開發(fā)應用幾個案例實現(xiàn)分析的內容介紹完了,希望通過本文的學習能對你有所幫助!更多關于iphone開發(fā)的相關的內容,請參考iphone開發(fā)頻道的內容。