Auto switching between a Device and Simulator Framework

I've built a Framework which I would like to use it in other applications.

I know the basics on including and using frameworks.


The problem is I really have TWO Frameworks the Device Framework and the Simulator Framework.


Is there a way to tell XCode to use the right Framework?

Replies

Have you considered making two targets, each with it's own specific framework?

The problem is a little bit more complicated, because you have (at least) two configurations as well: Debug and Release. If your framework is debugged and rock-solid, then you can use the Release configuration in other applications during debugging, but if you want to debug into the framework that's not a great approach.


You can solve this problem as well as your device/simulator problem if you include the framework project as a subproject of each other application that needs it. That way the correct configuration/architecture gets built for whatever you're doing at the moment.


In regard to your original question, I can only tell you what other people are doing, even though it's something that's explicitly unsupported by Apple. Some 3rd party iOS developers (including Microsoft) build the Intel architectures and ARM architectures separately, then 'lipo' them into a single "fat" framework. It happens to work for now, though of course I'm not recommending this as a solution. (Plus, you'd still have the Debug/Release configuration issue.)

Is this the basic Lipo approach:

https://developer.apple.com/library/mac/technotes/tn2284/_index.html

This seems to spell it out:

https://medium.com/@syshen/create-an-ios-universal-framework-148eb130a46c

If you can have the framework project present in the same workspace as your application, you can try this:


  1. Use a shared build folder for your workspace and/or individual projects (File > Workspace/Project Settings > Advanced > Build Location)
  2. Build your frameworks targeting 'iOS Device" so that when the frameworks are added the references are valid (if you select a simulator for this step, this won't work, ...who knows why this is necessary)
  3. Add the framework(s) to the project that is going to use them (probably under "linked frameworks and libraries"), and once they show up on the project navigator, select each one and make sure that "Location" (under file inspector, on your right side pane) is set to "Relative to Build Products". This will make your project looks for them using the BUILT_PRODUCTS_DIR environment variable, so no relative or absolute paths will be embedded in your project file (no Debug/Release-iphoneos/iphonesimulator references)


With this, next time you compile your project, Xcode should find the corresponding framework (for either device or simulator) with no problems.


Also, don't forget to include your frameworks in your unit tests targets if you use them there.


And you may want to check your Build Settings for any unexpected/unnecessary entries added by Xcode to your "Framework Search Paths", and remove them.

Aouch ... 6 years old !

Because XCFramework don't work with embedded statics lib, I use a workaround with two run scripts in build phase.

Note : for both script, check Based on depedency analysis to skip if not necessary, be sure For install builds only is uncheck


First script at the beginning of the build phase :

if [ $ARCHS == 'arm64' ] 
then 
  cp -rf ./lib/iphone/libName.framework ./lib/libName.framework
else
  cp -rf ./lib/simulator/libName.framework ./lib/libName.framework
fi

Second script at the end of the build phase

 rm -rf ./lib/libName.framework `

Note : if your framework is still there when you change your arch (i.e. from simulator to device ) the phase prepare build will stop your build.

That's the reason of the deletion at the end of the build phase, without the second script you will have this error :

path.xcodeproj Building for iOS, but the linked and embedded framework 'libName.framework' was built for iOS Simulator.


Note 2 : provide one framework (in the lib folder in my case) for the first build, and run with the associated arch

  • Hello and great answer. Can you tell me should I disable the target membership on the 2 frameworks that I have in device and sim folders ? Because if checks are on they are build and I get "Building for iOS, but the linked and embedded framework 'libName.framework' was built for iOS Simulator" ?

Add a Comment