/UsingCinSwift

Demo Project Using C in your app, Using C in your framework, Using that framework in your host app

Primary LanguageSwift

UsingCinSwift

xcode swift license

A sample project demonstrating how to integrate C libraries (SQLite3 and MySQL) into Swift iOS applications using module maps and bridging headers.

Overview

This project showcases two different approaches for using C libraries in Swift:

  1. Module Maps - Used in the framework target to expose C APIs to Swift
  2. Bridging Headers - Used in the app target for direct C library integration

Project Structure

The project contains three targets:

1. test_sqlite_ios_framework

An iOS framework that wraps C libraries using module maps.

  • TestSqlite_iOS.swift - Swift wrapper class with methods to test SQLite and MySQL
  • sqlite3/module.modulemap - Module map exposing C headers to Swift
  • sqlite3/sqlite.h - Wrapper header for SQLite3
  • sqlite3/mysql.h - Wrapper header for MySQL client library

2. test_sqlite_ios_app

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.h to import SQLite3

3. test_sqlite_ios_host

An iOS app that uses the test_sqlite_ios_framework.

  • Links against and embeds the framework
  • Demonstrates framework-based C library integration

Features

  • 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

Requirements

  • Xcode 11.4+
  • Swift 5.2+
  • iOS 10.0+

Setup

  1. Clone the repository
  2. Open UsingCinSwift.xcodeproj in Xcode
  3. Select a target and build:
    • test_sqlite_ios_framework - Build the framework
    • test_sqlite_ios_app - Run the standalone app
    • test_sqlite_ios_host - Run the app using the framework

Usage Example

Using the Framework

import test_sqlite_ios_framework

let sqliteTest = TestSqlite_iOS()
sqliteTest.testSqlite()

Direct SQLite Usage (via Bridging Header)

// 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)
}

Module Map Configuration

The framework uses a module map to expose C headers:

module test_sqlite_ios_framework {
    header "sqlite.h"
    header "mysql.h"
    export *
}

Notes

  • 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

Versions

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