FeedKit 是一个 Swift 库,用于读取和生成 RSS、Atom 和 JSON feeds。
FeedKit v10
现在是候选发布版 (RC),并引入了新的解析引擎、功能和改进。虽然此版本功能完整并经过广泛测试,但在最终发布之前可能仍有一些小问题需要解决。
它应该足够稳定 👀,但如果“足够稳定”还不够,请考虑使用 v9
。
Feed 读取可以使用专用类型(例如 RSSFeed
、AtomFeed
和 JSONFeed
)或通用类型 Feed
。
当您知道要读取的 feed 类型时,请使用专用类型。
try await RSSFeed(urlString: "https://developer.apple.com/news/rss/news.rss")
当您不知道 feed 类型时,请使用通用 Feed
枚举类型。
Feed
枚举类型处理 RSS、Atom 和 JSON feeds,并将在读取、解析和解码之前确定 feed 的类型。
// Read any type of feed
let feed = try await Feed(urlString: "https://surprise.me/feed")
// Use a switch to get the resulting feed model
switch feed {
case let .atom(feed): // An AtomFeed instance
case let .rss(feed): // An RSSFeed instance
case let .json(feed): // A JSONFeed instance
}
上面的通用 Feed
枚举自动确定 feed 类型,因此您通常不需要手动执行此操作。但是,当您想在不进行解析和解码开销的情况下识别 feed 类型时,使用 FeedType
枚举可能会有所帮助。
let feedType = try FeedType(data: data)
// Detect feed type
switch feedType {
case .rss: // RSS feed detected
case .atom: // Atom feed detected
case .json: // JSON feed detected
}
// Or feed content type
if feedType.isXML {
// XML feed detected
} else if feedType.isJson {
// JSON feed detected
}
所有 feed 类型都符合 FeedInitializable
协议,共享多个通用初始化器。它们提供了一种灵活的方式从最常见的来源读取 feeds。
从 URL String
init(urlString: String) async throws
从 URL
,处理本地文件 URL 和远程 URL
init(url: URL) async throws
从本地文件 URL
init(fileURL url: URL) throws
从远程 URL
init(remoteURL url: URL) async throws
从 XML 或 JSON String
init(string: String) throws
从原始 Data
init(data: Data) throws
要为任何给定的 XML feed 生成 XML 字符串,请创建 RSSFeed
或 AtomFeed
的实例,并使用必要的数据填充它。
let feed = RSSFeed(
channel: .init(
title: "Breaking News",
link: "http://www.breakingnews.com/",
description: "Get the latest updates as they happen.",
// ...
items: [
.init(
title: "Breaking News: All Hearts are Joyful",
link: "http://breakingnews.com/2025/01/09/joyful-hearts",
description: "A heartwarming story of unity and celebration."
// ...
),
]
)
)
然后调用 toXMLString(formatted:)
生成 XML 字符串。
try feed.toXMLString(formatted: true)
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Breaking News</title>
<link>http://www.breakingnews.com/</link>
<description>Get the latest updates as they happen.</description>
<item>
<title>Breaking News: All Hearts are Joyful</title>
<link>http://breakingnews.com/2025/01/09/joyful-hearts</link>
<description>A heartwarming story of unity and celebration.</description>
</item>
</channel>
</rss>
以相同的方式,您可以创建 JSONFeed
的实例,填充它,然后调用 toJSONString(formatted:)
生成 JSON 字符串。
try feed.toJSONString(formatted: true)
RSS、Atom 和 JSON feed 模型非常全面,特别是当与所有支持的命名空间结合使用时。以下是可用功能的一小部分预览。
channel.title
channel.link
channel.description
channel.language
channel.copyright
channel.managingEditor
channel.webMaster
channel.pubDate
channel.lastBuildDate
channel.categories
channel.generator
channel.docs
channel.cloud
channel.rating
channel.ttl
channel.image
channel.textInput
channel.skipHours
channel.skipDays
// ...
channel.dublinCore
channel.syndication
channel.iTunes
channel.atom
// ...
let item = channel.items?.first
item?.title
item?.link
item?.description
item?.author
item?.categories
item?.comments
item?.enclosure
item?.guid
item?.pubDate
item?.source
//...
item?.dublinCore
item?.content
item?.iTunes
item?.media
// ...
feed.title
feed.subtitle
feed.links
feed.updated
feed.authors
feed.contributors
feed.id
feed.generator
feed.icon
feed.logo
feed.rights
// ...
let entry = feed.entries?.first
entry?.title
entry?.summary
entry?.authors
entry?.contributors
entry?.links
entry?.updated
entry?.categories
entry?.id
entry?.content
entry?.published
entry?.source
entry?.rights
// ...
entry?.media
entry?.youTube
feed.version
feed.title
feed.homePageURL
feed.feedUrl
feed.description
feed.userComment
feed.nextUrl
feed.icon
feed.favicon
feed.author
feed.expired
feed.hubs
// ...
let item = feed.items?.first
item?.id
item?.url
item?.externalUrl
item?.title
item?.contentText
item?.contentHtml
item?.summary
item?.image
item?.bannerImage
item?.datePublished
item?.dateModified
item?.author
item?.url
item?.tags
item?.attachments
// ...
要将 FeedKit 添加到您的 Xcode 项目,请按照以下步骤操作
FeedKit 在 MIT 许可证下发布。有关详细信息,请参阅 LICENSE。