Book

Exploring Freelancing

Navigate freelancing as a developer; find clients, manage contracts, ensure timely payment, and learn from experiences!

If you want to display quotes in your app, then you can use QuoteKit. It is a Swift package that uses the free APIs provided by Quotable created by Luke Peavey.

This package powers Quoting.

You can use the async/await methods to access:

  • Random quote
  • Quotes
  • Authors
  • Tags
  • Searching quotes and authors.

As the methods uses the new structure concurrency, it supports:

  • iOS 13.0+
  • macOS 10.5+
  • watchOS 6.0+
  • tvOS 13.0+

You’ll find the type explicitly mentioned for every example so you know what structure you’re getting back from the corresponding methods.

Installation

Add QuoteKit to your project via the Swift Package Manager:

dependencies: [
    .package(url: "https://github.com/rudrankriyam/QuoteKit.git")
]

Structure

QuoteKit contains the structure QuoteKit that contains static methods to fetc revelant data. For example, to get the list of quotes:

do {
    let quotes: Quotes = try await QuoteKit.quotes()
} catch {
    print(error)
}

Random Quote

To get a single random Quote structure:

let randomQuote: Quote = try await QuoteKit.randomQuote()

You can customize the request by adding query parameters like the minimum and maximum length of the quote or its tag. You can also get a random quote from a specific author(s).

Few examples

Get a random quote with tags “History” OR “Civil Rights”:

try await QuoteKit.randomQuote(tags: [.history, .civilRights], type: .either)

Get a random quote with a maximum length of 50 characters:

try await QuoteKit.randomQuote(maxLength: 150)

Get a random quote by the author “Aesop” and “Stephen Hawking”:

try await QuoteKit.randomQuote(authors: ["aesop", "stephen-hawking"])

List Quotes

To list the Quotes structure. It contains an array of Quote, the number of pages, the current page, the number of pages, etc.

By default, the list contains 20 Quote on one page.

let quotes: Quotes = try await QuoteKit.quotes()

Few examples

Get all quotes with a maximum length of 50 characters:

try await QuoteKit.quotes(maxLength: 150)

Get the second page of quotes, with 20 results per page, with a limit of 10 quotes:

try await QuoteKit.quotes(limit: 10, page: 2)

Get all quotes with the tags technology AND famous quotes:

try await QuoteKit.quotes(tags: [.technology, .famousQuotes], type: .all)

Get all quotes by author, using the author’s slug:

try await QuoteKit.quotes(authors: ["albert-einstein"])

Quote by ID

If you know the ID of the quote, then you can fetch the corresponding Quote like:

let quote: Quote = try await QuoteKit.quote(id: "2xpHvSOQMD")

List Authors

You can get the Authors structure that by default, contains 20 Author on one page. You can filter multiple authors by providing their slugs in the query parameter.

let authors: Authors = try await QuoteKit.authors()

Few examples

Get the first page of authors, with 20 results per page:

try await QuoteKit.authors(page: 1)

Get multiple authors by slug:

try await QuoteKit.authors(slugs: ["albert-einstein", "abraham-lincoln"])

Author by ID

If you know the ID of the author, then you can fetch the corresponding Author . This is helpful in cases where you’re showing a list of authors and wants to fetch the details of a particular author to navigate to the detail view:

let author: Author = try await QuoteKit.author(id: "XYxYtSeixS-o")

Author Profile Image URL

The Author structure doesn’t contain the image URL and you’ll have to send another request to get it for the given author slug.

You can specify the image size as well. The default image size is 700x700.

let authorImageURL: URL = QuoteKit.authorProfile(size: 1000, slug: "aesop")

List Tags

The quotes are categorized into different Tags structures. You can fetch the list of tags like:

let tags: Tags= try await QuoteKit.tags()

Few examples

Get all tags sorted alphabetically by name:

try await QuoteKit.tags(sortBy: .name)

Get all tags sorted by number of quotes in descending order:

try await QuoteKit.tags(sortBy: .quoteCount, order: .descending)

Search Quotes

You can also search for a particular keyword and get the Quotes structure that contains the word. By default, the list contains 20 Quote on one page.

let quotes: Quotes = try await QuoteKit.searchQuotes(for: "love")

Get the second page of searched quotes with 20 results per page with a limit of 10 quotes -

try await QuoteKit.searchQuotes(for: "love", limit: 10, page: 2)

Search Authors

You can also search for a particular keyword and get the Authors structure that contains the word. By default, the list contains 20 Author on one page.

var authors: Authors = try await QuoteKit.searchAuthors(for: "kalam")

Get the third page of searched authors, with 20 results per page, with a limit of 10 authors -

try await QuoteKit.searchAuthors(for: "kalam", limit: 10, page: 3)

Conclusion

Adding quotes to your app has been never been easier. You can show them to inspire or motivate your users.

Explore the codebase, and if you’ve any feedback or want to contribute, please open an issue or pull request, respectively.

Tag @rudrankriyam on Twitter if you want to share your experience of using it!

Thanks for reading, and I hope you’re enjoying it!

Book

Exploring Freelancing

Navigate freelancing as a developer; find clients, manage contracts, ensure timely payment, and learn from experiences!