Search code examples
iosxcodexcode8ios-app-extensionios-frameworks

Linking to a Embedded Framework from a app extension


I have a project with

  • one application target: MyApp
  • one Embedded Framework target: MyKit.framework
  • one application extension target: MyExtension

Here is a sample project.

I want to use the shared code of MyKit in the MyExtension but when I link it, I get the warning

ld: warning: linking against a dylib which is not safe for use in application extensions: /Users/me/Library/Developer/Xcode/DerivedData/MyApp-dnztzmxjghjlsteetlokzhjtjqkm/Build/Products/Debug-iphonesimulator/MyKit.framework/MyKit

Apple's documentation says

To configure an app extension target to use an embedded framework, set the target’s “Require Only App-Extension-Safe API” build setting to Yes. If you don’t, Xcode reminds you to do so by displaying the warning “linking against dylib not safe for use in application extensions”.

which is correctly set by default. I could get rid of the warning by setting "Require Only App-Extension-Safe API" to NO but this could lead to some app rejections.

The framework does not use any API not allowed in an app extension. In fact, in the sample project, you'll see that MyKit.framework only logs a message to a console.

What is a correct way to link to en embedded framework from an app extension to avoid this wrning?


Solution

  • If your goal is to share code in between App and Extension, you do not need to create a Framework. You can add the source file to different targets:

    To use a framework, set "Require Only App-Extension-Safe API" to YES in the framework target build settings. After doing so, there are no warnings in your sample project.


    Opinion based addition: As you do not want to share code in between projects, using a frameworks does not make sense to me here. If you want to share the code in between projects and thus decide to use a framework, I recommend to make it an independent Xcode project under own version control.