Skip to content

pyro2927/RedditKit

 
 

Repository files navigation

RedditKit

RedditKit is a reddit API wrapper, written in Objective-C. This fork has more of a focus on OAuth support than the master repo.

Installation

CocoaPods

Add this to your Podfile:

pod 'RedditKit', :git => "[email protected]:pyro2927/RedditKit.git", :branch => "master"

Then run:

pod install

Manually

  1. Add RedditKit as a git submodule of your project.
  2. Install its dependencies with git submodule update --init --recursive
  3. Drag and drop RedditKit and its dependencies into your project, and you're done!

Getting Started

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.

More Examples

Get the top comments for a link:

RKLink *link = [[self links] firstObject];

RKPagination *pagination = [RKPagination paginationWithLimit:100];
pagination.commentSortingMethod = RKCommentSortingMethodTop;

[[RKClient sharedClient] commentsForLink:link pagination:pagination 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];

Pagination

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.

The example project implements pagination in a table view controller, loading new links when the user scrolls to the bottom.

Multiple Accounts

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.

Configuration

You can configure various aspects of RedditKit's operation, 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.

Requirements

RedditKit requires either iOS 7.0+ or Mac OS X 10.9+. Xcode 5 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.

Dependencies

Credits

SAMCategories by Sam Soffes is used for unescaping HTML entities in reddit link titles.

Need Help?

Open an issue, or hit me up on Twitter.

License

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.

About

An Objective-C wrapper for the reddit API.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 91.6%
  • C 7.0%
  • Other 1.4%