Unsplash API client written in Swift.
Unsplash offers 2 APIs:
- Source API (unlimited requests)
- Official API
- JSON API (5000 requests / hour)
JSON API is in progress and will be available soon in this library.
- iOS 8.0+ / Mac OS X 10.9+ / tvOS 9.0+ / watchOS 2.0+
- Xcode 8.0+
You can use CocoaPods to integrate the library with your project.
pod "UnsplashKit/API" # Official API
pod "UnsplashKit/Source" # Source API
import UnsplashKit
First thing you need is an instance of the client for executing requests and parsing responses from the API:
var token = "xxxx"
var client = UnsplashClient { request -> [String: String] in
return [ "Authorization": "Bearer \(token)"]
}
Since the client supports providing headers for the requests, we'll use that closure to specify the authenticationt tokent that we want to pass in. UnsplashKit doesn't provide the OAuth2 authentication components, but you can use Paparajote instead.
Once the client is created you can use models' resources that contain the information about the request and about how to parse the JSON response into models whose properties you can access directly to. Here's an example:
let searchPhotos = Search.photos(query: "mountain")
client.execute(resource: searchPhotos) { (result) in
print(result)
}
The models that contain resources are,
User
,Photo
,Search
,Collection
. These resources match the endpoints available from the API. If there's any new endpoint which is not supported, you can either create an issue, open a PR or contact hello@caramba.io
Source API allows you to get an Unsplash image in different ways.
All the calls return the image through a completion block that returns a Result<Image, Error>
.
call() { result in
switch result {
case .success(let image): //handle image
case .failure(let error): //handle error
}
}
You can also ignore the error and get only the result using
call() { result in
let image = result.value
}
UnsplashSource().randomPhoto() { result in
// handle the result
}
UnsplashSource().randomPhoto(fromCategory: .nature) { result in
// handle the result
}
Unsplash offers a list of predefined categories. You can ask for a photo from any of these categories.
.buildings
.food
.nature
.people
.technology
.objects
UnsplashSource().randomPhoto(fromUser: "carambalabs") { result in
// handle the result
}
UnsplashSource().randomPhoto(fromUserLikes: "mkwlsn") { result in
// handle the image
}
UnsplashSource().randomPhoto(fromCollection: "176316") { result in
// handle the result
}
UnsplashSource().randomPhoto(fromSearch: ["nature", "water"]) { result in
// handle the result
}
UnsplashSource().photo("WLUHO9A_xik") { result in
// handle the result
}
If you want to get an image of a specific size you can use the optional size
parameter in any call.
UnsplashSource().randomPhoto(fromCategory: .nature, size: CGSize(width: 600, height: 200)) { result in
// handle the result
}
The calls random
, search
, category
and user
can also be limited to only updating once per day or week. To do so, simply use the optional filter
parameter.
UnsplashSource().randomPhoto(filter: .daily) { result in
// handle the result
}
This project is funded and maintained by Caramba. We 💛 open source software!
Check out our other open source projects, read our blog or say 👋 on twitter @carambalabs.
Contributions are welcome 🤘 We encourage developers like you to help us improve the projects we've shared with the community. Please see the Contributing Guide and the Code of Conduct.
UnsplashKit is available under the MIT license. See the LICENSE file for more info.