LoginSignup
6
6

More than 5 years have passed since last update.

簡易RSSリーダーを作ってみた 〜 Objective-C編 〜

Last updated at Posted at 2015-06-05

Cで書いた簡易RSSリーダーと同じ機能のものをMacOS用にObjective-Cで書いてみました。

サンプルコード

アップルのRSSフィードを取得して、サマリを表示する Command Line Tool です。

#import <Foundation/Foundation.h>

#define PATH @"http://www.apple.com/jp/main/rss/hotnews/hotnews.rss"
int read_rss(NSXMLDocument*);

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // Download RSS feed.
        NSURL           *url = [NSURL URLWithString:PATH];
        NSURLRequest    *urlRequest = [NSURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                   timeoutInterval:30];

        NSData          *urlData;
        NSURLResponse   *response;
        NSError         *error;
        urlData = [NSURLConnection sendSynchronousRequest:urlRequest
                                        returningResponse:&response
                                                    error:&error];
        if (!urlData) {
            printf("failed to url request.");
        }

        // Create XML Document fron urlData
        NSXMLDocument   *doc;
        doc = [[NSXMLDocument alloc] initWithData:urlData
                                          options:0
                                            error:&error];
        if (!doc) {
            printf("failed to create xml doc.");
            return 1;
        }

        // read RSS Feed.
        read_rss(doc);
        return 0;
    }
}

int read_rss(NSXMLDocument  *doc) {
    NSError         *error;
    // NSString* xmlString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
    // NSLog(@"doc\n%@\n", xmlString);

    // Get and print Title of RSS Feed.
    NSArray *channelNodes = [doc nodesForXPath:@"//channel" error:&error];
    if (!channelNodes) {
        return -1;
    }
    for (NSXMLElement* element in channelNodes) {
        if ([element kind] != NSXMLElementKind) {
            continue;
        }
        NSArray*    children = [element children];
        NSString*   title;
        for (NSXMLNode* node in children) {
            if ([[node name] isEqualToString:@"title"]) {
                title = [node stringValue];
                printf("\nTitle: %s\n\n", [title cStringUsingEncoding:NSUTF8StringEncoding]);
            }
        }
    }

    // Get Node list of RSS items.
    NSArray *itemNodes = [doc nodesForXPath:@"//item" error:&error];
    if (!itemNodes) {
        return -1;
    }
    for (NSXMLElement* element in itemNodes) {
        if ([element kind] != NSXMLElementKind) {
            continue;
        }
        NSArray*    children = [element children];
        NSString*   title;
        NSString*   link;
        for (NSXMLNode* node in children) {
            if ([[node name] isEqualToString:@"title"]) {
                title = [node stringValue];
            } else if ([[node name] isEqualToString:@"link"]) {
                link = [node stringValue];
            }
        }
        printf(" title: %s\n", [title cStringUsingEncoding:NSUTF8StringEncoding]);
        printf(" link:  %s\n\n", [link cStringUsingEncoding:NSUTF8StringEncoding]);
    }
    return 0;
}

実行結果

ターミナルから実行すると「アップル - ホットニュース」のサマリが表示されました。

Title: アップル - ホットニュース

 title: Apple、感圧タッチトラックパッドを搭載した15インチのMacBook Pro、238,800円の新しいiMac Retina 5Kディスプレイモデルを発売
 link:  http://www.apple.com/jp/pr/library/2015/05/19Apple-Introduces-15-inch-MacBook-Pro-with-Force-Touch-Trackpad-New-1-999-iMac-with-Retina-5K-Display.html

 title: 日本郵政グループ、IBM、Apple、日本の高齢者がサービスを通じて家族・地域コミュニティーとつながるために、iPadと専用アプリケーションを提供
 link:  http://www.apple.com/jp/pr/library/2015/04/30Japan-Post-Group-IBM-and-Apple-Deliver-iPads-and-Custom-Apps-to-Connect-Elderly-in-Japan-to-Services-Family-and-Community.html

 title: Apple、第2四半期の業績を発表
  link:  http://www.apple.com/jp/pr/library/2015/04/27Apple-Reports-Record-Second-Quarter-Results.html      

    ・
    ・
    ・
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6