Swift Reddit API Wrapper
reddift is Swift Reddit API Wrapper framework, and includes a browser is developed using the framework.
Now, it's under developing. You have to pay attention to use this library.
# check out reddift and its submodules. > git clone --recursive https://github.com/sonsongithub/reddift.git
Check that these libraries are checked out at each path correctly.
/framework/vendor/HTMLSpecialCharacters /framework/vendor/MiniKeychain
Create new installed app via preference page at reddit.com. And then, check your app's
cliend_idand fill out
redirect_urifor OAuth2. For example,
redirect_uriis set to
myapp://response. In following sample,
redirect_uriis set to
reddift://response.
reddift_config.json
This JSON file saves application information to use OAuth. Rename
reddift_config.json.sampleto
reddift_config.json. And fill out
DeveloperName,
redirect_uriand
client_id.
redirect_urimust be same one you registered at reddit.com. You can check
client_idat application tab. reddift generates http's user-agent property using this JSON and application's info.plist.
{ "DeveloperName": "", "redirect_uri": "", "client_id": "" }
In Xcode, set up URL Types in order to receive call back from Safari. Set
URL Schemesto
redirect_urithat you set at reddit.com. You don't have to include
://responseto this form. These URI must be identical. If they are not identical, reddit.com does not authorize your OAuth request. In following sample,
URL Schemesis set to
reddift.
Test uses Application Only OAuth to remove user interaction from test process. If you want to run tests of reddift, you have to create another "Script" type application(personal use script) at reddit.com.
test_config.json
At first, rename
test_config.json.sampleto
test_config.json. Fill each following value using above preference pain of reddit.com.
{ "username": "test user account", "password": "test user password", "client_id": "test app client ID(must be script type app)", "secret": "test app secret" }
Cmd + U.
You have to build dependent frameworks using
carthagebefore building a sample application using Xcode.
# before open xcode project file. > carthage update --platform iOS
carthageworks corretly, you can get following frameworks at each path.
/Carthage/Build/iOS/FLAnimatedImage.framework /Carthage/Build/iOS/YouTubeGetVideoInfoAPIParser.framework /Carthage/Build/iOS/UZTextView.framework
And, you get to edit URI types and reddift_config.json as same as the framework.
reddift returns
Resultobject as a result. Get the value or error from
Resultobject. Concretely, you can access either value evaluating enum state like a following code.
// do not use "!" in your code switch(result) { case .failure(let error): println(error) case .success(let listing): // do something to listing }
In more detail about this coding style, see "Efficient JSON in Swift with Functional Concepts and Generics".
At first, you have to implement codes to receive the response of OAuth2 in
UIAppDelegate. reddift let you save tokens as a specified name into KeyChain. Specifically, following sample code saves token as user name at reddit.com.
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool { return OAuth2Authorizer.sharedInstance.receiveRedirect(url, completion:{(result) -> Void in switch result { case .failure(let error): print(error) case .success(let token): dispatch_async(dispatch_get_main_queue(), { () -> Void in OAuth2TokenRepository.saveIntoKeychainToken(token, name:token.name) }) } }) }
To communicate with reddit.com via OAuth2, you have to create
Sessionobject. See following section about getting response or error handling.
let result = OAuth2TokenRepository.restoreFromKeychainWithName(name) switch result { case .failure(let error): print(error.description) case .success(let token): con.session = Session(token: token) }
You can get contents from reddit via
Sessionobject like following codes.
session?.getList(paginator, subreddit:subreddit, sort:sortTypes[seg.selectedSegmentIndex], timeFilterWithin:.All, completion: { (result) in switch result { case .failure(let error): print(error) case .success(let listing): self.links.appendContentsOf(listing.children.flatMap{$0 as? Link}) } })
You can use
OAuth2AppOnlyTokenwhen you want to write a code for test or personal script tool(such as CLI).
OAuth2AppOnlyTokenenabled to access reddit without human action in order to authorize in web browser apps. Do not use
Oauth2AppOnlyTokenin installed app in terms of security.
OAuth2AppOnlyToken.getOAuth2AppOnlyToken( username: username, password: password, clientID: clientID, secret: secret, completion:( { (result) -> Void in switch result { case .failure(let error): print(error) case .success(let token): self.session = Session(token: token) } }))
In more detail, See my sample application, test code or Playground code included in this repository.
You can play with reddift in Playground. In more detail, check reddift.playground package. Before using, you have to copy
test_config.jsoninto
./reddift.playground/Resourcesin order to specify user account and your application informatin because reddift on Playground uses "Application Only OAuth".
MIT License.