CBPWordPress 0.1.2

CBPWordPress 0.1.2

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最新发布2015 年 1 月

Karl Monaghan 维护。




CBPWordPress 是一个用于在您的应用中展示 WordPress 博客内容的库。

要求

您需要在 WordPress 安装中安装并激活 Broadsheet.ie 分支的 WP-JSON-API 插件。

安装

Podfile

pod 'CBPWordPress'

用法

指向您自己的 WordPress 安装非常简单。对库的第一个调用应该是设置 API 的根 URL。

[CBPWordPressAPIClient rootURI:@"http://YOUR-API-URL"];

一旦设置,API 调用将使用该 URL。

获取帖子列表

要获取帖子列表,您可以使用 NSURLSessionDataTask 类别的 fetchPostsWithParams:withBlock: 方法。

以下示例中,检索了最新帖子的第一页,并将帖子分配给一个数组。

__weak typeof(self) weakSelf = self;

[NSURLSessionDataTask fetchPostsWithParams:@{@"page": @(1)}
                                 withBlock:^(CBPWordPressPostsContainer *data, NSError *error) {
                                    if (error) {
                                        //Handle Error
                                        return;
                                    }

                                    __strong typeof(weakSelf) strongSelf = weakSelf;

                                    strongSelf.posts = data.posts;
}];

允许的参数包括

  • page: 您想要获取的页。从 1 开始。
  • count: 要检索的帖子数量。默认为 10。

获取帖子

如果您知道帖子 ID,您可以使用 NSURLSessionDataTask 类别的 fetchPostWithId:withBlock: 方法来获取帖子。以下示例中检索帖子 1234 并将其分配给一个本地帖子变量。

__weak typeof(self) weakSelf = self;

[NSURLSessionDataTask fetchPostWithId:1234
                            withBlock:^(CBPWordPressPost *post, NSError *error){
                                if (error) {
                                    //Handle Error
                                    return;
                                }

                                __strong typeof(weakSelf) strongSelf = weakSelf;

                                strongSelf.post = post;
}];

如果您有帖子的 URL,您可以使用 fetchPostWithURL:withBlock: 方法代替。您将帖子的完整 URL 作为参数传递。

在帖子上发表评论

要发表评论,可以使用 NSURLSessionDataTask 类别的 postComment:withBlock: 方法。该方法将 CBPWordPressComment 对象作为第一个参数。以下是一个初始化评论的示例。

CBPWordPressComment *newComment = [CBPWordPressComment new];
newComment.postId = 1234;
newComment.email = @"[email protected]";
newComment.name = @"Jonny Appleseed";
newComment.content = @"This is a comment!";
//Optional
newComment.url = @"http://somewebsite.com";
//If the comment is replying to another comment
newComment.parent = 1234;

请注意,URL 和父属性是可选的,但其他所有内容都是必需的。如果用户正在回复评论,则应设置父属性并应为该评论的 ID。

初始化评论后,将其传递给 postComment:withBlock: 方法。在以下示例中,新评论被提交,并在成功时设置为返回的评论对象。

__weak typeof(self) weakSelf = self;

[NSURLSessionDataTask postComment:newComment
                        withBlock:^(CBPWordPressComment *comment, NSError *error){
                            __strong typeof(weakSelf) strongSelf = weakSelf;

                            if (error) {
                                //Handle error
                                return;
                            }

                            strongSelf.comment = newComment;
}];

已知问题:如果 WordPress 检测到重复评论,则返回结果是 HTML 而不是 JSON。

待办事项

这个库还在不断完善中。计划添加的一些功能包括:

  • 添加从WP-API插件获取数据的选项
  • 实现针对每个WP-JSON-API端点的辅助方法(例如,get_category_posts、get_tag_posts等)
  • Today Extension添加到示例应用中

贡献

欢迎通过pull请求和建议进行贡献。

联系

Karl Monaghan

许可

CBPWordPress遵循MIT许可协议。有关更多信息,请参阅LICENSE文件。