MWFeedParser 是一个 Objective-C 框架,用于下载和解析 RSS (1.* 和 2.*) 和 Atom 网络源。它是一个非常简单和清晰的实现,可以从网络源中读取以下信息:
如果您在 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;
设置解析类型。选项有 ParseTypeFull
、ParseTypeInfoOnly
、ParseTypeItemsOnly
。信息指的是关于源的信息,例如它的标题和描述。条目是单个条目或故事。
// 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
MWFeedInfo
和 MWFeedItem
包含属性(标题、链接、摘要等),它们将保存解析后的数据。请查看 MWFeedInfo.h
和 MWFeedItem.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
。
以下是源信息对象和条目对象可用属性列表
info.title
(NSString
)info.link
(NSString
)info.summary
(NSString
)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
)MWFeedInfo
和 MWFeedItem
的所有属性都返回源数据。提供的内容可能包含或未包含 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\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)
// - 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 目前不是线程安全的。
MWFeedParser.xcodeproj
。MWFeedParser
& Categories
组拖入您的项目,确保勾选 将项目复制到目标组的文件夹中。MWFeedParser.h
导入到您的源代码中。UIWebView
中预览格式化的项目摘要/内容(HTML 与图像、段落等)。请随时联系并提出/投票建议其他功能。
版权所有 (C) 2010 迈克尔·沃特福斯
在此特此授予任何人免费获得此软件及其关联文档文件(以下简称“软件”)副本的权利,可在不受限制的情况下处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许将软件提供给他人,以便他们可以进行此类操作,但需遵守以下条件
上述版权声明和本许可声明应包含在所有软件副本或主要部分中。
此软件不能用于诸如(但不仅限于)事件、新闻、经验和活动等数据的存档或收集,以任何与日记/日志保持有关的计划。
软件供应商提供的软件是“现状”的,未经任何形式的保证,无论是明示的还是暗示的,包括但不仅限于适销性、适用特定目的和不侵犯专利权利的保证。在没有任何情况下,作者或版权所有者不对任何索赔、损害或其他责任承担任何责任,无论其基于合同、侵权或其他,是否源自、由或与软件或其使用或其它方式有关。
网站:http://michaelwaterfall.com 推特:http://twitter.com/mwaterfall