Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a static library only when testing with device and not simulator?

I have external closed library that can compile with armv7s (etc) only. when I try to compile against simulator obviously its not running and display some errors. I don't want to insert this library to my code unless I can configure Xcode to use this library only when i test with a device. unfortunately, i tried to do it with cocoapods with no success and i wonder if there any way to do it?

like image 680
dang Avatar asked Oct 23 '14 14:10

dang


People also ask

Are XCFramework static or dynamic?

An XCFramework can be either static or dynamic and can include headers.

What is static framework in iOS?

Static frameworks contain a static library packaged with its resources. Dynamic frameworks contain the static/dynamic library with its resources. In addition to that, dynamic frameworks may conveniently include different versions of the same dynamic library in the same framework.

What are dynamic libraries in Swift?

Dynamic library - a unit of code and/or assets linked at runtime that may change. However, only Apple is allowed to create dynamic libraries for iOS . You're not allowed to create these, as this will get your app rejected.


1 Answers

Yes, this can be done. I had a similar problem with a framework that caused linker errors only in the simulator so I setup up my project to only use the framework when building for a device.

The following assumes you are not using cocoa pods to link the library. I'm not sure what would need to be changed if you are.

  1. Select your target and go to the Build Phases tab.
  2. Under the "Link Binary With Libraries" section, remove the static library from the list but make sure the library file still exists in your project files under Frameworks folder. If the library file name is missing the leading "lib" in its name you might need to rename the file in the project navigator and add the leading "lib" to the file name.
  3. Go to the Build Settings tab.
  4. Find the "Other Linker Flags" setting.
  5. Double-click on the Debug value. Tap the + and enter "-l"
  6. In place of enter the actual name of your library minus the leading "lib". Do not include the extension. The "libMyLibrary.a" should be entered as "-lMyLibrary"
  7. Select the Debug value and notice a little circled +. Click the +.
  8. Click on the new "Any Architecture | Any SDK" part and change it to "Any iOS Simulator SDK".
  9. Now double click on the value to the right of "Any iOS Simulator SDK" and remove the -lsomeLibrary entry you added.

Now do a debug build.

The above change basically means that the library is linked in for all builds except for iOS Simulator builds.

You will probably also need to make some code changes. Any code making any reference to header files or other symbols from the library should be wrapped as follows:

#if !TARGET_IPHONE_SIMULATOR
#import "someLibrary.h"
#endif

#if !TARGET_IPHONE_SIMULATOR
    // Use stuff from the library
#endif
like image 132
rmaddy Avatar answered Nov 15 '22 21:11

rmaddy