Hi, I’m building a multiplatform app and I have ru...
# multiplatform
c
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:
Copy code
//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?
x
What does the swift ui preview diagnostics say?
c
It simply states that the preview has crashed
j
I seem to remember this was a known issue, but can't recall where did I read about it
c
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:
Copy code
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. 😕
v
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.
x
@Vitor Prado Where exactly is this setting?
v
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
Did not solve my problem either, I guess I’ll just use the Framework+PackForXcode approach and do pod dependencies within Xcode