A sample project demonstrating how to integrate C libraries (SQLite3 and MySQL) into Swift iOS applications using module maps and bridging headers.
This project showcases two different approaches for using C libraries in Swift:
- Module Maps - Used in the framework target to expose C APIs to Swift
- Bridging Headers - Used in the app target for direct C library integration
The project contains three targets:
An iOS framework that wraps C libraries using module maps.
TestSqlite_iOS.swift- Swift wrapper class with methods to test SQLite and MySQLsqlite3/module.modulemap- Module map exposing C headers to Swiftsqlite3/sqlite.h- Wrapper header for SQLite3sqlite3/mysql.h- Wrapper header for MySQL client library
A standalone iOS app that uses SQLite3 directly via bridging header.
- Demonstrates direct C library usage without a framework wrapper
- Uses
test_sqlite_ios_app-Bridging-Header.hto import SQLite3
An iOS app that uses the test_sqlite_ios_framework.
- Links against and embeds the framework
- Demonstrates framework-based C library integration
- SQLite3 database operations (open, close, error handling)
- MySQL header integration
- Module map configuration for C-to-Swift bridging
- Example of both framework and direct app integration approaches
- Xcode 11.4+
- Swift 5.2+
- iOS 10.0+
- Clone the repository
- Open
UsingCinSwift.xcodeprojin Xcode - Select a target and build:
test_sqlite_ios_framework- Build the frameworktest_sqlite_ios_app- Run the standalone apptest_sqlite_ios_host- Run the app using the framework
import test_sqlite_ios_framework
let sqliteTest = TestSqlite_iOS()
sqliteTest.testSqlite()// SQLite3 functions are available directly
var db: OpaquePointer?
let result = sqlite3_open(":memory:", &db)
if result == SQLITE_OK {
print("Database opened successfully")
sqlite3_close(db)
}The framework uses a module map to expose C headers:
module test_sqlite_ios_framework {
header "sqlite.h"
header "mysql.h"
export *
}
- This is a proof-of-concept project for C-Swift interoperability
- The MySQL integration requires MySQL client libraries to be installed
- SQLite3 is included in iOS SDK by default
Version 11.4.1 (11E503a)
Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)
Target: x86_64-apple-darwin19.4.0