OpenTriviaDatabase
provides a collection of common controls for use with Trivia Questions from the Open Trivia Database.
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:
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.
Swift Package Manager (Xcode 11 and above)
- In Xcode, select the File > Add Package Dependency… menu item.
- Paste
https://github.com/Appracatappra/OpenTriviaDatabase.git
in the dialog box. - 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.
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.
There is one embedded logo in OpenTriviaDatabase
:
OTDLogo.png
- The official Open Trivia Database logo.
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")
The Package includes full DocC Documentation for all of its features.