Skip to content

Commit 0e92a71

Browse files
committed
-iOS Toast Enhancement
1 parent a455bf2 commit 0e92a71

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

alert-kmp/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ mavenPublishing {
7575
coordinates(
7676
groupId = "io.github.khubaibkhan4",
7777
artifactId = "alert-kmp",
78-
version = "1.0.4"
78+
version = "1.0.5"
7979
)
8080

8181
// Configure POM metadata for the published artifact

alert-kmp/src/iosMain/kotlin/NativeNotify.ios.kt

+27-17
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,38 @@ import platform.UserNotifications.UNNotificationRequest
1515
import platform.UserNotifications.UNNotificationSound
1616
import platform.UserNotifications.UNTimeIntervalNotificationTrigger
1717
import platform.UserNotifications.UNUserNotificationCenter
18+
import platform.darwin.dispatch_async
19+
import platform.darwin.dispatch_get_main_queue
1820

1921
actual fun Notify(message: String, duration: NotificationDuration) {
20-
val viewController = UIApplication.sharedApplication.keyWindow?.rootViewController?.modalViewController
21-
val alertController = UIAlertController.alertControllerWithTitle(
22-
title = UIDevice.currentDevice.systemName,
23-
message = message,
24-
preferredStyle = UIAlertControllerStyle.MAX_VALUE
25-
)
26-
alertController.addAction(
27-
UIAlertAction.actionWithTitle(
28-
"OK",
29-
style = UIAlertControllerStyle.MAX_VALUE,
30-
handler = null
22+
val viewController = UIApplication.sharedApplication.keyWindow?.rootViewController
23+
if (viewController != null) {
24+
val alertController = UIAlertController.alertControllerWithTitle(
25+
title = UIDevice.currentDevice.systemName,
26+
message = message,
27+
preferredStyle = UIAlertControllerStyleAlert
3128
)
32-
)
33-
viewController?.presentViewController(alertController, animated = true, completion = {
34-
val delay = if (duration == NotificationDuration.LONG) 3.0 else 1.5
35-
NSTimer.scheduledTimerWithTimeInterval(delay, repeats = false) {
36-
alertController.dismissViewControllerAnimated(true, completion = null)
29+
alertController.addAction(
30+
UIAlertAction.actionWithTitle(
31+
"OK",
32+
style = UIAlertActionStyleDefault,
33+
handler = null
34+
)
35+
)
36+
37+
dispatch_async(dispatch_get_main_queue()) {
38+
viewController.presentViewController(alertController, animated = true, completion = {
39+
val delay = if (duration == NotificationDuration.LONG) 3.0 else 1.5
40+
NSTimer.scheduledTimerWithTimeInterval(delay, repeats = false) {
41+
alertController.dismissViewControllerAnimated(true, completion = null)
42+
}
43+
})
3744
}
38-
})
45+
} else {
46+
NSLog("Error: rootViewController is null")
47+
}
3948
}
49+
4050
actual fun createNotification(type: NotificationType): Notification = when (type) {
4151
NotificationType.ALERT -> object : Notification() {
4252
override fun show(message: String) {

0 commit comments

Comments
 (0)