메뉴 건너뛰기

app

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

박영식2011.09.13 18:55조회 수 2563댓글 0

  • 2
    • 글자 크기
How To Make A Simple RSS Reader iPhone App Tutorial (http://www.raywenderlich.com/2636/how-to-make-a-simple-rss-reader-iphone-app-tutorial) 을 한글화 했다고 보면 된다.

1. XCODE 에서 프로젝트를 생성하고, 메인 윈도우에서 네비게이션 컨트롤러를 추가한다. 이렇게 하면 자동으로 Root View Controller 까지 추가된다.(실제 글에서 이게 빠져 있어 초반에 당황했다.)
FileNew File, choose iOSCocoa Touch ClassObjective-C class 로 subclass는 UIViewController subclass 로 subclass of UITableViewController을 with XIB user interface가 체크된채 생성한다. MainWindow.xib에서 RootViewController(identity inspector)의 class를 RootViewController로 지정한다. Attribute inspector 에서 NIB 를 RootViewController로 지정하면 연결된다.

2. 원문에 나온대로, RSSEntry.h 와 RSSEntry.m을 추가한다. FileNew File, choose iOSCocoa Touch ClassObjective-C class 로 subclass는 NSObject로 생성하면 된다.

[RSSEntry.h]

#import <Foundation/Foundation.h>
 
@interface RSSEntry : NSObject {
    NSString *_blogTitle;
    NSString *_articleTitle;
    NSString *_articleUrl;
    NSDate *_articleDate;
}
 
@property (copy) NSString *blogTitle;
@property (copy) NSString *articleTitle;
@property (copy) NSString *articleUrl;
@property (copy) NSDate *articleDate;
 
- (id)initWithBlogTitle:(NSString*)blogTitle articleTitle:(NSString*)articleTitle articleUrl:(NSString*)articleUrl articleDate:(NSDate*)articleDate;
 
@end

[RSSEntry.m]

#import "RSSEntry.h"
 
@implementation RSSEntry
@synthesize blogTitle = _blogTitle;
@synthesize articleTitle = _articleTitle;
@synthesize articleUrl = _articleUrl;
@synthesize articleDate = _articleDate;
 
- (id)initWithBlogTitle:(NSString*)blogTitle articleTitle:(NSString*)articleTitle articleUrl:(NSString*)articleUrl articleDate:(NSDate*)articleDate 
{
    if ((self = [super init])) {
        _blogTitle = [blogTitle copy];
        _articleTitle = [articleTitle copy];
        _articleUrl = [articleUrl copy];
        _articleDate = [articleDate copy];
    }
    return self;
}
 
- (void)dealloc {
    [_blogTitle release];
    _blogTitle = nil;
    [_articleTitle release];
    _articleTitle = nil;
    [_articleUrl release];
    _articleUrl = nil;
    [_articleDate release];
    _articleDate = nil;
    [super dealloc];
}
 
@end

3. 이 과정 다음에 원문에서는 RootViewController에 뭔가를 추가하라고 하는데, 1번을 설명 하지 않았기 때문에 당황했다. 그러나 여기서는 1번을 추가했기 때문에 그대로 따라하면 된다.

[RootViewController.h]

// Inside @interface
NSMutableArray *_allEntries;
 
// After @interface
@property (retain) NSMutableArray *allEntries;


[RootViewController.m]

// At top of file
#import "RSSEntry.h"
 
// After @implementation
@synthesize allEntries = _allEntries;
 
// Add new method
- (void)addRows {    
    RSSEntry *entry1 = [[[RSSEntry alloc] initWithBlogTitle:@"1" 
                                               articleTitle:@"1" 
                                                 articleUrl:@"1" 
                                                articleDate:[NSDate date]] autorelease];
    RSSEntry *entry2 = [[[RSSEntry alloc] initWithBlogTitle:@"2" 
                                               articleTitle:@"2" 
                                                 articleUrl:@"2" 
                                                articleDate:[NSDate date]] autorelease];
    RSSEntry *entry3 = [[[RSSEntry alloc] initWithBlogTitle:@"3" 
                                               articleTitle:@"3" 
                                                 articleUrl:@"3" 
                                                articleDate:[NSDate date]] autorelease];    
 
    [_allEntries insertObject:entry1 atIndex:0];
    [_allEntries insertObject:entry2 atIndex:0];
    [_allEntries insertObject:entry3 atIndex:0];        
}
 
// Uncomment viewDidLoad and make it look like the following
- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.title = @"Feeds";
    self.allEntries = [NSMutableArray array];
    [self addRows];    
}
 
