Thread
#multiplatform
    c

    Christian Gaisl

    1 year ago
    Hi, I’m building a multiplatform app and I have run into an issue in iOS regarding SwiftUI preview not working at all. I’ve boiled the issue down to the following minimum working example: I’m using an Android Studio KMM template project with the iOS framework distribution set to the CocoaPods dependency manager. When use a class that has the member-parameter syntax (I don’t know how it’s called? i mean “testString” in the following example code) in a View, the preview crashes. An example for such a class would be:
    //Does not work with SwiftUI preview
    class Greeting1(
        var testString: String
    ) {
        fun greeting(): String {
            return "Hello, ${Platform().platform}!"
        }
    }
    
    //Works with SwiftUI preview
    class Greeting2(
        testString: String
    ) {
        fun greeting(): String {
            return "Hello, ${Platform().platform}!"
        }
    }
    An instance of this class being part of the view, even if none of its members are actually displayed, crashes the preview. It works fine on the simulator though. This does not happen in the template project when using the Xcode build phases framework approach, the one with the packForXcode task. Does anyone have any idea what could cause this?
    xxfast

    xxfast

    1 year ago
    What does the swift ui preview diagnostics say?
    c

    Christian Gaisl

    1 year ago
    It simply states that the preview has crashed
    j

    José González Gómez

    1 year ago
    I seem to remember this was a known issue, but can't recall where did I read about it
    c

    Christian Gaisl

    1 year ago
    I don’t think that it’s the same problem, because my preview diagnostics output is different (and even less helpful):
    RemoteHumanReadableError: Failed to update preview.
    The preview process appears to have crashed.
    Error encountered when sending 'previewInstances' message to agent.
    ==================================
    | RemoteHumanReadableError: The operation couldn't be completed. (BSServiceConnectionErrorDomain error 3.)
    |
     
    | BSServiceConnectionErrorDomain (3):
    | ==BSErrorCodeDescription: OperationFailed
    Nevertheless, I tried one of the suggested solutions, which was using the following settings in my “shared” gradle file:
    kotlin {
        targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> {
            binaries.withType<org.jetbrains.kotlin.gradle.plugin.mpp.Framework> {
                isStatic = false
            }
        }
    }
    However, I did not get this to compile with this setting enabled, Xcode could not find the shared library at all anymore. I tried setting the framework search paths to $(inherited), but no luck. 😕
    Vitor Prado

    Vitor Prado

    1 year ago
    I fixed my problem with swiftui just removing build optimizations.
    (this is a setting in your ios project)
    just set your optimization level to none.
    xxfast

    xxfast

    1 year ago
    @Vitor Prado Where exactly is this setting?
    Vitor Prado

    Vitor Prado

    1 year ago
    look on build settings inside your schemes.
    open project settings.. go to your target settings.. open build setting tabs and use search to find “optimization level”
    c

    Christian Gaisl

    1 year ago
    Did not solve my problem either, I guess I’ll just use the Framework+PackForXcode approach and do pod dependencies within Xcode