Swift-SMART is a full client implementation of the 🔥FHIR specification for building apps that interact with healthcare data through SMART on FHIR. Written in Swift 5.0 it is compatible with iOS 11 and macOS 10.13 and newer and requires Xcode 10.2 or newer.
Due to the complications of combining two volatile technologies, here's an overview of which version numbers use which Swift and FHIR versions.
- The
masterbranch should always compile and is on (point releases of) these main versions. - The
developbranch should be on versions corresponding to the latest freezes and may be updated from time to time with the latest and greatest CI build.
See tags/releases.
| Version | Swift | FHIR | |
|---|---|---|---|
| 4.2 | 5.0 Package | 4.0.0-a53ec6ee1b |
R4 |
| 4.1 | 5.0 | 4.0.0-a53ec6ee1b |
R4 |
| 4.0 | 4.2 | 4.0.0-a53ec6ee1b |
R4 |
| 3.2 | 3.2 | 3.0.0.11832 |
STU 3 |
| 3.0 | 3.0 | 3.0.0.11832 |
STU 3 |
| 2.9 | 3.0 | 1.6.0.9663 |
STU 3 Ballot, Sep 2016 |
| 2.8 | 3.0 | 1.0.2.7202 |
DSTU 2 (+ technical errata) |
| 2.4 | 2.2 | 1.6.0.9663 |
STU 3 Ballot, Sep 2016 |
| 2.3 | 2.3 | 1.0.2.7202 |
DSTU 2 (+ technical errata) |
| 2.2.3 | 2.2 | 1.0.2.7202 |
DSTU 2 (+ technical errata) |
| 2.2 | 2.0-2.2 | 1.0.2.7202 |
DSTU 2 (+ technical errata) |
| 2.1 | 2.0-2.2 | 1.0.1.7108 |
DSTU 2 |
| 2.0 | 2.0-2.2 | 0.5.0.5149 |
DSTU 2 Ballot, May 2015 |
| 1.0 | 1.2 | 0.5.0.5149 |
DSTU 2 Ballot, May 2015 |
| 0.2 | 1.1 | 0.5.0.5149 |
DSTU 2 Ballot, May 2015 |
| 0.1 | 1.0 | 0.0.81.2382 |
DSTU 1 |
- Programming Guide with code examples
- Technical Documentation of classes, properties and methods
- Medication List sample app
- SMART on FHIR API documentation
See the programming guide for more code examples and details.
The following is the minimal setup working against our reference implementation.
It is assuming that you don't have a client_id and on first authentication will register the client with our server, then proceed to retrieve a token.
If you know your client-id you can specify it in the settings dict.
The app must also register the redirect URL scheme so it can be notified when authentication completes.
import SMART
// create the client
let smart = Client(
baseURL: URL(string: "https://fhir-api-dstu2.smarthealthit.org")!,
settings: [
//"client_id": "my_mobile_app", // if you have one
"redirect": "smartapp://callback", // must be registered
]
)
Task {
do {
guard let patient = try await smart.authorize() else {
print("Authorization cancelled")
return
}
let response = try await smart.getJSON(at: "MedicationRequest?patient=\(patient.id?.value?.string ?? "")")
print("FHIR response status: \(response.status)")
} catch {
print("Authorization or fetch failed: \(error)")
}
}
For authorization to work with Safari/SFViewController, you also need to:
1. register the scheme (such as `smartapp` in the example here) in your app's `Info.plist` and
2. intercept the callback in your app delegate, like so:
```swift
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ app: UIApplication, open url: URL,
options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
// "smart" is your SMART `Client` instance
if smart.awaitingAuthCallback {
return smart.didRedirect(to: url)
}
return false
}
}Swift-SMART now embraces Swift's structured concurrency model. When upgrading from earlier releases:
- Call
try await client.authorize()instead of supplying a callback. The former callback API still exists but is deprecated and simply wraps the new async entry point in aTask. - Inject an
AuthUIHandlerwhen you need custom UI presentation. Platform defaults (iOSAuthUIHandler,macOSAuthUIHandler) are provided, and tests can depend onMockAuthUIHandlerfromTests/Mocks. - Interact with
Authvia async helpers such asawait auth.handleRedirect(_:)andawait auth.reset(); their blocking counterparts remain only as deprecated shims for legacy integrations. - Ensure any custom interceptors or helpers use
@Sendableclosures when capturing state across tasks to satisfy Swift 6 strict concurrency checks.
These changes eliminate data races by isolating mutable OAuth state inside the new AuthCore actor, and make it explicit where UI code must hop to the MainActor.
The suggested approach is to add Swift-SMART as a git submodule to your project. Find detailed instructions on how this is done on the Installation page.
The framework can also be installed via Carthage and is also available via CocoaPods under the name “SMART”.
This work is Apache 2 licensed: NOTICE.txt. FHIR® is the registered trademark of HL7 and is used with the permission of HL7.
- To verify against your test server using a non-interactive flow (client_credentials), run the live E2E test with environment variables:
SMART_BASE_URL="https://your.fhir.server/baseR5" \
SMART_CLIENT_ID="your-client-id" \
SMART_CLIENT_SECRET="your-client-secret" \
SMART_SCOPE="system/*.rs" \
SMART_TEST_QUERY_PATH="Patient?_count=1" \
swift test -c debug- To run the manual OAuth checklist (authorization_code), set:
- To exercise the full Authorization Code + PKCE flow (standalone launch), export sandbox credentials and run the harness:
```bash
cp scripts/test_scripts/standalone_launch.env.example scripts/test_scripts/standalone_launch.env
# Edit values in standalone_launch.env, then invoke:
scripts/test_scripts/standalone_launch.shSet SMART_AUTOMATION_ENDPOINT if you have an automation worker (e.g., Playwright) listening for authorize URLs and driving the login form. Otherwise the script opens the system browser for manual interaction.
- To validate EHR/context-aware launches end-to-end, configure the new harness:
cp scripts/test_scripts/ehr_launch/ehr_launch.env.example scripts/test_scripts/ehr_launch/ehr_launch.env
# Populate SMART_* and SMART_EHR_* values, then run:
scripts/test_scripts/ehr_launch/ehr_launch.shThe script first signs in as the EMR practitioner, posts the launch context to Keycloak (using Tests/Fixtures/launch-context.json as a template for all optional fields), and finally runs the EHRLaunchTests suite to perform the SMART launch and patient read.
- To run the manual OAuth checklist (authorization_code), set:
RUN_MANUAL_OAUTH=1 \
SMART_LAUNCHER_URL="https://launch.smarthealthit.org" \
swift test -c debug
# After completing the documented steps, re-run with:
SMART_MANUAL_AUTH_CONFIRMED=1 RUN_MANUAL_OAUTH=1 swift test -c debug- Network logging (Debug builds): inject a logging interceptor when constructing the
Server, e.g.
let server = Server(
baseURL: URL(string: "https://your.fhir.server/baseR5")!,
additionalInterceptors: [LoggingInterceptor(log: .body)]
)- OAuth2 internal logging: assign an
OAuth2Logger, such asOSLogOAuth2Logger, after creating the server:
server.logger = OSLogOAuth2Logger()