带有框架的 iOS 应用程序在设备上崩溃,dyld:库未加载,Xcode 6 Beta

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24333981/
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-08-31 01:01:21  来源:igfitidea点击:

iOS app with framework crashed on device, dyld: Library not loaded, Xcode 6 Beta

iosswiftcrashxcode6dyld

提问by vladof81

This crash has been a blocking issue I used the following steps to reproduce the issue:

此崩溃一直是一个阻塞问题,我使用以下步骤来重现该问题:

  • Create a Cocoa Touch Framework project
  • Add a swift file and a class Dog
  • Build a framework for device
  • Create a Single View application in Swift
  • Import framework into app project
  • Instantiate swift class from the framework in ViewController
  • Build and run an app on the device
  • 创建 Cocoa Touch Framework 项目
  • 添加一个 swift 文件和一个类Dog
  • 构建设备框架
  • 在 Swift 中创建单视图应用程序
  • 将框架导入应用项目
  • 从 ViewController 中的框架实例化 swift 类
  • 在设备上构建并运行应用程序

The app immediate crashed upon launching, here is console log:

该应用程序在启动时立即崩溃,这是控制台日志:

dyld: Library not loaded: @rpath/FrameworkTest03.framework/FrameworkTest03
  Referenced from: /var/mobile/Applications/FA6BAAC8-1AAD-49B4-8326-F30F66458CB6/FrameworkTest03App.app/FrameworkTest03App
  Reason: image not found

I have tried to build on iOS 7.1 and 8.0 devices, they both have the same crash. However, I can build an app and run on the simulator fine. Also, I am aware that I can change the framework to form Requiredto Optionalin Link Binary With Libraries, but it did not completely resolve the problem, the app crashed when I create an instance of Dog. The behavior is different on the device and simulator, I suspect that we can't distribute a framework for the device using a beta version of Xcode. Can anyone shed light on this?

我尝试在 iOS 7.1 和 8.0 设备上进行构建,它们都发生了相同的崩溃。但是,我可以构建一个应用程序并在模拟器上正常运行。此外,我知道我可以在Link Binary With Libraries中将框架更改为从Requiredto Optional 的形式,但它并没有完全解决问题,当我创建Dog实例时应用程序崩溃了。设备和模拟器上的行为不同,我怀疑我们无法使用 Xcode 的 beta 版本为设备分发框架。任何人都可以阐明这一点吗?

回答by vladof81

In the target's Generaltab, there is an Embedded Binariesfield. When you add the framework there the crash is resolved.

在目标的General选项卡中,有一个Embedded Binaries字段。当您在那里添加框架时,崩溃就解决了。

Reference is hereon Apple Developer Forums.

参考是这里的苹果开发者论坛。

回答by Dawson

For iOS greater than or equal to 8

对于大于或等于 8 的 iOS

Under the target's General tab, in the Embedded Binaries section add the framework. This will copy the framework into the compiled so that it can be linked to at runtime.

在目标的 General 选项卡下,在 Embedded Binaries 部分中添加框架。这会将框架复制到编译中,以便它可以在运行时链接到。

enter image description here

在此处输入图片说明

Why is this happening? :because the framework you are linking to is compiled as a dynamically linked framework and thus is linked to at runtime.

为什么会这样?:因为您链接的框架被编译为动态链接的框架,因此在运行时链接到。

** Note:** Embedding custom frameworks is only supported in iOS > 8 and thus an alternative solution that works on older versions of iOS follows.

** 注意:** 嵌入自定义框架仅在 iOS > 8 中受支持,因此适用于旧版本 iOS 的替代解决方案如下。

For iOS less than 8

对于小于 8 的 iOS

If you influence this framework (have access to the source code/build process) you may change this framework to be statically linked rather than dynamically linked. This will cause the code to be included in your compiled app rather than linked to at runtime and thus the framework will not have to be embedded.

