CSFeedKit
macOS 的 RSS 提供商和解析器。支持 Swift 和 Objective-C。
安装
通过将以下行添加到您的 Podfile 使用 CocoaPods 安装
use_frameworks!
target 'MyApp' do
pod 'CSFeedKit'
end
创建 RSS 提供商
以下示例创建了一个 RSS 提供商,并打印出了结果 XML 字符串。
// Create a channel
let channel = CSRSSFeedChannel.init(title: "My RSS feed", link: "http://my.rss.feed/", description: "My first CSFeedKit RSS feed")
channel.category = "Examples"
// Add two items to the channel
var items = Array<CSRSSFeedItem>()
items.append(CSRSSFeedItem(title: "Item 1" , link: "http://my.rss.feed/item1", description: "The coolest item so far."))
items.append(CSRSSFeedItem(title: "Item 2" , link: "http://my.rss.feed/item2", description: "An even cooler item."))
channel.items = items
// Create the feed
let feed = CSRSSFeed()
// Add the channel to the feed
feed.channels = [channel]
// Output the XML
print(feed.xmlElement().xmlString(options: .nodePrettyPrint))
解析 RSS 提供商
以下代码打印出了 Hacker News RSS 提供商中条目的标题和 URL。
// Get the XML string (don't do it like this in the real-world ;) )
let xmlString = try String(contentsOf: URL(string: "https://news.ycombinator.com/rss")!)
// Init the feed
let feed = try CSRSSFeed(xmlString: xmlString)
// Print channel info
let channel = feed.channels[0]
print("channel: \(channel.title) - \(channel.pubDate ?? Date.distantPast)")
// Print the items
for item in channel.items {
print(" * \(item.pubDate ?? Date.distantPast) - \(item.title) (\(item.link))")
}
接下来是什么
查看完整的文档:CocoaDocs。