TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Oct 2016 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by evermeer.
Depends on: | |
Alamofire | >= 0 |
KeychainAccess | >= 0 |
A Swift implementation of OAuth2 for iOS using Alamofire.
This library is heavilly inspired by the SwiftOAuth2 repository from crousselle
AlamofireOauth2 relies on Alamofire, and KeychainAccess
'AlamofireOauth2' is now available through the dependency manager CocoaPods. You do have to use cocoapods version 0.36. At this moment this can be installed by executing:
[sudo] gem install cocoapods
I have now moved on to Swift 2. If you want to use AlamofireOauth2, then get that version by using the podfile command:
use_frameworks!
pod "AlamofireOauth2", '~> 1.0'
Version 0.36 of cocoapods will make a dynamic framework of all the pods that you use. Because of that it's only supported in iOS 8.0 or later. When using a framework, you also have to add an import at the top of your swift file like this:
import AlamofireOauth2
If you want support for older versions than iOS 8.0, then you can also just copy the AlamofireOauth2 folder containing the 4 classes to your app. besides that you also have to embed the Alamofire, and KeychainAccess libraries
1) Clone the repo to a working directory
2) CocoaPods is used to manage dependencies. Pods are setup easily and are distributed via a ruby gem. Follow the simple instructions on the website to setup. After setup, run the following command from the toplevel directory of AlamofireOauth to download the dependencies for AlamofireOauth:
pod install
3) Open the AlamofireOauth.xcworkspace
in Xcode and.
4) Create your own clientID and clientSecret at https://developer.wordpress.com/docs/oauth2/
5) set the clientID and clientSecret in the wordpressOauth2Settings object in the ViewController
and you are ready to go!
Below is the sample code for a simple call to the WorPress API while authenticating using OAuth2
class ViewController: UIViewController {
@IBOutlet weak var result: UITextView!
@IBAction func startWordpressOauth2Test(sender: AnyObject) {
self.result.text = ""
UsingOauth2(wordpressOauth2Settings, self, { token in
WordPressRequestConvertible.OAuthToken = token
Alamofire.request(WordPressRequestConvertible.Me())
.responseJSON { (request, response, json, error ) -> Void in
self.result.text = "\(json)"
println("JSON = \(json)")
}
}, {
println("Oauth2 failed")
})
}
}
// Create your own clientID and clientSecret at https://developer.wordpress.com/docs/oauth2/
let wordpressOauth2Settings = Oauth2Settings(
baseURL: "https://public-api.wordpress.com/rest/v1",
authorizeURL: "https://public-api.wordpress.com/oauth2/authorize",
tokenURL: "https://public-api.wordpress.com/oauth2/token",
redirectURL: "alamofireoauth2://wordpress/oauth_callback",
clientID: "????????????",
clientSecret: "????????????"
)
// Minimal Alamofire implementation. For more info see https://github.com/Alamofire/Alamofire#crud--authorization
public enum WordPressRequestConvertible: URLRequestConvertible {
static var baseURLString: String? = wordpressOauth2Settings.baseURL
static var OAuthToken: String?
case Me()
public var URLRequest: NSURLRequest {
let URL = NSURL(string: WordPressRequestConvertible.baseURLString!)!
let mutableURLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent("/me"))
mutableURLRequest.HTTPMethod = "GET"
if let token = WordPressRequestConvertible.OAuthToken {
mutableURLRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}
return mutableURLRequest
}
}
AlamofireOauth2 is available under the MIT 3 license. See the LICENSE file for more info.
Also see my other open source iOS libraries: