CXFeedParser 0.2.1

CXFeedParser 0.2.1

测试已测试
语言语言 Obj-CObjective C
许可 MIT 协议
发布最新版本2014年12月

Vladimir Grichina 维护。



  • 作者:
  • Michael Waterfall 和 Vladimir Grichina

MWFeedParser — iOS 的 RSS 和 Atom 网络源解析器

MWFeedParser 是一个 Objective-C 框架,用于下载和解析 RSS (1.* 和 2.*) 和 Atom 网络源。它是一个非常简单和清晰的实现,可以从网络源中读取以下信息:

源信息

  • 标题
  • 链接
  • 摘要

源条目

  • 标题
  • 链接
  • 日期(发布条目的日期)
  • 更新日期(如果有的话)
  • 摘要(条目的简短描述)
  • 内容(详细的条目内容,如果有的话)
  • 附件(例如播客、mp3、pdf 等)
  • 标识符(条目的 guid/id)

如果您在 iPhone/iPad 应用中使用 MWFeedParser,请通知我,我很乐意查看它:)

重要:此免费软件在以下条件下提供 MIT 许可(X11 许可):

此软件不能用于诸如(但不仅限于)事件、新闻、经验和活动等数据的存档或收集,以任何与日记/日志保持有关的计划。

完整许可证可在本文档末尾找到。

演示/示例应用

项目中有示例 iPhone 应用程序,展示如何使用解析器显示源的标题、列出所有源条目,并在点击时显示条目的详细信息。

设置解析器

创建解析器

// Create feed parser and pass the URL of the feed
NSURL *feedURL = [NSURL URLWithString:@"http://images.apple.com/main/rss/hotnews/hotnews.rss"];
feedParser = [[MWFeedParser alloc] initWithFeedURL:feedURL];

设置代理

// Delegate must conform to `MWFeedParserDelegate`
feedParser.delegate = self;

设置解析类型。选项有 ParseTypeFullParseTypeInfoOnlyParseTypeItemsOnly。信息指的是关于源的信息,例如它的标题和描述。条目是单个条目或故事。

// Parse the feeds info (title, link) and all feed items
feedParser.feedParseType = ParseTypeFull;

设置解析器是否应该同步或异步地连接和下载源数据。注意,这只会影响源数据的下载,而不会影响解析操作本身。

// Connection type
feedParser.connectionType = ConnectionTypeSynchronously;

启动解析

// Begin parsing
[feedParser parse];

解析器将然后下载和解析源。如果您在任何时候希望停止解析,可以调用

// Stop feed download / parsing
[feedParser stopParsing];

将调用 stopParsing 方法立即停止下载和解析源。

读取源数据

一旦解析开始,代理将接收到解析时的源数据。

- (void)feedParserDidStart:(MWFeedParser *)parser; // Called when data has downloaded and parsing has begun
- (void)feedParser:(MWFeedParser *)parser didParseFeedInfo:(MWFeedInfo *)info; // Provides info about the feed
- (void)feedParser:(MWFeedParser *)parser didParseFeedItem:(MWFeedItem *)item; // Provides info about a feed item
- (void)feedParserDidFinish:(MWFeedParser *)parser; // Parsing complete or stopped at any time by `stopParsing`
- (void)feedParser:(MWFeedParser *)parser didFailWithError:(NSError *)error; // Parsing failed

MWFeedInfoMWFeedItem 包含属性(标题、链接、摘要等),它们将保存解析后的数据。请查看 MWFeedInfo.hMWFeedItem.h 以获取更多信息。

重要:有时源数据可能不包含某些信息,例如标题、链接或摘要。在使用任何数据之前,您应该检查这些数据是否存在

NSString *title = item.title ? item.title : @"[No Title]";
NSString *link = item.link ? item.link : @"[No Link]";
NSString *summary = item.summary ? item.summary : @"[No Summary]";

当源数据成功解析或通过调用 stopParsing 被停止时,方法 feedParserDidFinish: 才会调用。为了确定解析是否成功完成或被停止,您可以调用 isStopped

有关用法示例,请参阅演示项目中的 RootViewController.m

可用的数据

以下是源信息对象和条目对象可用属性列表

