|
3 | 3 | - [SQLite.swift Documentation](#sqliteswift-documentation) |
4 | 4 | - [Installation](#installation) |
5 | 5 | - [Swift Package Manager](#swift-package-manager) |
| 6 | + - [Using SQLite.swift with SQLCipher](#using-sqliteswift-with-sqlcipher) |
6 | 7 | - [Carthage](#carthage) |
7 | 8 | - [CocoaPods](#cocoapods) |
8 | 9 | - [Requiring a specific version of SQLite](#requiring-a-specific-version-of-sqlite) |
9 | | - - [Using SQLite.swift with SQLCipher](#using-sqliteswift-with-sqlcipher) |
10 | 10 | - [Manual](#manual) |
11 | 11 | - [Getting Started](#getting-started) |
12 | 12 | - [Connecting to a Database](#connecting-to-a-database) |
@@ -119,7 +119,79 @@ process of downloading, compiling, and linking dependencies. |
119 | 119 | $ swift build |
120 | 120 | ``` |
121 | 121 |
|
| 122 | +#### Using SQLite.swift with SQLCipher |
| 123 | + |
| 124 | +If you want to use [SQLCipher][] with SQLite.swift you can specify the `SQLCipher` trait when consuming SQLite.swift. |
| 125 | + |
| 126 | +```swift |
| 127 | +depdencies: [ |
| 128 | + .package(url: "https://github.com/stephencelis/SQLite.swift.git", from: "0.15.4", traits: ["SQLCipher"]) |
| 129 | +] |
| 130 | +``` |
| 131 | + |
| 132 | +As of Xcode 26.2 (17C52), there's no direct way in the Xcode UI to select trait variations so you'll need to use a local wrapper package to pull in the SQLite.swift dependency with the `SQLCipher` trait enabled: |
| 133 | + |
| 134 | +```swift |
| 135 | +// swift-tools-version: 6.1 |
| 136 | +// The swift-tools-version declares the minimum version of Swift required to build this package. |
| 137 | + |
| 138 | +import PackageDescription |
| 139 | + |
| 140 | +let package = Package( |
| 141 | + name: "AppDependencies", |
| 142 | + platforms: [ |
| 143 | + .macOS(.v10_14), |
| 144 | + .iOS(.v13), |
| 145 | + .macCatalyst(.v13), |
| 146 | + .watchOS(.v8), |
| 147 | + .tvOS(.v15), |
| 148 | + .visionOS(.v1) |
| 149 | + ], |
| 150 | + products: [ |
| 151 | + .library( |
| 152 | + name: "AppDependencies", |
| 153 | + targets: ["AppDependencies"]), |
| 154 | + ], |
| 155 | + dependencies: [ |
| 156 | + .package( |
| 157 | + url: "https://github.com/stephencelis/SQLite.swift.git", |
| 158 | + from: "0.15.4", |
| 159 | + traits: ["SQLCipher"]) |
| 160 | + ], |
| 161 | + targets: [ |
| 162 | + .target( |
| 163 | + name: "AppDependencies", |
| 164 | + dependencies: [ |
| 165 | + .product( |
| 166 | + name: "SQLite", |
| 167 | + package: "SQLite.swift") |
| 168 | + ] |
| 169 | + ) |
| 170 | + ] |
| 171 | +) |
| 172 | +``` |
| 173 | + |
| 174 | +Within Xcode add your local `AppDependencies` wrapper package as a package dependency and SQLite.swift with SQLCipher functionality will be accessible. |
| 175 | + |
| 176 | +Using the `SQLCipher` trait will cause SQLite.swift to include a dependency on SQLCipher.swift and enable `Connection` methods to set and change the database key: |
| 177 | + |
| 178 | +```swift |
| 179 | +import SQLite |
| 180 | + |
| 181 | +let db = try Connection("path/to/encrypted.sqlite3") |
| 182 | +try db.key("secret") |
| 183 | +try db.rekey("new secret") // changes encryption key on already encrypted db |
| 184 | +``` |
| 185 | + |
| 186 | +To encrypt an existing database: |
| 187 | + |
| 188 | +```swift |
| 189 | +let db = try Connection("path/to/unencrypted.sqlite3") |
| 190 | +try db.sqlcipher_export(.uri("encrypted.sqlite3"), key: "secret") |
| 191 | +``` |
| 192 | + |
122 | 193 | [Swift Package Manager]: https://swift.org/package-manager |
| 194 | +[SQLCipher]: https://www.zetetic.net/sqlcipher/ |
123 | 195 |
|
124 | 196 | ### Carthage |
125 | 197 |
|
|
191 | 263 |
|
192 | 264 | See the [sqlite3 podspec][sqlite3pod] for more details. |
193 | 265 |
|
194 | | -#### Using SQLite.swift with SQLCipher |
195 | | - |
196 | | -If you want to use [SQLCipher][] with SQLite.swift you can require the |
197 | | -`SQLCipher` subspec in your Podfile (SPM is not supported yet, see [#1084](https://github.com/stephencelis/SQLite.swift/issues/1084)): |
198 | | - |
199 | | -```ruby |
200 | | -target 'YourAppTargetName' do |
201 | | - # Make sure you only require the subspec, otherwise you app might link against |
202 | | - # the system SQLite, which means the SQLCipher-specific methods won't work. |
203 | | - pod 'SQLite.swift/SQLCipher', '~> 0.15.4' |
204 | | -end |
205 | | -``` |
206 | | - |
207 | | -This will automatically add a dependency to the SQLCipher pod as well as |
208 | | -extend `Connection` with methods to change the database key: |
209 | | - |
210 | | -```swift |
211 | | -import SQLite |
212 | | -
|
213 | | -let db = try Connection("path/to/encrypted.sqlite3") |
214 | | -try db.key("secret") |
215 | | -try db.rekey("new secret") // changes encryption key on already encrypted db |
216 | | -``` |
217 | | - |
218 | | -To encrypt an existing database: |
219 | | - |
220 | | -```swift |
221 | | -let db = try Connection("path/to/unencrypted.sqlite3") |
222 | | -try db.sqlcipher_export(.uri("encrypted.sqlite3"), key: "secret") |
223 | | -``` |
224 | | - |
225 | 266 | [CocoaPods]: https://cocoapods.org |
226 | 267 | [CocoaPods Installation]: https://guides.cocoapods.org/using/getting-started.html#getting-started |
227 | 268 | [sqlite3pod]: https://github.com/clemensg/sqlite3pod |
228 | | -[SQLCipher]: https://www.zetetic.net/sqlcipher/ |
229 | 269 |
|
230 | 270 | ### Manual |
231 | 271 |
|
|
0 commit comments