9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swiftで書かれたRSSパーサー TIFeedParserの使い方

Last updated at Posted at 2016-06-18

TIFeedParserはSwiftで書かれたシンプルなRSSパーサーです。Alamofireなどと一緒に使えばRSSリーダーを簡単に作れます。規格はAtom、RSS1.0、RSS2.0に対応しています。cocoapodsからダウンロードして使えます。

Cocoapodsでのインストール

Podfileに以下のように追記します。

import TIFeedParser

サンプル

AlamofireでRSS feedを取得してから、返ってきた情報をTIFeedParserでパースしてます。

RSS1.0, RSS2.0の場合

func loadRSS() {

        let feedString:String = "https://news.google.com/news?hl=us&ned=us&ie=UTF-8&oe=UTF-8&output=rss"

        Alamofire.request(.GET, feedUrlString, parameters:nil)
            .response {request, response, xmlData, error  in

                if (xmlData == nil) {
                    return
                }

                TIFeedParser.parseRSS(xmlData, completionHandler: {(isSuccess, channel, error) -> Void in

                    if (isSuccess) {
                        // self.items = channel.items!
                        // self.tableView.reloadData()
                    } else {
                        if (error != nil) {
                            // print(error?.localizedDescription)
                        }
                    }
                })
        }
    }

Atomの場合

func loadAtom() {

        let feedString:String = "https://news.google.com/news?ned=us&ie=UTF-8&oe=UTF-8&q=nasa&output=atom&num=3&hl=ja"

        Alamofire.request(.GET, feedUrlString, parameters:nil)
            .response {request, response, xmlData, error  in

                if (xmlData == nil) {
                    return
                }

                TIFeedParser.parseAtom(xmlData, completionHandler: {(isSuccess, feed, error) -> Void in

                    if (isSuccess) {
                        // self.entries = feed.entries!
                        // self.tableView.reloadData()
                    } else {
                        if (error != nil) {
                            // print(error?.localizedDescription)
                        }
                    }
                })
        }
    }
9
8
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
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?