SwiftHTTP
SwiftHTTP is a thin wrapper around NSURLSession in Swift to simplify HTTP requests.
Features
- Convenient Closure APIs
- Simple Queue Support
- Parameter Encoding
- Builtin JSON Request Serialization
- Upload/Download with Progress Closure
- Concise Codebase.
First thing is to import the framework. See the Installation instructions on how to add the framework to your project.
Examples
GET
The most basic request. By default an Data object will be returned for the response.
We can also add parameters as with standard container objects and they will be properly serialized to their respective HTTP equivalent.
The Response
contains all the common HTTP response data, such as the responseObject of the data and the headers of the response.
HTTP Methods
All the common HTTP methods are avalaible as convenience methods as well.
POST
PUT
HEAD
DELETE
Download
Upload
File uploads can be done using the Upload
object. All files to upload should be wrapped in a Upload object and added as a parameter.
Upload
comes in both a on disk fileUrl version and a Data version.
Custom Headers
Custom HTTP headers can be add to a request with the standard NSMutableRequest API:
SSL Pinning
SSL Pinning is also supported in SwiftHTTP.
You load either a Data
blob of your certificate or you can use a SecKeyRef
if you have a public key you want to use. The usePublicKeys
bool is whether to use the certificates for validation or the public keys. The public keys will be extracted from the certificates automatically if usePublicKeys
is choosen.
Authentication
SwiftHTTP supports authentication through NSURLCredential. Currently only Basic Auth and Digest Auth have been tested.
Allow all certificates example:
Operation Queue
SwiftHTTP also has a simple queue in it!
Cancel
Let's say you want to cancel the request a little later, call the cancel
method.
JSON Request Serializer
Request parameters can also be serialized to JSON as needed. By default request are serialized using standard HTTP form encoding.
Progress
SwiftHTTP can monitor the progress of a request.
Global handlers
SwiftHTTP also has global handlers, to reduce the requirement of repeat HTTP modifiers, such as a auth header or setting NSMutableURLRequest
properties such as timeoutInterval
.
Client/Server Example
This is a full example swiftHTTP in action. First here is a quick web server in Go.
Now for the request:
POST example
Now for the Swift:
Requirements
SwiftHTTP works with iOS 7/OSX 10.10 or above. It is recommended to use iOS 8/10.10 or above for CocoaPods/framework support.
To use SwiftHTTP with a project targeting iOS 7, you must include all Swift files directly in your project.
Installation
CocoaPods
Check out Get Started tab on cocoapods.org.
To use SwiftHTTP in your project add the following 'Podfile' to your project
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'SwiftHTTP', '~> 3.0.1'
Then run:
pod install
Carthage
Check out the Carthage docs on how to add a install. The SwiftHTTP
framework is already setup with shared schemes.
You can install Carthage with Homebrew using the following command:
To integrate SwiftHTTP into your Xcode project using Carthage, specify it in your Cartfile
:
github "daltoniam/SwiftHTTP" >= 3.0.1
Rogue
First see the installation docs for how to install Rogue.
To install SwiftHTTP run the command below in the directory you created the rogue file.
rogue add https://github.com/daltoniam/SwiftHTTP
Next open the libs
folder and add the SwiftHTTP.xcodeproj
to your Xcode project. Once that is complete, in your "Build Phases" add the SwiftHTTP.framework
to your "Link Binary with Libraries" phase. Make sure to add the libs
folder to your .gitignore
file.
Other
Simply grab the framework (either via git submodule or another package manager).
Add the SwiftHTTP.xcodeproj
to your Xcode project. Once that is complete, in your "Build Phases" add the SwiftHTTP.framework
to your "Link Binary with Libraries" phase.
Add Copy Frameworks Phase
If you are running this in an OSX app or on a physical iOS device you will need to make sure you add the SwiftHTTP.framework
included in your app bundle. To do this, in Xcode, navigate to the target configuration window by clicking on the blue project icon, and selecting the application target under the "Targets" heading in the sidebar. In the tab bar at the top of that window, open the "Build Phases" panel. Expand the "Link Binary with Libraries" group, and add SwiftHTTP.framework
. Click on the + button at the top left of the panel and select "New Copy Files Phase". Rename this new phase to "Copy Frameworks", set the "Destination" to "Frameworks", and add SwiftHTTP.framework
.
TODOs
- [ ] Linux support?
- [ ] Add more unit tests
License
SwiftHTTP is licensed under the Apache v2 License.