mirzemehdi / KMPNotifier

Kotlin Multiplatform Push Notification Library targetting android, iOS, Desktop and Web (JS and Wasm)
http://mirzemehdi.com/KMPNotifier/
Apache License 2.0
340 stars 19 forks source link

Unable to build for iOS #7

Closed lmiskovic closed 10 months ago

lmiskovic commented 10 months ago

Hey, I'm trying to set up KMPNotifier to be used in shared code. Here is the relevant code. Am I maybe missing something? Android is working properly but for iOS I'm getting following errors:

> Task :shared:linkDebugFrameworkIosSimulatorArm64 FAILED
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld invocation reported errors

The /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld command returned non-zero exit code: 1.
output:
ld: warning: ignoring duplicate libraries: '-ldl'
ld: framework 'FirebaseMessaging' not found
error: Compilation finished with errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':shared:linkDebugFrameworkIosSimulatorArm64'.
> Compilation finished with errors

I've gone through all steps I believe. library is imported as api in shared source sets

    sourceSets {

        val commonMain by getting {
            dependencies {
                ....
                api(libs.kmpnotifier)

            }
        }

I've added the required library in Swift PM

image

Here is all of the Swift code. Please note that I'm not an iOS developer

import SwiftUI
import shared
import KMMViewModelSwiftUI
import FirebaseCore
import FirebaseMessaging

class AppDelegate: NSObject, UIApplicationDelegate {

  func application(_ application: UIApplication,
                   didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

      FirebaseApp.configure()
      NotifierManager.shared.initialize(configuration: NotificationPlatformConfigurationIos.shared)

    return true
  }

  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
  }

}

@main
struct iOSApp: App {
    init() {
        KoinKt.doInitKoin()
        NapieriOS.companion.setupLogging()
    }

    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate

    var body: some Scene {
        WindowGroup {
            RootView(model: RootModel(), viewModel: ViewModelsHelper().rootViewModel)
        }
    }
}
lmiskovic commented 10 months ago

Looks like I've been missing this part

export(libs.kmpnotifier)
isStatic = true

Works after adding it

anuragdalia commented 5 months ago

@mirzemehdi can you explain or point to the doc where it explains why you need to export + download via swiftPM even though you have added it via pod into your library

mirzemehdi commented 5 months ago

@anuragdalia export part is needed for calling library methods from Swift when needed. And the reason why you need download also via Swift PM or Cocopoads, when it is not added from Swift side, ios compilation fails. I am not sure what is main reason behind that though

anuragdalia commented 5 months ago

Thanks. I have a use case for FIRanalytics. when i add analytics in the cocoapods section in the build.gradle.kts, i get duplicate class warnings. I understand they are coming because FirebaseCore is imported indirectly via SwiftPM as well as via your lib and my own cocoapods { pod("FirebaseMessaging") } It's not related to your library for sure, but if (you know anything about it + can shed some light on it), it'd be super awesome.

mirzemehdi commented 5 months ago

@anuragdalia Unfortunately, I don't know full context so don't know the solution. But maybe you add FirebaseMessaging from both Swift PM and Cocopoads at the same time and this can cause the issue.

or you can check this out and test if it would help:

https://kotlinlang.org/docs/native-cocoapods-libraries.html#with-custom-cinterop-options

When you add cocoapods { pod("FirebaseMessaging") } setting different packageName. I never tested this though

anuragdalia commented 5 months ago

Thanks @mirzemehdi