OpenTriviaDatabase

1.0.0

OpenTriviaDatabase provides a collection of common controls for use with Trivia Questions from the Open Trivia Database
Appracatappra/OpenTriviaDatabase

What's New

1.0.0

2025-05-19T13:22:10Z

Initial Release

OpenTriviaDatabase

OpenTriviaDatabase provides a collection of common controls for use with Trivia Questions from the Open Trivia Database.

Support

If you find OpenTriviaDatabase useful and would like to help support its continued development and maintenance, please consider making a small donation, especially if you are using it in a commercial product:

Buy Me A Coffee

It's through the support of contributors like yourself, I can continue to build, release and maintain high-quality, well documented Swift Packages like SwiftUIKit for free.

Installation

Swift Package Manager (Xcode 11 and above)

  1. In Xcode, select the File > Add Package Dependency… menu item.
  2. Paste https://github.com/Appracatappra/OpenTriviaDatabase.git in the dialog box.
  3. Follow the Xcode's instruction to complete the installation.

Why not CocoaPods, or Carthage, or etc?

Supporting multiple dependency managers makes maintaining a library exponentially more complicated and time consuming.

Since, the Swift Package Manager is integrated with Xcode 11 (and greater), it's the easiest choice to support going further.

Overview

OpenTriviaDatabase provides a simply way to retrieve Trivia Questions from the Open Trivia Database. The following code will get the counts of all questions in the database:

import OpenTriviaDatabase
...

// Attempt to get counts
TriviaDatabaseCounts.sharedCounts = await TriviaDatabaseCounts.getCounts()
    
let count = TriviaDatabaseCounts.sharedCounts.overall.totalNumOfQuestions

This code will get the number of questions in the Japanese Animé and Manga category:

import OpenTriviaDatabase
...

// Attempt to get counts
TriviaDatabaseCounts.sharedCounts = await TriviaDatabaseCounts.getCounts()
    
let counts = TriviaDatabaseCounts.sharedCounts.getCounts(for: .entertainmentJapaneseAnimeAndManga)
let count = counts.totalNumOfVerifiedQuestions

The Shared Token keeps Open Trivia Database from returning the same question twice during a session. The following code will request a new token:

import OpenTriviaDatabase
...
// Attempt to get the session token.
TriviaSessionToken.sharedSessionToken = await TriviaSessionToken.getNewToken()

If you load this into the sharedSessionToken global property, all other calls to the Open Trivia Database will use this token:

import OpenTriviaDatabase
...
// Attempt to get the session token.
TriviaSessionToken.sharedSessionToken = await TriviaSessionToken.getNewToken()

// Get a collection of 10 questions.
let questions = await TrivaQuestionCollection.getQuestions(for: .entertainmentJapaneseAnimeAndManga)

Use the getQuestions static method of the TriviaQuestionCollection class to request questions from the Open Trivia Database.

Embedded Graphic

There is one embedded logo in OpenTriviaDatabase:

  • OTDLogo.png - The official Open Trivia Database logo.

The OpenTriviaDatabase Class

The OpenTriviaDatabase provides a few helper utilities that allow you to easily access resources stored in the Swift Package (such as the image above).

For example, the following code would return the path to the OTDLogo.png file:

let path = OpenTriviaDatabase.pathTo(resource:"OTDLogo.png")

Documentation

The Package includes full DocC Documentation for all of its features.

Description

  • Swift Tools 6.1.0
View More Packages from this Author

Dependencies

Last updated: Sun Jun 08 2025 18:53:07 GMT-0900 (Hawaii-Aleutian Daylight Time)