一个用于获取、解析和更新RSS/Atom订阅源的Swift库
不建议手动安装框架,但如果您不愿意使用上述任何一个依赖管理器,您可以将RSSKit手动整合到项目中。在项目中使用RSSKit的一个常见方式是使用嵌入式框架。
cd
命令进入您的顶级项目目录,然后运行以下命令,如果您的项目没有被初始化为git仓库$ git init
cd
命令进入您的顶级项目目录,并输入以下命令$ git submodule add https://github.com/quan118/RSSKit.git
打开RSSKit
文件夹,并将RSSKit.xcodeproj
拖拽到您的应用项目中的“项目导航器”下
它应该位于您的应用程序的蓝色项目图标下,它是在所有其他Xcode组之上还是之下并不重要。
在Xcode中,通过点击蓝色项目图标并选择侧边栏中的“Targets”部分下的应用目标,导航到目标配置窗口。
RSSKit.framework
。RSSKit.framework
。创建解析器
// Create feed parser and pass the URL of the feed
let feedURL:NSURL = NSURL(string:"http://images.apple.com/main/rss/hotnews/hotnews.rss")
let feedParser:FeedParser = RSSFeedParser(feedURL:url)
设置委托
// Delegate must conform to `RSSFeedParserDelegate`
feedParser.delegate = self
设置解析类型。选项有ParseType.Full
、ParseType.ItemsOnly
、ParseType.InfoOnly
。Info表示关于订阅源的信息,如标题和描述。Items是单个条目或故事。
// Parse the feeds info (title, link) and all feed items
feedParser.feedParseType = ParseType.Full
设置解析器是否应同步或异步连接并下载订阅源数据。注意,这仅影响订阅源数据的下载,不影响解析操作本身。
// Connection type
feedParser.connectionType = ConnectionType.Asynchronously
初始化解析
// Begin parsing
feedParser.parse()
然后解析器将下载并解析订阅源。如果您在任何时候希望停止解析,可以调用
// Stop feed download / parsing
feedParser.stopParsing()
stopParsing
方法将立即停止下载和解析源。
一旦开始解析,代理将接收到解析过程中生成的源数据。
optional func feedParserDidStart(parser:RSSFeedParser) // Called when data has downloaded and parsing has begun
optional func feedParser(parser:RSSFeedParser, didParseFeedInfo info:RSSFeedInfo) // Provides info about the feed
optional func feedParser(parser:RSSFeedParser, didParseFeedItem item:RSSFeedItem) // Provides info about a feed item
optional func feedParserDidFinish(parser:RSSFeedParser) // Parsing complete or stopped at any time by `stopParsing`
optional func feedParser(parser:RSSFeedParser, didFailWithError error: NSError) // Parsing failed
RSSFeedInfo
和 RSSFeedItem
包含属性(标题、链接、摘要等),这些属性将存放解析后的数据。查看 RSSFeedInfo.swift
和 RSSFeedItem.swift
以获取更多信息。
以下是可供源信息和条目对象使用的属性列表
info.title
(String?
)info.link
(String?
)info.summary
(String?
)item.title
(String?
)item.link
(String?
)item.author
(String?
)item.date
(NSDate?
)item.updated
(NSDate?
)item.summary
(String?
)item.content
(String?
)item.enclosures
(具有键 url
、type
和 length
的 Dictionary
组成的 Array
)item.identifier
(String?
)RSSFeedInfo
和 RSSFeedItem
的所有属性都返回源提供的数据。此内容可能包含或不包含 HTML 和编码实体。如果内容包含 HTML,则可以在 UIWebView 中显示数据,或者可以使用提供的 String
类别 (String+HTML
),这将允许您操作此 HTML 内容。便捷的方法有
// Convert HTML to Plain Text
// - Strips HML tags & comments, removes extra whitespace and decodes HTML character entities
public func stringByConvertingHTMLToPlainText() -> String
// Decode all HTML entities using GTM.
public func stringByDecodingHTMLEntities() -> String
// Encode all HTML entities using GTM
public func stringByEncodingHTMLEntities() -> String
RSSKit 采用 MIT 许可证发布。有关详细信息,请参阅 LICENSE。