// Replace return 0 in tableView:numberOfRowsInSection with this
return [_allEntries count];
 
// Modify tableView:cellForRowAtIndexPath below like the following
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    static NSString *CellIdentifier = @"Cell";
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
 
    RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
 
    NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate];
 
    cell.textLabel.text = entry.articleTitle;        
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", articleDateString, entry.blogTitle];
 
    return cell;
}
 
// In dealloc
[_allEntries release];
_allEntries = nil;

GettingStarted.jpg


Command + R로 실행하면 위의 화면을 볼 수 있다.
4. 이제 RSS 포맷을 받아와 보여주기 위한 작업이 필요하다.
ASIHTTPRequest source 를 다운 받으라고 되어 있는데, 예제 소스에 포함되어 있으므로 예제소스에서 ASIHTTPRequest 폴더를 프로젝트 폴더에 복사하고, add시켜서 추가하는 방법을 사용했다.
Add files to '프로젝트명' 으로 디렉터리를 선택하면 트리구조에 나타난다. 그러면 추가된 것이다.

[RootViewController.h]
// Inside @interface
NSOperationQueue *_queue;
NSArray *_feeds;
 
// After @interface
@property (retain) NSOperationQueue *queue;
@property (retain) NSArray *feeds;


[RootViewController.m]
// Add to top of file
#import "ASIHTTPRequest.h"
 
// Add under @implementation
@synthesize feeds = _feeds;
@synthesize queue = _queue;
 
// Add new method
- (void)refresh {
    for (NSString *feed in _feeds) {
        NSURL *url = [NSURL URLWithString:feed];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request setDelegate:self];
        [_queue addOperation:request];
    }    
}

- (void)requestFinished:(ASIHTTPRequest *)request {
 
    RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:request.url.absoluteString
                                               articleTitle:request.url.absoluteString
                                                 articleUrl:request.url.absoluteString
                                                articleDate:[NSDate date]] autorelease];    
    int insertIdx = 0;                    
    [_allEntries insertObject:entry atIndex:insertIdx];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
                          withRowAnimation:UITableViewRowAnimationRight];
 
}
 
- (void)requestFailed:(ASIHTTPRequest *)request {
    NSError *error = [request error];
    NSLog(@"Error: %@", error);
}
// Modify viewDidLoad as follows - (void)viewDidLoad { [super viewDidLoad]; self.title = @"Feeds"; self.allEntries = [NSMutableArray array]; self.queue = [[[NSOperationQueue alloc] init] autorelease]; self.feeds = [NSArray arrayWithObjects:@"http://feeds.feedburner.com/RayWenderlich", @"http://feeds.feedburner.com/vmwstudios", @"http://idtypealittlefaster.blogspot.com/feeds/posts/default", @"http://www.71squared.com/feed/", @"http://cocoawithlove.com/feeds/posts/default", @"http://feeds2.feedburner.com/brandontreb", @"http://feeds.feedburner.com/CoryWilesBlog", @"http://geekanddad.wordpress.com/feed/", @"http://iphonedevelopment.blogspot.com/feeds/posts/default", @"http://karnakgames.com/wp/feed/", @"http://kwigbo.com/rss", @"http://shawnsbits.com/feed/", @"http://pocketcyclone.com/feed/", @"http://www.alexcurylo.com/blog/feed/", @"http://feeds.feedburner.com/maniacdev", @"http://feeds.feedburner.com/macindie", nil]; [self refresh]; }   // In dealloc [_queue release]; _queue = nil; [_feeds release]; _feeds = nil;

위 과정까지 끝내면, 아래 화면을 볼 수 있다.
ASIHTTPRequest.jpg

박영식 (비회원)
  • 2
    • 글자 크기
[xcode] RSS 리더 구현하기(2) (by 박영식) [wget] 여러 정책을 무시하고 걍 긇어오기 (by 박영식)

댓글 달기

박영식
2011.09.22 조회 2415
박영식
2011.09.21 조회 2330
이전 1 2 3 4 5 6 7 8 9 10 11... 14다음
첨부 (2)
GettingStarted.jpg
9.3KB / Download 32
ASIHTTPRequest.jpg
42.2KB / Download 30
위로