RedditKit is a reddit API wrapper, written in Objective-C.
Documentation for RedditKit is available on CocoaDocs.
Add this to your Podfile:
pod 'RedditKit', '~> 1.3'
Then run:
pod install
Adding RedditKit:
- Add RedditKit as a git submodule of your project:
git submodule add https://github.com/samsymons/RedditKit.git
- Fetch its dependencies with
git submodule update --init --recursive
Adding AFNetworking:
Inside the newly created RedditKit
directory, there exists an External
directory containing its dependencies which the git submodule update
command will have populated. Drag AFNetworking's Classes
directory into your project.
Adding Mantle:
Follow Mantle's instructions on getting the project set up.
Once you have everything set up, you may need to restart Xcode to have it pick up your changes.
RedditKit is structured around the RKClient
class. This class manages authentication for a single reddit account and performs HTTP requests on that user's behalf. RKClient
can be used as a singleton with its sharedClient
class method, or as a standalone object.
[[RKClient sharedClient] signInWithUsername:@"name" password:@"password" completion:^(NSError *error) {
if (!error)
{
NSLog(@"Successfully signed in!");
}
}];
Once you're signed in, RKClient
will keep track of any necessary authentication state. You can then call methods which require authentication, such as getting the subreddits you are subscribed to.
Note: RedditKit does not persist your authentication credentials itself; you'll have to do this manually via the Keychain, and call signInWithUsername:password:completion:
on your application's launch.
[[RKClient sharedClient] subscribedSubredditsWithCompletion:^(NSArray *subreddits, NSError *error) {
NSLog(@"Subreddits: %@", subreddits);
}];
Retrieving the current top links in a subreddit is simple.
RKSubreddit *subreddit = [[self subreddits] firstObject];
[[RKClient sharedClient] linksInSubreddit:subreddit pagination:nil completion:^(NSArray *links, RKPagination *pagination, NSError *error) {
NSLog(@"Links: %@", links);
}];
You can then upvote a link.
RKLink *link = [[self links] firstObject];
[[RKClient sharedClient] upvote:link completion:^(NSError *error) {
NSLog(@"Upvoted the link!");
}];
RedditKit doesn't have any built-in rate limiting. reddit's API rules require that you make no more than 30 requests per minute and try to avoid requesting the same page more than once every 30 seconds. You can read up on the API rules on their wiki page.
Get the top comments for a link:
RKLink *link = [[self links] firstObject];
[[RKClient sharedClient] commentsForLink:link completion:^(NSArray *comments, NSError *error) {
if (comments)
{
NSLog(@"Comments: %@", comments);
}
}];
Fetch a user's account:
[[RKClient sharedClient] userWithUsername:@"name" completion:^(RKUser *account, NSError *error) {
if (account)
{
NSLog(@"%@", account);
}
}];
Send private messages:
[[RKClient sharedClient] sendMessage:@"Hello!" subject:nil recipient:@"samsymons" completion:nil];
Canceling a request:
Each of RedditKit's methods return a NSURLSessionDataTask
, which can be used to cancel a request before it has completed.
NSURLSessionDataTask *task = [[RKClient sharedClient] frontPageLinksWithCompletion:nil];
[task cancel];
When using RedditKit's APIs, it's important to remember that users who have reddit gold may have settings which affect the response from the server. For example, users who have disabled promoted links will see an empty array returned when retrieving links from the promoted category of a subreddit.
Methods which are paginated can accept RKPagination
objects.
RKPagination
lets you change the sorting of returned objects. For example, when fetching the top 25 links in a subreddit, setting the subredditCategory
property changes whether you get the top 25 links right now or the top links overall.
In addition to letting you change the pagination of your requests, RedditKit also gives you pagination information for any requests made. A request for links in a subreddit has a pagination object as an argument in its completion block.
Due to the way reddit's API is structured, comments do not have support for pagination. Instead, comment listings will return RKMoreComments
object which can then be used to retrieve the comments in question.
The example project implements pagination in a table view controller, loading new links when the user scrolls to the bottom.
RKClient
manages a single reddit account. When supporting multiple accounts, all you need to do is switch from using the sharedClient
method to using one RKClient
instance per account.
For example, this code:
[[RKClient sharedClient] signInWithUsername:@"username" password:@"password" completion:nil];
[[RKClient sharedClient] upvote:someLink completion:nil];
Becomes this:
RKClient *client = [[RKClient alloc] init];
[client signInWithUsername:@"username" password:@"password" completion:nil];
[client upvote:someLink completion:nil];
How you manage the various RKClient
instances is up to you. Probably with an NSDictionary
, using reddit usernames as keys.
You can configure various aspects of RedditKit, including its default API endpoint and user agent. Check out the RKClient
header file for more.
You should set your user agent to the name and version of your app, along with your reddit username. That way, if you ever have a buggy version of your app in the wild, the reddit admins will know who to contact.
If you do not set one manually, the user agent will be provided by AFNetworking.
RedditKit requires either iOS 7.0+ or Mac OS X 10.9+. Xcode 5 or greater is required in order to run its test suite.
ARC is required. For projects that don't use ARC, you can set the -fobjc-arc
compiler flag on the RedditKit source files.
SAMCategories by Sam Soffes is used for unescaping HTML entities in reddit link titles.
Also, a big thanks to all of the contributors to RedditKit.
Open an issue, or hit me up on Twitter.
Copyright (c) 2013 Sam Symons (http://samsymons.com/)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.