Swiftpack.co - Package - OAuthSwift/OAuthSwift



Swift based OAuth library for iOS and macOS.

Support OAuth1.0, OAuth2.0

Twitter, Flickr, Github, Instagram, Foursquare, Fitbit, Withings, Linkedin, Dropbox, Dribbble, Salesforce, BitBucket, GoogleDrive, Smugmug, Intuit, Zaim, Tumblr, Slack, Uber, Gitter, Facebook, Spotify, Typetalk, SoundCloud, etc


OAuthSwift is packaged as a Swift framework. Currently this is the simplest way to add it to your app:

  • Drag OAuthSwift.xcodeproj to your project in the Project Navigator.
  • Select your project and then your app target. Open the Build Phases panel.
  • Expand the Target Dependencies group, and add OAuthSwift framework.
  • import OAuthSwift whenever you want to use OAuthSwift.

Support Carthage

  • Install Carthage (https://github.com/Carthage/Carthage)
  • Create Cartfile file
github "OAuthSwift/OAuthSwift" ~> 1.2.0
  • Run carthage update.
  • On your application targets’ “General” settings tab, in the “Embedded Binaries” section, drag and drop OAuthSwift.framework from the Carthage/Build/iOS folder on disk.

Support CocoaPods

  • Podfile
platform :ios, '10.0'

pod 'OAuthSwift', '~> 1.2.0'

swift 3

Use the swift3 branch, or the tag 1.1.2 on main branch

How to

Setting URL Schemes

In info tab of your target Image Replace oauth-swift by your application name

Handle URL in AppDelegate

  • On iOS implement UIApplicationDelegate method
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
  if (url.host == "oauth-callback") {
    OAuthSwift.handle(url: url)
  return true

:warning: Any other application may try to open a URL with your url scheme. So you can check the source application, for instance for safari controller :

if (options[.sourceApplication] as? String == "com.apple.SafariViewService") {
  • On macOS you must register an handler on NSAppleEventManager for event type kAEGetURL (see demo code)
func applicationDidFinishLaunching(_ aNotification: NSNotification) {
    NSAppleEventManager.shared().setEventHandler(self, andSelector:#selector(AppDelegate.handleGetURL(event:withReplyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
func handleGetURL(event: NSAppleEventDescriptor!, withReplyEvent: NSAppleEventDescriptor!) {
    if let urlString = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue, let url = URL(string: urlString) {
        OAuthSwift.handle(url: url)

Authorize with OAuth1.0

// create an instance and retain it
oauthswift = OAuth1Swift(
    consumerKey:    "********",
    consumerSecret: "********",
    requestTokenUrl: "https://api.twitter.com/oauth/request_token",
    authorizeUrl:    "https://api.twitter.com/oauth/authorize",
    accessTokenUrl:  "https://api.twitter.com/oauth/access_token"
// authorize
let handle = oauthswift.authorize(
    withCallbackURL: URL(string: "oauth-swift://oauth-callback/twitter")!,
    success: { credential, response, parameters in
      // Do your request
    failure: { error in

OAuth1 without authorization

No urls to specify here

// create an instance and retain it
oauthswift = OAuth1Swift(
    consumerKey:    "********",
    consumerSecret: "********"
// do your HTTP request without authorize
    success: { response in
    failure: { error in

Authorize with OAuth2.0

// create an instance and retain it
oauthswift = OAuth2Swift(
    consumerKey:    "********",
    consumerSecret: "********",
    authorizeUrl:   "https://api.instagram.com/oauth/authorize",
    responseType:   "token"
let handle = oauthswift.authorize(
    withCallbackURL: URL(string: "oauth-swift://oauth-callback/instagram")!,
    scope: "likes+comments", state:"INSTAGRAM",
    success: { credential, response, parameters in
      // Do your request
    failure: { error in

See demo for more examples

Handle authorize URL

The authorize URL allows the user to connect to a provider and give access to your application.

By default this URL is opened into the external web browser (ie. safari), but apple does not allow it for app-store iOS applications.

To change this behavior you must set an OAuthSwiftURLHandlerType, simple protocol to handle an URL

oauthswift.authorizeURLHandler = ..

For instance you can embed a web view into your application by providing a controller that displays a web view (UIWebView, WKWebView). Then this controller must implement OAuthSwiftURLHandlerType to load the URL into the web view

func handle(_ url: NSURL) {
  let req = URLRequest(URL: targetURL)

and present the view (present(viewController, performSegue(withIdentifier:, ...) You can extend OAuthWebViewController for a default implementation of view presentation and dismiss

Use the SFSafariViewController (iOS9)

A default implementation of OAuthSwiftURLHandlerType is provided using the SFSafariViewController, with automatic view dismiss.

oauthswift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthswift)

Of course you can create your own class or customize the controller by setting the variable SafariURLHandler#factory.

Make signed request

Just call HTTP functions of oauthswift.client

      success: { response in
        let dataString = response.string
      failure: { error in
// same with request method
oauthswift.client.request("https://api.linkedin.com/v1/people/~", .GET,
      parameters: [:], headers: [:],
      success: { ...

See more examples in the demo application: ViewController.swift

OAuth provider pages


Image Image Image



Add a new service in demo app


OAuthSwift could be used with others frameworks

You can sign Alamofire request with OAuthSwiftAlamofire

To achieve great asynchronous code you can use one of these integration frameworks


OAuthSwift is available under the MIT license. See the LICENSE file for more info.

License Platform Language Cocoapod Carthage compatible


Stars: 2206
Help us keep the lights on



1.1.2 - Jun 4, 2017


  • add encodeCallbackURL to oauth 2 to URL encore callback URL. Seems to be needed for some services issue #339, pr #325
  • in SafariURLHandler presentViewControllerAnimated variable added by @felipeflorencio
  • Optionally add callback URL to authorize URL using addCallbackURLToAuthorizeURL field of OAuth1Swift , as requested by PR #373
  • add NounProject and Digu to demo app


  • swift 3.1 warning @patricks @hiragram
  • restore OAuth1Swift init without URL parameters @josephquigley
  • Allow changing grand type of device token authorisation Fix #377
  • Add parameters parameter to renewAccessToken @wesley-dynamicowl


  • Do not send oauth_verifier query parameter if not necessary ie. if allowMissingOAuthVerifier=true, Fix #334
  • Fix a crash when encoding credentials in a test target @eunikolsky

1.1.1 - Feb 26, 2017


  • Now success Handler return a OAuthSwiftResponse, which contains the Data, the HTTPURLResponse and URLRequest
    • On this object you can get decoded json object, string value, ...
    • You can extends OAuthSwiftResponse to use some JSON decoded like SwiftyJSON or any object mapper
  • If there is some problem to encode URL with OAuth2, "handle" is not returned (request is cancelled)


  • OAuthSwiftError implement CustomNSError protocol
  • Ability to specify body data for POST request
  • Add wordpress in demo


  • Fix multipart request with some parameter #287 @ktakayama
  • Fix OAuthSwiftCredential.Version init(:) error @bzmario

1.1.0 - Nov 5, 2016

In success handler/closure an OAuthSwiftResponse object is now returned instead of multiple objects (data and http response)

oauthswift.client.get("an url",
      success: { response in
           let data = response.data // response.string for utf8 decoded string from data

You can extends OAuthSwiftResponse to return a decoded object by reading JSON for instance

You can also get the URLRequest send to the server into this OAuthSwiftResponse object

Backward compatibility with Objective C work with prefixed objc_ functions

1.0.0 - Oct 6, 2016

Update to swift 3

  • functions renamed (ex: authorizeWithCallbackURL( -> authorize(withCallbackURL:)
  • error in callback is typed OAuthSwiftError
  • variable with _ renamed (ex: oauthSwift.client.credential.oauth_token-> oauthSwift.client.credential.oauthToken)
  • add some type alias (ex: OAuthSwift.Headers , OAuthSwift.Parameters)
  • ...

Take a look to also updated project OAuthSwiftAlamofire if you want to use Alamofire to make request

0.6.0 - Jul 28, 2016

  • See Milestone for bug fix
  • many change from @FGoessler PR #219
    • fix memory leak
    • allow to cancel request
    • ..
  • Fix and customisation on provided web view controller presentation and dismissal
  • Demo app have now a form to choose url handler and fill key and secret
  • add SoundCloud to OAuthSwiftDemo @satoshin2071

:warning: If you update and use SafariURLHandler, you will have a compilation issue: SafariURLHandlerinit now take an OAuthSwiftobject as parameter