如果您影响这个框架(可以访问源代码/构建过程),您可以将此框架更改为静态链接而不是动态链接。这将导致代码包含在您编译的应用程序中,而不是在运行时链接到,因此不必嵌入框架。

** How:** Under the framework's Build Setting tab, in the Linking section, change the Mach-O Typeto Static Library. You should now not need to include the framework under embedded binaries.

** 如何:** 在框架的 Build Setting 选项卡下,在 Linking 部分,将Mach-O Type更改为 Static Library。您现在不需要在嵌入式二进制文件下包含框架。

Static Framework

静态框架

Including Assets:To include things such as images, audio, or xib/nib files I recommend creating a bundle (essentially a directory, more info here bit.ly/ios_bundle) and then load the assets from the bundle using NSBundle.

包括资产:为了包括诸如图像、音频或 xib/nib 文件之类的内容,我建议创建一个包(本质上是一个目录,更多信息请参见 bit.ly/ios_bundle),然后使用 NSBundle 从包中加载资产。

回答by Brandon A

Just dragging the framework into your project isn't going to be good enough. That is like being in the same ballpark but not being able to find your kids. Follow these steps:

仅仅将框架拖入您的项目是不够的。这就像在同一个球场,但找不到你的孩子。按着这些次序:

1)Create your framework

1)创建你的框架

  • Develop your framework.
  • Once your development is complete, COMMAND+Bbuild your framework and ensure you receive "Build Succeeded".
  • 开发你的框架。
  • 一旦您的开发完成,COMMAND+B构建您的框架并确保您收到“构建成功”。

enter image description here

在此处输入图片说明

2)Access your framework

2)访问您的框架

  • Once your framework project successfully builds it will then be ready for you to access in your Productsfolder in your project.
  • 一旦您的框架项目成功构建,您就可以在Products项目的文件夹中访问它了。

enter image description here

在此处输入图片说明

  • Right click on your .frameworkand select "Show in Finder".
  • 右键单击您的.framework并选择“在 Finder 中显示”。

enter image description here

在此处输入图片说明

3)Place framework in your project

3)在你的项目中放置框架

  • Drag and drop the .frameworkfrom your Finder window to your app project's "Framework" folder.
  • .framework从 Finder 窗口拖放到应用项目的“Framework”文件夹中。

enter image description here

在此处输入图片说明

4)Configure app project for framework

4)为框架配置应用项目

  • Select the top level in your project
  • 选择项目中的顶层

enter image description here

在此处输入图片说明

  • Choose your target
  • 选择你的目标

enter image description here

在此处输入图片说明

  • Go to "Build Phases", then "Link Binary with Libraries", and ensure that your framework is included with optional selected.
  • 转到“构建阶段”,然后“将二进制文件与库链接”,并确保您的框架包含在可选的 selected.

enter image description here

在此处输入图片说明

  • Still in "Build Phases", go to the upper left and select the +button. In the drop down choose "New Copy Files Phase".
  • 仍然在“构建阶段”中,转到左上角并选择+按钮。在下拉列表中选择“新复制文件阶段”。

enter image description here

在此处输入图片说明

  • Scroll down to the new "Copy Files" section and ensure that you set Destinationto "Frameworks". Leave the subpath empty. Then click the +button at the bottom left.
  • 向下滚动到新的“复制文件”部分并确保设置Destination为“框架”。将子路径留空。然后点击左下角的+按钮。

enter image description here

在此处输入图片说明

  • You will be presented with your project hierarchy. Scroll down to the "Frameworks" folder that you added the framework to in step 3, or search for it in the search bar at the top. Select your framework and click "Add".
  • 您将看到您的项目层次结构。向下滚动到您在步骤 3 中添加框架的“Frameworks”文件夹,或在顶部的搜索栏中搜索它。选择您的框架并单击“添加”。

enter image description here

在此处输入图片说明

  • Ensure that your framework is included with "Code Sign On Copy" selected.
  • 确保您的框架包含在选中的“代码登录副本”中。