MWFeedInfo

  • info.title (NSString)
  • info.link (NSString)
  • info.summary (NSString)

MWFeedItem

  • item.title (NSString)
  • item.link (NSString)
  • item.date (NSDate)
  • item.updated (NSDate)
  • item.summary (NSString)
  • item.content (NSString)
  • item.enclosures (NSArray of NSDictionary with keys url, type and length)
  • item.identifier (NSString)

使用数据

MWFeedInfoMWFeedItem 的所有属性都返回源数据。提供的内容可能包含或未包含 HTML 和编码实体。如果内容包含 HTML,您可以在 UIWebView 中显示数据,或者可以使用提供的 NSString 类别(NSString+HTML),这将允许您操作此 HTML 内容。为您提供这些方便的方法:

// Convert HTML to Plain Text
//  - Strips HTML tags & comments, removes extra whitespace and decodes HTML character entities.
- (NSString *)stringByConvertingHTMLToPlainText;

// Decode all HTML entities using GTM.
- (NSString *)stringByDecodingHTMLEntities;

// Encode all HTML entities using GTM.
- (NSString *)stringByEncodingHTMLEntities;

// Minimal unicode encoding will only cover characters from table
// A.2.2 of http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_Special_characters
// which is what you want for a unicode encoded webpage.
- (NSString *)stringByEncodingHTMLEntities:(BOOL)isUnicode;

// Replace newlines with <br /> tags.
- (NSString *)stringWithNewLinesAsBRs;

// Remove newlines and white space from string.
- (NSString *)stringByRemovingNewLinesAndWhitespace;

// Wrap plain URLs in <a href="..." class="linkified">...</a>
//  - Ignores URLs inside tags (any URL beginning with =")
//  - HTTP & HTTPS schemes only
//  - Only works in iOS 4+ as we use NSRegularExpression (returns self if not supported so be careful with NSMutableStrings)
//  - Expression: (?<!=")\b((http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?)
//  - Adapted from http://regexlib.com/REDetails.aspx?regexp_id=96
- (NSString *)stringByLinkifyingURLs;

以下是一个例子:

// Display item summary which contains HTML as plain text
NSString *plainSummary = [item.summary stringByConvertingHTMLToPlainText];

调试问题

如果解析器似乎没有正常运行,请尝试在 MWFeedParser.h 中启用调试日志记录。这将记录错误消息到控制台并帮助您诊断问题。错误代码及其描述可在 MWFeedParser.h 的顶部找到。

其他信息

MWFeedParser 目前不是线程安全的。

添加到项目中

  1. 打开 MWFeedParser.xcodeproj
  2. MWFeedParser & Categories 组拖入您的项目,确保勾选 将项目复制到目标组的文件夹中
  3. 根据需要将 MWFeedParser.h 导入到您的源代码中。

突出的和推荐的功能

  • 在演示应用程序中演示在 UIWebView 中预览格式化的项目摘要/内容(HTML 与图像、段落等)。
  • 提供在给定与一个或多个相关联的网页源时列出可用源的功能。
  • 支持 Media RSS 扩展(来自 Flickr 等)
  • 支持 GeoRSS 扩展。
  • 查看源网页图标。
  • 查看支持/检测源条目中的图像。

请随时联系并提出/投票建议其他功能。

许可证

版权所有 (C) 2010 迈克尔·沃特福斯

在此特此授予任何人免费获得此软件及其关联文档文件(以下简称“软件”)副本的权利,可在不受限制的情况下处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许将软件提供给他人,以便他们可以进行此类操作,但需遵守以下条件

  1. 上述版权声明和本许可声明应包含在所有软件副本或主要部分中。

  2. 此软件不能用于诸如(但不仅限于)事件、新闻、经验和活动等数据的存档或收集,以任何与日记/日志保持有关的计划。

软件供应商提供的软件是“现状”的,未经任何形式的保证,无论是明示的还是暗示的,包括但不仅限于适销性、适用特定目的和不侵犯专利权利的保证。在没有任何情况下,作者或版权所有者不对任何索赔、损害或其他责任承担任何责任,无论其基于合同、侵权或其他,是否源自、由或与软件或其使用或其它方式有关。

联系方式

网站:http://michaelwaterfall.com 推特:http://twitter.com/mwaterfall