How do I exclude linking a library for simulator builds

I have a third party library added to my project as ThirdParty.framework, but it only supports real device. I want to make my project still build for simulators but exclude this library and any references of importing it.

I tried a few things, for example in the target's build phase "Link Binary With Libraries" set this framework as "Optional", and in the code I have:

#if !targetEnvironment(simulator)
import ThirdParty

class SomeClass: ProtocolFromThirdParty {
// ...
}

#endif

Also some view that has reference to this class:

import SwiftUI

#if targetEnvironment(simulator)
struct TestViewSimulator: View {
    
    var body: some View {
        Text("See this view on real device")
    }
}
#else
struct TestView: View {
    @StateObject var example = SomeClass()
    var body: some View {
        // ...
    }
}
#endif

But still when I build for simulator, it still gives error for the linking issue:

Building for 'iOS-simulator', but linking in object file (/<Path>/ThirdParty.framework/ThirdPartySDK[arm64][2](someobject.o)) built for 'iOS'

Building to a real device works as expected.

Any suggestions I could make it build on simulators? (I'd like to isolate this screen only for real device and not impacting other parts of the app)

I can see in the build log the framework is already marked as -weak_framework ThirdParty

The best way to solve this problem is to convince the framework’s vendor to build and ship an XCFramework that supports the simulator. Even if the code is all a no-op on the simulator, it’ll make things radically easier for clients of that framework.

If they won’t do that then your next best option is to do it yourself. The simulator architecture in the framework doesn’t have to contain a lot of code — because you’re not referencing the code in your simulator build — it just has to be present.

If you don’t want to do that then things get uglier. The trick is to remove the library from the Link Binary With Libraries build phase and then reference the library in the Other Linker Flags build setting. You can then make that reference conditional on the SDK.

I can see in the build log the framework is already marked as -weak_framework ThirdParty

Right. Sadly, that won’t help because it affects the run-time behaviour, and you’re facing a build-time problem.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How do I exclude linking a library for simulator builds
 
 
Q