xcode 从应用程序扩展链接到嵌入式框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42362390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:47:59  来源:igfitidea点击:

Linking to a Embedded Framework from a app extension

iosxcodexcode8ios-app-extensionios-frameworks

提问by Jan

I have a project with

我有一个项目

  • one application target: MyApp
  • one Embedded Framework target: MyKit.framework
  • one application extension target: MyExtension
  • 应用目标之一: MyApp
  • 一个嵌入式框架目标: MyKit.framework
  • 一个应用程序扩展目标: MyExtension

Here is a sample project.

这是一个示例项目。

I want to use the shared code of MyKitin the MyExtensionbut when I link it, I get the warning

我想使用的共享代码MyKitMyExtension,但是当我联系了,我得到的警告

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”.

要将应用扩展目标配置为使用嵌入式框架,请将目标的“仅需要应用扩展安全 API”构建设置设置为是。如果您不这样做,Xcode 会通过显示警告“链接到 dylib 在应用程序扩展中使用不安全”来提醒您这样做。

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

这是默认设置正确的。我可以通过将“Require Only App-Extension-Safe API”设置为 NO 来消除警告,但这可能会导致一些应用程序被拒绝。

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

该框架不使用应用程序扩展中不允许的任何API。实际上,在示例项目中,您将看到MyKit.framework仅将消息记录到控制台。

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

从应用程序扩展链接到嵌入式框架以避免这种错误的正确方法是什么?

回答by shallowThought

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:

如果您的目标是在 App 和 Extension 之间共享代码,则不需要创建框架。您可以将源文件添加到不同的目标:

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

要使用框架,请YES在框架目标构建设置中将“Require Only App-Extension-Safe API”设置为。这样做之后,您的示例项目中就没有警告了。



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.

基于意见的补充:由于您不想在项目之间共享代码,因此在这里使用框架对我来说没有意义。如果您想在项目之间共享代码并因此决定使用框架,我建议将其设为一个独立的 Xcode 项目,由自己的版本控制。