你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> [iPhone中級]iPhone團購信息客戶端的開發 (二)

[iPhone中級]iPhone團購信息客戶端的開發 (二)

編輯:IOS開發綜合

接上一篇,這篇我們對我們的客戶端加入KissXML,MBProgressHUD,AQridView這幾個庫,首先我們先加入KissXML,這是XML解析庫,支持Xpath,可以方便添加更改任何節點。先從官方網站上下載KissXML這個庫


接下來,將KissXML庫中的KissXML文件夾拷入我們的工程中,如圖

 

\

然後跟上次一樣,我們通過File/Add File to "Tuan1"這個選項加入到我們的工程中,我們以同樣的方法分別加入MBProgressHUD和AGridView這兩個庫

在Framework中加入QuartzCore.framework,因為AGridView需要用到。
 

\

接著我們在代碼中加入

#import"MBProgressHUD.h"

#import"DDXML.h"

這二行,運行一下,如果沒有報錯的話,那這幾個庫我們就加入成功了。

接著我們更改一下storybord,將FirstViewController這個視圖加入NavigationController,選中FirstViewController在主菜單Editor/Embed In/Navigation Controller,系統自動為我們加入NavigationController。
 

\

下面,我們將FirstViewController中清空,所有內容都刪除

\

好了,到這一步,我們的視圖基本都完成了,接下來就是編碼了,首先我們來解析美團網團購信息

FirstViewController.h

[plain]
#import <UIKit/UIKit.h> 
#import "ASIHTTPRequest.h" 
   
@interface KKFirstViewController : UIViewController<ASIHTTPRequestDelegate> 
 
 
@end 
#import <UIKit/UIKit.h>
#import "ASIHTTPRequest.h"


@interface KKFirstViewController : UIViewController<ASIHTTPRequestDelegate>

@end

FirstViewController.m

[plain]
#import "KKFirstViewController.h" 
#import "MBProgressHUD.h" 
#import "DDXML.h" 
#import "DDXMLElementAdditions.h" 
 
@interface KKFirstViewController (){ 
     
    MBProgressHUD *hud; 
    NSMutableArray *arrays; 
     

 
@end 
 
@implementation KKFirstViewController 
 
- (void)viewDidLoad 

    [super viewDidLoad]; 
     
    self.navigationItem.title = @"美團"; 
    //背景 
    UIImage *bgImage = [UIImage imageNamed:@"bg-app.png"]; 
    self.view.backgroundColor = [UIColor colorWithPatternImage:bgImage]; 
    //HUD提示框 
    hud = [[MBProgressHUD alloc] init]; 
    hud.labelText = @"載入數據..."; 
    //網址 
    NSString *urlString = @"http://www.meituan.com/api/v2/beijing/deals"; 
     
    [hud showWhileExecuting:@selector(startHttpRequest:) onTarget:self withObject:urlString animated:YES]; 
    //因為hud需要顯示在最前面,所以我們在NavigationController中加入 
    [self.navigationController.view addSubview:hud]; 
     
    arrays = [NSMutableArray array]; 

 
//請求URL 
-(void)startHttpRequest:(NSString *)url{ 
     
    ASIHTTPRequest *httpRequest = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:url]]; 
     
    httpRequest.delegate = self; 
     
    [httpRequest startSynchronous]; 
     

 
- (void)viewDidUnload 

    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 

 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

 
 
//請求結束 
-(void)requestFinished:(ASIHTTPRequest *)request{ 
    //移除提示框 
    [hud removeFromSuperview]; 
    //返回解析後的數據,每一個Array包括一個字典 
    arrays = [self xmlParser:[request responseData]]; 
     
    NSLog(@"%@", arrays); 
     

 
//解析結點 
static NSString *kXPath_Item = @"//data"; 
static NSString *kName_Deal = @"deal"; 
static NSString *kName_Title = @"deal_title"; 
static NSString *kName_Url = @"deal_url"; 
static NSString *kName_Img = @"deal_img"; 
static NSString *kName_Desc = @"deal_desc"; 
static NSString *kName_Tips = @"deal_tips"; 
 
//XML解析 
-(NSMutableArray *)xmlParser:(NSData *)data{ 
     
    NSMutableArray *array = [NSMutableArray array]; 
    //文檔開始 
    DDXMLDocument *xmlDoc = [[DDXMLDocument alloc] initWithData:data options:0 error:nil]; 
    //返回<data>中的所有元素 
    NSArray *items = [xmlDoc nodesForXPath:kXPath_Item error:nil]; 
    //遍歷每個元素 
    for (DDXMLElement *item in items) { 
         
        NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 
        //返回<deal>中的所有元素 
        NSArray *deals = [item elementsForName:kName_Deal]; 
         
        for (DDXMLElement *deal in deals) { 
            //<deal_title> 
            DDXMLElement *title = [deal elementForName:kName_Title]; 
            if (title) { 
                [dict setObject:[title stringValue] forKey:kName_Title]; 
            } 
            //<deal_url> 
            DDXMLElement *url = [deal elementForName:kName_Url]; 
            if (url) { 
                [dict setObject:[url stringValue] forKey:kName_Url]; 
            } 
            //<deal_img> 
            DDXMLElement *img = [deal elementForName:kName_Img]; 
            if (img) { 
                [dict setObject:[img stringValue] forKey:kName_Img]; 
            } 
            //<deal_desc> 
            DDXMLElement *desc = [deal elementForName:kName_Desc]; 
            if (desc) { 
                [dict setObject:[desc stringValue] forKey:kName_Desc]; 
            } 
            //<deal_tips> 
            DDXMLElement *tips = [deal elementForName:kName_Tips]; 
            if (tips) { 
                [dict setObject:[tips stringValue] forKey:kName_Tips]; 
            } 
             
        } 
        [array addObject:dict]; 
    } 
     
    return array; 
     

 
//請求中止(出錯) 
-(void)requestFailed:(ASIHTTPRequest *)request{ 
     
    [hud removeFromSuperview]; 
     
    NSError *error = [request error]; 
     
    NSLog(@"RequestError : %@", [error description]); 
     

 
@end 
作者:kangkangz4
 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved