메뉴 건너뛰기

app

[xcode] RSS 리더 구현하기(2)

박영식2011.09.13 19:11조회 수 2535댓글 0

  • 2
    • 글자 크기
(1)에서는 여러 경로에서 리스트를 가져왔다. 이제 여러 개를 가져와서 처리하는 것까지 다룬다.

gdata-objective-c client library 를 다운 받으라고 되어있다. 예제 코드에서 복사하여 ADD TO '프로젝트이름' 으로 추가하는 방법으로 해도된다. 컴파일 옵션을 추가한다.

Click ProjectEdit Project Settings, go to the Build tab, and make sure “All Configurations” are checked.

  1. Find the Search PathsHeader Search Paths setting and add /usr/include/libxml2 to the list.
  2. Finally, find the LinkingOther Linker Flags section and add -lxml2 to the list.
스크린샷 2011-09-13 오전 10.02.46.png

xcode4를 이용했기 때문에 이런 UI가 보이는데, 버전이 다르면 다를 수도 있다. 찾는데 20~30분은 걸린 것 같다. 후우.....

AddNew File, choose iOSCocoa Touch ClassObjective-C class 로 새 파일을 추가하고, 다음과 같이 코딩한다.

[GDataXMLElement-Extras.m]

#import <Foundation/Foundation.h>
#import "GDataXMLNode.h"
 
@interface GDataXMLElement (Extras)
 
- (GDataXMLElement *)elementForChild:(NSString *)childName;
- (NSString *)valueForChild:(NSString *)childName;
 
@end

[GDataXMLElement-Extras.m]

#import "GDataXMLElement-Extras.h"
 
@implementation GDataXMLElement(Extras)
 
- (GDataXMLElement *)elementForChild:(NSString *)childName {
    NSArray *children = [self elementsForName:childName];            
    if (children.count > 0) {
        GDataXMLElement *childElement = (GDataXMLElement *) [children objectAtIndex:0];
        return childElement;
    } else return nil;
}
 
- (NSString *)valueForChild:(NSString *)childName {    
    return [[self elementForChild:childName] stringValue];    
}
 
@end

[RootViewController.h]

// Add to top of file
#import "GDataXMLNode.h"
#import "GDataXMLElement-Extras.h"
 
// Modify requestFinished as follows
- (void)requestFinished:(ASIHTTPRequest *)request {
 
    [_queue addOperationWithBlock:^{
 
        NSError *error;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData] 
                                                               options:0 error:&error];
        if (doc == nil) { 
            NSLog(@"Failed to parse %@", request.url);
        } else {
 
            NSMutableArray *entries = [NSMutableArray array];
            [self parseFeed:doc.rootElement entries:entries];                
 
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
 
                for (RSSEntry *entry in entries) {
 
                    int insertIdx = 0;                    
                    [_allEntries insertObject:entry atIndex:insertIdx];
                    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
                                          withRowAnimation:UITableViewRowAnimationRight];
 
                }                            
 
            }];
 
        }        
    }];
 
}

[RootViewController.m]
// 새로운 매서드 추가

- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {
 
    NSArray *channels = [rootElement elementsForName:@"channel"];
    for (GDataXMLElement *channel in channels) {            
 
        NSString *blogTitle = [channel valueForChild:@"title"];                    
 
        NSArray *items = [channel elementsForName:@"item"];
        for (GDataXMLElement *item in items) {
 
            NSString *articleTitle = [item valueForChild:@"title"];
            NSString *articleUrl = [item valueForChild:@"link"];            
            NSString *articleDateString = [item valueForChild:@"pubDate"];        
            NSDate *articleDate = nil;
 
            RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle 
                                                      articleTitle:articleTitle 
                                                        articleUrl:articleUrl 
                                                       articleDate:articleDate] autorelease];
            [entries addObject:entry];
 
        }      
    }
 
}
 
- (void)parseAtom:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {
 
    NSString *blogTitle = [rootElement valueForChild:@"title"];                    
 
    NSArray *items = [rootElement elementsForName:@"entry"];
    for (GDataXMLElement *item in items) {
 
        NSString *articleTitle = [item valueForChild:@"title"];
        NSString *articleUrl = nil;
        NSArray *links = [item elementsForName:@"link"];        
        for(GDataXMLElement *link in links) {
            NSString *rel = [[link attributeForName:@"rel"] stringValue];
            NSString *type = [[link attributeForName:@"type"] stringValue]; 
            if ([rel compare:@"alternate"] == NSOrderedSame && 
                [type compare:@"text/html"] == NSOrderedSame) {
                articleUrl = [[link attributeForName:@"href"] stringValue];
            }
        }
 
        NSString *articleDateString = [item valueForChild:@"updated"];        
        NSDate *articleDate = nil;
 
        RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle 
                                                  articleTitle:articleTitle 
                                                    articleUrl:articleUrl 
                                                   articleDate:articleDate] autorelease];
        [entries addObject:entry];
 
    }      
 
}


- (void)parseFeed:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {    
    if ([rootElement.name compare:@"rss"] == NSOrderedSame) {
        [self parseRss:rootElement entries:entries];
    } else if ([rootElement.name compare:@"feed"] == NSOrderedSame) {                       
        [self parseAtom:rootElement entries:entries];
    } else {
        NSLog(@"Unsupported root element: %@", rootElement.name);
    }    
}

 parseFeed 매서드가 parseAtom, parseRSS보다 뒤에 나와야 warning이 안 생긴다.

이제 리스팅까지는 끝났다.

GDataXML.jpg

박영식 (비회원)
  • 2
    • 글자 크기
[xcode] RSS 리더 구현하기(3) (by 박영식) [xcode] RSS 리더 구현하기(1) (by 박영식)

댓글 달기

박영식
2011.09.22 조회 2415
박영식
2011.09.21 조회 2330
이전 1 2 3 4 5 6 7 8 9 10 11... 14다음
첨부 (2)
스크린샷 2011-09-13 오전 10.02.46.png
180.6KB / Download 34
GDataXML.jpg
36.8KB / Download 36
위로