Home > database >  XCode weakly-linked/optional framework not excluded
XCode weakly-linked/optional framework not excluded

Time:08-03

I am working on an iOS project. I am using a framework (UnityFramework) that only works on device and not on the simulator. I want to exclude the framework when running on simulator for ease of testing, as the app will not compile otherwise.

I have followed the instructions from this StackOverflow post (which seems to be out of date) and this Apple documentation to weakly link the framework so that it is not included when building for simulator, but I still get the same error when building for simulator: framework not found.

I am not sure if I am missing something I need to exclude this framework from simulator builds, or if something is wrong with my configuration. I have tried using both -weak_framework and -ObjC -weak_framework in "Other Linker Flags." I am using Xcode 13.3.

My build settings:

Frameworks, Libraries, and Embedded Content: UnityFramework.framework - Embed & Sign

Build Settings - Other Linker Flags - All marked with -weak_framework UnityFramework

Build Phases - Link Binary with Libraries - UnityFramework.framework - Optional

Here is a snippet of the error I get:

Ld .../Library/Developer/Xcode/DerivedData/app-gygzddvoumbadmatpojfllhgdkag/Build/Products/Debug-iphonesimulator/AppDevelopment.app/AppDevelopment normal (in target 'AppDevelopment' from project 'App') cd .../projects/app-folder

-Xlinker .../Library/Developer/Xcode/DerivedData/app-gygzddvoumbadmatpojfllhgdkag/Build/Intermediates.noindex/app.build/Debug-iphonesimulator/appDevelopment.build/Objects-normal/x86_64/appDevelopment.swiftmodule -weak_framework UnityFramework -Xlinker -sectcreate -Xlinker __TEXT -Xlinker

ld: framework not found UnityFramework

CodePudding user response:

Assuming the name of your framework is YourFramework:

#if !targetEnvironment(simulator) //notice the not operator (!)
import YourFramework //YourFramework will be imported only when not on simulator
#endif

It also might be worth checking #if canImport(YourFramework)

CodePudding user response:

Fixed my problem in the following way:

First, set validate workspace to 'yes'.

Secondly, xcode was looking for my framework in derivedData. I copied the framework to the project folder and then added it to 'Frameworks' by manually dragging it in.

The problem seemed to be that xcode could not build my framework in the simulator condition, but it still needed to link it, and so it couldn't 'find' the built framework. By making a copy of the framework and keeping it in the project directory, it won't look for it in derived data anymore. There is probably a better way to do this with a build script.

I can now run the app in both the simulator and on a device, and using the #if targetEnvironment(simulator) attribute I can access features of the framework perfectly well on device and leave them out on simulator.

I now get the following warning when building for simulator, which did not appear beforehand:

Building for iOS Simulator, but the linked and embedded framework 'UnityFramework.framework' was built for iOS.

  • Related