Location Service Manager for Kotlin Multiplatform Mobile iOS and android
Features
- Provides simple permission settings
- Dramatically reduce the amount of code
- Common interface available on KMM Shared
Requirements
- iOS
- Deployment Target 10.0 or higher
- Android
- minSdkVersion 21
Installation
Gradle Settings
Add below gradle settings into your KMP (Kotlin Multiplatform Project)
build.gradle.kts in root
buildscript {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:7.0.1")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21")
}
}
allprojects {
repositories {
google()
mavenCentral()
maven {
url = uri("http://repo.navercorp.com/maven-release/")
isAllowInsecureProtocol = true
}
maven {
url = uri("http://repo.navercorp.com/maven-snapshot/")
isAllowInsecureProtocol = true
}
}
}
build.gradle.kts in shared
plugins {
id("com.android.library")
kotlin("multiplatform")
kotlin("native.cocoapods")
}
val abcLocationLib = "com.linecorp:abc-kmm-location:0.2.3"
kotlin {
android()
ios {
binaries
.filterIsInstance<Framework>()
.forEach {
it.baseName = "shared"
it.transitiveExport = true
it.export(abcLocationLib)
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(abcLocationLib)
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation("com.google.android.material:material:1.2.1")
implementation(abcLocationLib)
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
implementation("androidx.test:core:1.0.0")
implementation("androidx.test:runner:1.1.0")
implementation("org.robolectric:robolectric:4.2")
}
}
val iosMain by getting {
dependencies {
implementation(abcLocationLib)
}
}
val iosTest by getting
}
}
Project Settings
Android
- You can use right now without other settings.
iOS
- Create
Podfile
with below setting in your project root.
use_frameworks!
platform :ios, '10.0'
install! 'cocoapods', :deterministic_uuids => false
target 'iosApp' do
pod 'shared', :path => '../shared/'
end
- Run command
pod install
on the terminal
Usage
Register handlers for permissions
-
Android
ABCLocation .onPermissionUpdated(this) { isGranted -> println("onPermissionUpdated -> isGranted: $isGranted") } .onLocationUnavailable(this) { println("onLocationUnavailable") }
-
iOS
ABCLocation.Companion() .onPermissionUpdated(target: self) { isGranted -> print("onPermissionUpdated -> isGranted: \(isGranted)") } .onLocationUnavailable(target: self) { print("onLocationUnavailable") } .onAlwaysAllowsPermissionRequired(target: self) { print("onAlwaysAllowsPermissionRequired") }
To get current location just once
-
Android
ABCLocation.currentLocation { data -> println("currentLocation -> data: $data") }
-
iOS
ABCLocation.Companion().currentLocation { data in print("currentLocation -> data: \(data)") }
To get current location whenever location changes
-
Android
ABCLocation .onLocationUpdated(this) { data -> println("onLocationUpdated -> data: $data") } .startLocationUpdating()
-
iOS
ABCLocation.Companion() .onLocationUpdated(target: self) { data in print("onLocationUpdated -> data: \(data)") } .startLocationUpdating()
To stop location updating
-
Android
ABCLocation.stopLocationUpdating()
-
iOS
ABCLocation.Companion().stopLocationUpdating()