Killswitch is a clever control panel built by Mirego that allows mobile developers to apply
runtime version-specific behaviors to their iOS or Android application.
Killswitch is a clever control panel built by Mirego that allows mobile developers to apply runtime version-specific behaviors to their iOS or Android application.
The library is published to Mirego's public Maven repository, so make sure you have it included in your settings.gradle.kts dependencyResolutionManagement
block.
dependencyResolutionManagement {
repositories {
// ...
maven("https://s3.amazonaws.com/mirego-maven/public")
}
}
You can then add the dependency to your common source set dependencies in your build.gradle.kts.
commonMain {
dependencies {
// ...
api("com.mirego.killswitch-mobile:killswitch:x.y.z")
}
}
To make the code available on iOS, don't forget to export the library in the framework.
kotlin {
androidTarget()
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach {
it.binaries {
framework {
// ...
export("com.mirego.killswitch-mobile:killswitch:x.y.z")
}
}
}
}
On iOS, if the Killswitch is engaged from another thread than the main one, you may need to add this line in your project's gradle.properties to prevent your app from crashing.
kotlin.native.binary.objcExportSuspendFunctionLaunchThreadRestriction=none
There is two ways of using the Killswitch: by letting the library display a native dialog or by implementing a custom UI.
With this approach it is easy to display a native dialog on the launch of your app.
In the onCreate()
function of your main Activity
, you can engage the Killswitch in a CoroutineScope
and let the library handle the response in order to display the native dialog.
lifecycleScope.launch {
try {
AndroidKillswitch.showDialog(
AndroidKillswitch.engage(KILLSWITCH_API_KEY, this@MainActivity, KILLSWITCH_URL),
this@MainActivity,
android.R.style.Theme_DeviceDefault_Light_Dialog_Alert
)
} catch (e: KillswitchException) {
Log.e(TAG, "Killswitch exception", e)
}
}
In the application()
function of your AppDelegate
, you can engage the Killswitch in a Task
and let the library handle the response in order to display the native dialog.
Task {
do {
let viewData = try await IOSKillswitch().engage(key: KILLSWITCH_API_KEY, url: KILLSWITCH_URL)
DispatchQueue.main.async {
IOSKillswitch().showDialog(viewData: viewData)
}
} catch {
print("Killswitch error: \(error)")
}
}
With this approach you are free to customize the Killswitch UI and UX the way you want. It could either occupy the whole screen with an opaque background, or be displayed on top of the main view with some alpha.
In the root view of your application, you can engage the Killswitch inside a LaunchedEffect
and decide which view to display depending on the state of the view data.
var viewData by remember { mutableStateOf<KillswitchViewData?>(null) }
when (val localViewData = viewData) {
is KillswitchViewData -> CustomDialog(
viewData = localViewData,
dismiss = { viewData = null },
navigateToUrl = this@MainActivity::navigateToKillswitchUrl
)
else -> MainView()
}
val lifecycleOwner = LocalLifecycleOwner.current
val context = LocalContext.current
LaunchedEffect(true) {
lifecycleOwner.lifecycle.coroutineScope.launch {
try {
viewData = AndroidKillswitch.engage(KILLSWITCH_API_KEY, context, KILLSWITCH_URL)
} catch (e: KillswitchException) {
Log.e(TAG, "Killswitch exception", e)
}
}
}
You can find a sample CustomDialog Compose view here.
In the root view of your application, you can engage the Killswitch inside a task
modifier and decide which view to display depending on the state of the view data.
@State private var viewData: KillswitchViewData? = nil
var body: some View {
ZStack {
if let viewData = viewData {
CustomDialog(viewData: viewData) {
self.viewData = nil
}
} else {
MainView()
}
}
.task {
do {
self.viewData = try await IOSKillswitch().engage(key: KILLSWITCH_API_KEY, url: KILLSWITCH_URL)
} catch {
print("Killswitch error: \(error)")
}
}
}
You can find a sample CustomDialog SwiftUI view here.
Killswitch is © 2013-2023 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md
file.
The shield logo is based on this lovely icon by Kimmi Studio, from The Noun Project. Used under a Creative Commons BY 3.0 license.
Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We’re a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.
We also love open-source software and we try to give back to the community as much as we can.