Medium SDK - Swift
A library to allow access to Medium API for any Swift iOS application.
Features
- Medium.com authorization & token handling
- Login state saved in user defaults
- Supported scopes: basicProfile, listPublications, publishPost
- Implemented all the related requests from the Medium API docs
- Support Safari View Controller
Compatibility
- iOS 9.0+
- osx 10.10
Installation
pod 'MediumSDKSwift'
and run pod install
in the directory to load dependencies.
Configuration
Add your MEDIUM_CLIENT_ID
, MEDIUM_CLIENT_SECRET
, MEDIUM_CALLBACK_URL
in Build settings
-> User-Define
In the project right click on your Info.plist
file, choose "Open As" - "Source code" and add these lines :
CFBundleURLTypes
CFBundleURLName
$(PRODUCT_BUNDLE_IDENTIFIER)
CFBundleURLSchemes
mediumswift
MediumCallbackURL
$(MEDIUM_CALLBACK_URL)
MediumClientID
$(MEDIUM_CLIENT_ID)
MediumClientSecret
$(MEDIUM_CLIENT_SECRET)
right before the tag.
Handle the callback in App Delegate:
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
if (url.host == "mediumswift-callback") {
OAuthSwift.handleOpenURL(url)
}
return true
}
Note that the redirect is done via a free Heroku app and it's highly recommended to setup your own redirect service by cloning and running this repo (courtesy of the original oauthswift.herokuapp.com repo). Change all the mediumswift
strings in the repo to the name of your app and make related changes in Info.plist
and App Delegate
files. The deployment with Heroku is easy - just link your GitHub repo and the app is built automatically.
You are all set!
Usage
MediumSDKManager
class is designed as a singleton, you can access all it's methods by calling a shared instance:
let mediumSession = MediumSDKManager.sharedInstance
List of methods of the MediumSDKManager
class:
- Authorize, completion handler returns state: success/failure and user's medium token or error string:
doOAuthMedium(completionHandler: (String, String) -> Void)
- Check login credentials, completion handler returns state: success/failure and medium token or error string:
checkCred(completionHandler: (String, String) -> Void)
- Get current user ID, completion handler returns state: success/failure and user ID or error string:
getUserID(completionHandler: (String, String) -> Void)
- Get current Medium token, completion handler returns state: success/failure and medium token or error string:
getToken(completionHandler: (String, String) -> Void)
- Sign out, completion handler returns state: success/failure, and message or error string:
signOutMedium(completionHandler: (String, String) -> Void)
- Get current user's credentials, completion handler returns state: success/failure, and user ID as or error string:
ownCredentialsRequest(completionHandler: (String, String) -> Void)
- Get list of current user's publications, completion handler returns state: success/failure, number of users publications or error string and publications JSON if present:
userPublicationsListRequest(completionHandler: (String, String, JSON) -> Void)
- Get list of a publication's contributors, completion handler returns state: success/failure, number of users publications or error string and publications JSON if present:
getListOfContributors(publicationId: String, completionHandler: (String, String, JSON) -> Void)
- Create new post, completion handler returns state: success/failure, and message or error string:
createPost(title: String, contentFormat: String, content: String, canonicalUrl: String?=nil, tags: [String]?=nil, publishStatus: MediumPublishStatus?=nil, license: MediumLicense?=nil, completionHandler: (String, String) -> Void)
- Create a post under existing publication, completion handler returns state: success/failure, and message or error string:
createPostUnderPublication(rootPublication: String, title: String, contentFormat: String, content: String, canonicalUrl: String?=nil, tags: [String]?=nil, publishStatus: MediumPublishStatus?=nil, license: MediumLicense?=nil, completionHandler: (String, String) -> Void)
Note that publish status and licence fields are coded as enums with all the possible states of these parameters, when creating a publication just use the proper values.
Release History
- 0.1.1
- Remove OSX
- 0.1.0
- Update syntax for Swift 3.0
- Drop support <= iOS 9.0
- Update dependencies
- 0.0.4
- add User-defnie variables
- current version
- 0.0.3
- bug fixies
- 0.0.2
- update dependencies
- 0.0.1
- The first release
Contribute
We would love for you to contribute to Medium SDK - Swift, check the LICENSE
file for more info.
Meta
Original author:
- Alexander Telegin – @drinkius – [email protected]
Maintained:
- Fernando Bass = @ferbass
Contributions by
- Reejo Samuel - @reejosamuel
Created as a part of development internship, distributed under the MIT license. See LICENSE
for more information.