SwiftUI Setup (Optional)
Creating an AppDelegate for Customization and Setup
To perform setup tasks such as initializing Kommunicate or handling its push notifications, you need to include an AppDelegate in your SwiftUI project.
If your project does not already include an AppDelegate, follow these steps to add and configure one.
Step 1: Create an AppDelegate.swift File
Create a new Swift file in your project and name it AppDelegate.swift.
Step 2: Add the Following Code
Paste the following implementation inside AppDelegate.swift:
import UIKit
import Kommunicate
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Initialize Kommunicate with your App ID
Kommunicate.setup(applicationId: "YOUR_APP_ID")
return true
}
}
Step 3: Register AppDelegate in Your SwiftUI App
In your main App struct (usually the entry point of the SwiftUI app), register the AppDelegate:
@main
struct YourApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Step 4: Add Customization Code in AppDelegate
Add your customization logic inside the AppDelegate. Here's an example:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Initialize Kommunicate with your App ID
Kommunicate.setup(applicationId: "YOUR_APP_ID")
// Customize UI settings
Kommunicate.defaultConfiguration.hideBottomStartNewConversationButton = true
return true
}
For more customization options, refer to the 📄 Kommunicate iOS Customization Guide.
Step 5: Setup Push Notifications (Optional)
To handle push notifications, your AppDelegate should conform to UNUserNotificationCenterDelegate and import the necessary framework:
import UserNotifications
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
// Implement required notification delegate methods here
}
Refer to the official Kommunicate documentation for push notification setup instructions: 📄 Kommunicate iOS Push Notification Guide
Tip: Using
UIApplicationDelegateAdaptorallows you to maintain UIKit-style configuration and lifecycle management while still leveraging SwiftUI.
Using the Top View Controller Instead of self
When working with SwiftUI or other UIKit contexts where self (i.e., the current UIViewController) isn't directly available, you can use a utility function to retrieve the top-most view controller in your app. This is especially useful when you need to present a view controller or trigger UI-related actions.
Utility Extension
Add the following extension to retrieve the top view controller from the current window scene:
extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap { $0.windows }
.first(where: { $0.isKeyWindow })?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(base: nav.visibleViewController)
} else if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
return topViewController(base: selected)
} else if let presented = base?.presentedViewController {
return topViewController(base: presented)
}
return base
}
}
Example Usage
Here's how you can use this utility in a practical scenario—for instance, when integrating Kommunicate to present a conversation screen:
guard let topVC = UIApplication.topViewController() else { return }
Kommunicate.createAndShowConversation(from: topVC) { error in
guard error == nil else {
print("Conversation error: \(error.debugDescription)")
return
}
// Success
}
Note: This approach ensures your app uses the correct top-most view controller when presenting new UI, avoiding issues with undefined or missing
selfin SwiftUI environments.