Swift / iOS
Follow these steps to add libgraft.swift to your Xcode project using the Swift Package Manager:
-
Open your Xcode project.
-
Go to
File
>Add Package Dependencies...
-
Enter the repository URL for
libgraft.swift
in the search bar (top right):https://github.com/orbitinghail/libgraft.swift.git -
Select the version you want to use (probably the latest).
-
Click “Add Package”.
-
Add the libgraft “Package Product” to your application target on the next screen.
-
Import and initialize the library during application startup:
// first, you need to import libgraftimport libgraft// then add this to your application startup process// this example assumes you are using GRDB for SQLitelet fm = FileManager.defaultlet supportDir = try fm.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)let configPath = supportDir.appendingPathComponent("graft-config.toml")let dataPath = supportDir.appendingPathComponent("data")// write out a libgraft config file and set the GRAFT_CONFIG env variablelet config = """data_dir = "\(dataPath.path)"autosync = falsemake_default = true"""try config.write(to: configPath, atomically: true, encoding: .utf8)setenv("GRAFT_CONFIG", configPath.path, 1)// Initialize graft with an in-memory SQLite database managed by GRDBlet tempDB = try DatabaseQueue()_ = tempDB.inDatabase { db in libgraft.graft_static_init(db.sqliteConnection) }// Open a Graft backed database with a random Volume ID through GRDBlet db = try DatabaseQueue(path: "random")
The libgraft.swift Package contains a statically compiled version of the libgraft SQLite extension. When you add it to your Swift project, Swift will statically link libgraft during compilation. This will fail if libgraft is unable to find SQLite symbols at compile time.
The system version of SQLite included on iOS should be sufficient for Graft to work. So pull in SQLite using any mechanism you prefer.
The example in the previous section uses GRDB which is a nice wrapper around SQLite. But it’s not required.