Swift based OAuth library for iOS and OSX.
Twitter, Flickr, Github, Instagram, Foursquare. Fitbit, Withings, Linkedin, Dropbox, Dribbble, Salesforce, BitBucket, GoogleDrive, Smugmug, Intuit, Zaim, Tumblr, Slack, Uber, Gitter, Facebook 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.
- Install Carthage (https://github.com/Carthage/Carthage)
- Create Cartfile file
github "dongri/OAuthSwift" ~> 0.4.6
- 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.
- Podfile
platform :ios, '8.0'
use_frameworks!
pod "OAuthSwift", "~> 0.4.8"
Replace oauth-swift by your application name
func application(application: UIApplication!, openURL url: NSURL!, sourceApplication: String!, annotation: AnyObject!) -> Bool {
if (url.host == "oauth-callback") {
if (url.path!.hasPrefix("/twitter")){
OAuth1Swift.handleOpenURL(url)
}
if ( url.path!.hasPrefix("/github" )){
OAuth2Swift.handleOpenURL(url)
}
}
return true
}
let 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"
)
oauthswift.authorizeWithCallbackURL(
NSURL(string: "oauth-swift://oauth-callback/twitter"),
success: { credential, response, parameters in
println(credential.oauth_token)
println(credential.oauth_token_secret)
println(parameters["user_id"])
},
failure: { error in
print(error.localizedDescription)
}
)
let oauthswift = OAuth2Swift(
consumerKey: "********",
consumerSecret: "********",
authorizeUrl: "https://api.instagram.com/oauth/authorize",
responseType: "token"
)
oauthswift.authorizeWithCallbackURL(
NSURL(string: "oauth-swift://oauth-callback/instagram"),
scope: "likes+comments", state:"INSTAGRAM",
success: { credential, response, parameters in
println(credential.oauth_token)
},
failure: { error in
print(error.localizedDescription)
}
)
See demo for more examples
The authorize URL allow 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 don't allow it for app-store iOS application.
To change this behavior you must set an OAuthSwiftURLHandlerType
, simple protocol to handle an NSURL
oauthswift.authorize_url_handler = ..
For instance you can embed a web view into your application by providing a controller that display 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 = NSURLRequest(URL: targetURL)
self.webView.loadRequest(req)
...
and present the view (presentViewController
, performSegueWithIdentifier
, ...)
You can extends OAuthWebViewController
for a default implementation of view presentation and dismiss
A default implementation of OAuthSwiftURLHandlerType
is provided using the SFSafariViewController
, with automatic view dismiss.
oauthswift.authorize_url_handler = SafariURLHandler(viewController: self)
Of course you can create your own class or customize the controller by setting the variable SafariURLHandler#factory
.
- Flickr
- Github
- Foursquare
- Fitbit
- Withings
- Dropbox
- Dribbble
- Salesforce
- BitBucket
- GoogleDrive
- Smugmug
- Intuit
- Zaim
- Tumblr
- Slack
- Uber
- Gitter
See CONTRIBUTING.md
OAuthSwift is available under the MIT license. See the LICENSE file for more info.