2 <img src="Assets/OAuthSwift-icon.png?raw=true" alt="OAuthSwift"/>
7 Swift based OAuth library for iOS and macOS.
9 ## Support OAuth1.0, OAuth2.0
11 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
13 ## Sponsored by Auth0 <span><img src="https://user-images.githubusercontent.com/1801923/31792116-d4fca9ec-b512-11e7-92eb-56e8d3df8e70.png" height="28" align="top"></span>
14 If you want to easily add authentication to Swift apps, feel free to check out Auth0's Swift SDK and free plan at [auth0.com/overview](https://auth0.com/overview?utm_source=GHsponsor&utm_medium=GHsponsor&utm_campaign=oauthswift&utm_content=auth)
18 OAuthSwift is packaged as a Swift framework. Currently this is the simplest way to add it to your app:
20 * Drag OAuthSwift.xcodeproj to your project in the Project Navigator.
21 * Select your project and then your app target. Open the Build Phases panel.
22 * Expand the Target Dependencies group, and add OAuthSwift framework.
23 * import OAuthSwift whenever you want to use OAuthSwift.
27 * Install Carthage (https://github.com/Carthage/Carthage)
28 * Create Cartfile file
30 github "OAuthSwift/OAuthSwift" ~> 1.2.0
32 * Run `carthage update`.
33 * 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.
43 pod 'OAuthSwift', '~> 1.2.0'
48 Use the `swift3` branch, or the tag `1.1.2` on main branch
51 ### Setting URL Schemes
52 In info tab of your target
53 ![Image](Assets/URLSchemes.png "Image")
54 Replace oauth-swift by your application name
56 ### Handle URL in AppDelegate
57 - On iOS implement `UIApplicationDelegate` method
59 func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
60 if (url.host == "oauth-callback") {
61 OAuthSwift.handle(url: url)
66 :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 :
68 if (options[.sourceApplication] as? String == "com.apple.SafariViewService") {
71 - On macOS you must register an handler on `NSAppleEventManager` for event type `kAEGetURL` (see demo code)
73 func applicationDidFinishLaunching(_ aNotification: NSNotification) {
74 NSAppleEventManager.shared().setEventHandler(self, andSelector:#selector(AppDelegate.handleGetURL(event:withReplyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
76 func handleGetURL(event: NSAppleEventDescriptor!, withReplyEvent: NSAppleEventDescriptor!) {
77 if let urlString = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue, let url = URL(string: urlString) {
78 OAuthSwift.handle(url: url)
83 ### Authorize with OAuth1.0
85 // create an instance and retain it
86 oauthswift = OAuth1Swift(
87 consumerKey: "********",
88 consumerSecret: "********",
89 requestTokenUrl: "https://api.twitter.com/oauth/request_token",
90 authorizeUrl: "https://api.twitter.com/oauth/authorize",
91 accessTokenUrl: "https://api.twitter.com/oauth/access_token"
94 let handle = oauthswift.authorize(
95 withCallbackURL: URL(string: "oauth-swift://oauth-callback/twitter")!,
96 success: { credential, response, parameters in
97 print(credential.oauthToken)
98 print(credential.oauthTokenSecret)
99 print(parameters["user_id"])
103 print(error.localizedDescription)
107 ### OAuth1 without authorization
108 No urls to specify here
110 // create an instance and retain it
111 oauthswift = OAuth1Swift(
112 consumerKey: "********",
113 consumerSecret: "********"
115 // do your HTTP request without authorize
116 oauthswift.client.get("https://api.example.com/foo/bar",
117 success: { response in
126 ### Authorize with OAuth2.0
128 // create an instance and retain it
129 oauthswift = OAuth2Swift(
130 consumerKey: "********",
131 consumerSecret: "********",
132 authorizeUrl: "https://api.instagram.com/oauth/authorize",
133 responseType: "token"
135 let handle = oauthswift.authorize(
136 withCallbackURL: URL(string: "oauth-swift://oauth-callback/instagram")!,
137 scope: "likes+comments", state:"INSTAGRAM",
138 success: { credential, response, parameters in
139 print(credential.oauthToken)
143 print(error.localizedDescription)
149 See demo for more examples
151 ### Handle authorize URL
152 The authorize URL allows the user to connect to a provider and give access to your application.
154 By default this URL is opened into the external web browser (ie. safari), but apple does not allow it for app-store iOS applications.
156 To change this behavior you must set an `OAuthSwiftURLHandlerType`, simple protocol to handle an `URL`
158 oauthswift.authorizeURLHandler = ..
160 For instance you can embed a web view into your application by providing a controller that displays a web view (`UIWebView`, `WKWebView`).
161 Then this controller must implement `OAuthSwiftURLHandlerType` to load the URL into the web view
163 func handle(_ url: NSURL) {
164 let req = URLRequest(URL: targetURL)
165 self.webView.loadRequest(req)
168 and present the view (`present(viewController`, `performSegue(withIdentifier: `, ...)
169 *You can extend `OAuthWebViewController` for a default implementation of view presentation and dismiss*
171 #### Use the SFSafariViewController (iOS9)
172 A default implementation of `OAuthSwiftURLHandlerType` is provided using the `SFSafariViewController`, with automatic view dismiss.
174 oauthswift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthswift)
176 Of course you can create your own class or customize the controller by setting the variable `SafariURLHandler#factory`.
178 ### Make signed request
180 Just call HTTP functions of `oauthswift.client`
183 oauthswift.client.get("https://api.linkedin.com/v1/people/~",
184 success: { response in
185 let dataString = response.string
192 // same with request method
193 oauthswift.client.request("https://api.linkedin.com/v1/people/~", .GET,
194 parameters: [:], headers: [:],
198 See more examples in the demo application: [ViewController.swift](/Demo/Common/ViewController.swift)
200 ## OAuth provider pages
202 * [Twitter](https://dev.twitter.com/oauth)
203 * [Flickr](https://www.flickr.com/services/api/auth.oauth.html)
204 * [Github](https://developer.github.com/v3/oauth/)
205 * [Instagram](http://instagram.com/developer/authentication)
206 * [Foursquare](https://developer.foursquare.com/overview/auth)
207 * [Fitbit](https://dev.fitbit.com/build/reference/web-api/oauth2/)
208 * [Withings](http://oauth.withings.com/api)
209 * [Linkedin](https://developer.linkedin.com/docs/oauth2)
210 * [Dropbox](https://www.dropbox.com/developers/core/docs)
211 * [Dribbble](http://developer.dribbble.com/v1/oauth/)
212 * [Salesforce](https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/)
213 * [BitBucket](https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html)
214 * [GoogleDrive](https://developers.google.com/drive/v2/reference/)
215 * [Smugmug](https://smugmug.atlassian.net/wiki/display/API/OAuth)
216 * [Intuit](https://developer.intuit.com/docs/0100_accounting/0060_authentication_and_authorization/oauth_management_api)
217 * [Zaim](https://dev.zaim.net/home/api/authorize)
218 * [Tumblr](https://www.tumblr.com/docs/en/api/v2#auth)
219 * [Slack](https://api.slack.com/docs/oauth)
220 * [Uber](https://developer.uber.com/docs/ride-requests/guides/authentication/introduction#oauth-20)
221 * [Gitter](https://developer.gitter.im/docs/authentication)
222 * [Facebook](https://developers.facebook.com/docs/facebook-login)
223 * [Spotify](https://developer.spotify.com/web-api/authorization-guide/)
224 * [Trello](https://developers.trello.com/authorize)
225 * [Buffer](https://buffer.com/developers/api/oauth)
226 * [Goodreads](https://www.goodreads.com/api/documentation#oauth)
227 * [Typetalk](http://developer.nulab-inc.com/docs/typetalk/auth)
228 * [SoundCloud](https://developers.soundcloud.com/docs/api/guide#authentication)
229 * [Doper](https://doper.io/developer/oauth)
230 * [NounProject](http://api.thenounproject.com/getting_started.html#authentication)
234 ![Image](Assets/Services.png "Image")
235 ![Image](Assets/TwitterOAuth.png "Image")
236 ![Image](Assets/TwitterOAuthTokens.png "Image")
239 See [CONTRIBUTING.md](.github/CONTRIBUTING.md)
241 [Add a new service in demo app](https://github.com/OAuthSwift/OAuthSwift/wiki/Demo-application#add-a-new-service-in-demo-app)
245 OAuthSwift could be used with others frameworks
247 You can sign [Alamofire](https://github.com/Alamofire/Alamofire) request with [OAuthSwiftAlamofire](https://github.com/OAuthSwift/OAuthSwiftAlamofire)
249 To achieve great asynchronous code you can use one of these integration frameworks
250 - [OAuthSwiftFutures](https://github.com/OAuthSwift/OAuthSwiftFutures) - [BrightFutures](https://github.com/Thomvis/BrightFutures)
251 - [OAuthRxSwift](https://github.com/OAuthSwift/OAuthRxSwift) - [RxSwift](https://github.com/ReactiveX/RxSwift)
252 - [OAuthReactiveSwift](https://github.com/OAuthSwift/OAuthReactiveSwift) - [ReactiveSwift](https://github.com/ReactiveCocoa/ReactiveSwift)
256 OAuthSwift is available under the MIT license. See the LICENSE file for more info.
258 [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat
259 )](http://mit-license.org) [![Platform](https://img.shields.io/badge/platform-iOS_OSX_TVOS-lightgrey.svg?style=flat
260 )](https://developer.apple.com/resources/) [![Language](https://img.shields.io/badge/language-swift-orange.svg?style=flat
261 )](https://developer.apple.com/swift) [![Cocoapod](https://img.shields.io/cocoapods/v/OAuthSwift.svg?style=flat)](http://cocoadocs.org/docsets/OAuthSwift/)
262 [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)