enter image description here

在此处输入图片说明

5)Clean, then run your project

5)清理,然后运行你的项目

  • COMMAND+SHIFT+K
  • COMMAND+R
  • COMMAND+ SHIFT+K
  • COMMAND+R

回答by NeverHopeless

I created a framework using Swift3/Xcode 8.1 and was consuming it in an Objective-C/Xcode 8.1 project. To fix this issue I had to enable Always Embed Swift Standard Librariesoption under Build Options.

我使用 Swift3/Xcode 8.1 创建了一个框架,并在一个 Objective-C/Xcode 8.1 项目中使用它。为了解决这个问题,我必须Always Embed Swift Standard LibrariesBuild Options.

Have a look at this screenshot:

看看这个截图:

enter image description here

在此处输入图片说明

回答by Desert Rose

Firstly Try to build after Command+Option+Shift+K .If still fails then do below steps.

首先尝试在 Command+Option+Shift+K 之后构建。如果仍然失败,请执行以下步骤。

If anybody is facing this error in Xcode 8 then change your framework status to Optionalinstead of Required under the General Tab of your target.

如果有人在 Xcode 8 中遇到此错误,请在目标的“常规”选项卡下将您的框架状态更改为“可选”而不是“必需”。

enter image description here

在此处输入图片说明

回答by jfalexvijay

I got same kind of issue in iOS 9.x version

我在 iOS 9.x 版本中遇到了同样的问题

ISSUE IS: App crashes as soon as I open the app with below error.

dyld: Library not loaded: /System/Library/Frameworks/UserNotifications.framework/UserNotifications Referenced from: /var/containers/Bundle/Application/######/TestApp.app/TestApp Reason: image not found

I have resolved this issue by changing Requiredto Optionalin Linked Frameworks and Librariesfor UserNotifications.frameworkframework.

我已经通过更改RequiredOptionalin Linked Frameworks and LibrariesforUserNotifications.framework框架解决了这个问题。

回答by Aviel Gross

I had to (on top of what mentioned here) add the following line to Runpath Search Pathsunder Build Settingstab:
@executable_path/Frameworks

我不得不(除了这里提到的内容之外)将以下行添加到构建设置”选项卡下的“运行路径搜索路径
@executable_path/Frameworks

enter image description here

在此处输入图片说明

回答by liamnichols

You need to add the framework to a new Copy Files Build Phase to ensure that the framework is copied into the application bundle at runtime..

您需要将框架添加到新的复制文件构建阶段,以确保在运行时将框架复制到应用程序包中。

See How to add a 'Copy Files build phase' to my Targetfor more information.

有关更多信息,请参阅如何将“复制文件构建阶段”添加到我的目标

Official Apple Docs: https://developer.apple.com/library/mac/recipes/xcode_help-project_editor/Articles/CreatingaCopyFilesBuildPhase.html

苹果官方文档:https: //developer.apple.com/library/mac/recipes/xcode_help-project_editor/Articles/CreatingaCopyFilesBuildPhase.html

回答by Tamás Sengel

If you're using Xcode 11 or newer:

如果您使用的是Xcode 11 或更新版本

  1. Navigate to the settings of your target and select General.
  1. 导航到目标的设置并选择General

Target Settings/General

目标设置/常规

  1. Scroll down to Frameworks, Libraries and Embedded Content.

  2. Make sure the Embed & Signor Embed Without Signingvalue is selected for the Embedoption if necessary.

  1. 向下滚动到框架、库和嵌入式内容

  2. 如有必要,请确保为“嵌入”选项选择“嵌入并签名”或“嵌入而不签名”值。

Frameworks, Libraries and Embedded Content section

框架、库和嵌入式内容部分

回答by Vineesh TP

Add the framework in Embedded Binaries enter image description here

在嵌入式二进制文件中添加框架 在此处输入图片说明

Then Clean and Build.

然后清理和构建。