This SwiftUI package makes using the Unsplash API in an app simple and easy.
Call the view and apply modifiers to it like you would with any other view. The package will fetch the image metadata from the API and it will load the remote image asynchronously. The view also has a text in the bottom left corner crediting the photographer as well as hotlinking to the image on Unsplash (A requirement while using the API).
The package is only compatible with iOS 15 and iPadOS 15 for now. Previous releases of this package support older OS versions however this release uses the new concurrency features as well as the new AsyncImage View
The package requires a Access Key (Client ID) for the Unsplash API. You can get one from the Unsplash Developers page. You'll need to register your app there and you will be Rate Limited (limited number of API requests per hour) until you apply for a high-volume application (refer to the Unsplash Developers page for more details)
Disclamer: I have not attempted applying for a high-volume application using this library as of yet. I believe that I have met all the requirements of the API and you should have no problem with it however I am not completely sure.
In Xcode go to File -> Add Packages
and paste in the repo's url: https://github.com/ArnavMotwani/UnsplashSwiftUI.git
then either select a version or the main branch (I will update the main branch more frequently with minor changes, while the version number will only increase with significant changes)
Import the package into the file with import UnsplashSwiftUI
then call the UnsplashRandom
view wherever you want.
import SwiftUI
import UnsplashSwiftUI
struct UnsplashRandomTest: View {
var body: some View {
UnsplashRandom(clientId: "YOUR_ACCESS_KEY")
}
}
struct UnsplashRandomTest_Previews: PreviewProvider {
static var previews: some View {
UnsplashRandomTest()
}
}
import SwiftUI
import UnsplashSwiftUI
struct UnsplashRandomTest: View {
var body: some View {
UnsplashRandom(clientId: "YOUR_ACCESS_KEY")
.padding(25)
}
}
struct UnsplashRandomTest_Previews: PreviewProvider {
static var previews: some View {
UnsplashRandomTest()
}
}
import SwiftUI
import UnsplashSwiftUI
struct UnsplashRandomTest: View {
var body: some View {
UnsplashRandom(clientId: "YOUR_ACCESS_KEY")
.clipShape(RoundedRectangle(cornerRadius: 10))
}
}
struct UnsplashRandomTest_Previews: PreviewProvider {
static var previews: some View {
UnsplashRandomTest()
}
}
Parameter | Optional? | Type | Description | Default |
---|---|---|---|---|
clientId | No | String | Client ID from the Unsplash Developer page | - |
orientation | Yes | Orientations | Filter by photo orientation. (Valid values: .landscape, .portrait, .squarish, .none) | - |
query | Yes | String | Limit selection to photos matching a search term. | - |
textColor | Yes | Color | Color of the text hotlinked to image on Unsplash | Color.white |
textBackgroundColor | Yes | Color | Color of text background (opacity set to 0.2 automatically) | Color.black |
aspectRatio | Yes | ContentMode | Choose whether the image fits or fills the container | ContentMode.fit |
UnsplashRandom(clientId: "YOUR_ACCESS_KEY", orientation: .landscape)
UnsplashRandom(clientId: "YOUR_ACCESS_KEY", orientation: .portrait)
UnsplashRandom(clientId: "YOUR_ACCESS_KEY", orientation: .squarish)
UnsplashRandom(clientId: "YOUR_ACCESS_KEY", query: "trees")
UnsplashRandom(clientId: "YOUR_ACCESS_KEY", query: "landscapes")
UnsplashRandom(clientId: "YOUR_ACCESS_KEY", query: "space")
UnsplashRandom(clientId: "YOUR_ACCESS_KEY", textColor: .black)
UnsplashRandom(clientId: "YOUR_ACCESS_KEY", textColor: .blue)
UnsplashRandom(clientId: "YOUR_ACCESS_KEY", textColor: .primary)
UnsplashRandom(clientId: "YOUR_ACCESS_KEY", textBackgroundColor: .white)
UnsplashRandom(clientId: "YOUR_ACCESS_KEY", textBackgroundColor: .blue)
UnsplashRandom(clientId: "YOUR_ACCESS_KEY", textBackgroundColor: .primary)
UnsplashRandom(clientId: "YOUR_ACCESS_KEY", aspectRatio: .fit)
UnsplashRandom(clientId: "YOUR_ACCESS_KEY", aspectRatio: .fill)