这是使用嵌入式可可豆荚创建动态/嵌入式框架的正确方法吗? [英] Is this the correct way to create dynamic/embedded framework with embedded cocoa pods?

查看:102
本文介绍了这是使用嵌入式可可豆荚创建动态/嵌入式框架的正确方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建动态框架以在我的应用程序的各种扩展程序之间共享代码.

I am trying to create a Dynamic Framework to share the code among various extensions of my app.

问题:

这是我的项目结构.

MyFrameworks是我的应用程序的网络层,其固有地使用Alamofire.因此,我的pod文件的结构如下.

MyFrameworks is a network layer of my app which inherently uses Alamofire. So structured my pod file as follow.

platform :ios, '9.0'
use_frameworks!

workspace 'CocoaPodsProjectworkspace'

def shared_pods
    pod 'Alamofire'
    pod 'SwiftyJSON'
end

target 'CocoaPodsProject' do
    project 'CocoaPodsProject.xcodeproj'
    # Pods for CocoaPodsProject
end

target 'MyFramework' do
    project 'MyFramework/MyFramework.xcodeproj'
    shared_pods
end

target 'CocoaPodsProjectTests' do

end

target 'CocoaPodsProjectUITests' do

end

在构建框架时,将其作为嵌入式二进制文件拖到我的Main项目中时,会出现错误.

On building the framework when I drag it as embedded binary to my Main project I get the error.

dyld:未加载库:.framework/Alamofire引用自: /用户/sandeep/库/开发人员/Xcode/DerivedData/CocoaPodsProjectworkspace-enpobdyluhbxdwazuvbfogcspfof/Build/Products/Debug-iphonesimulator/MyFramework.framework/MyFramework 原因:找不到图片

dyld: Library not loaded: .framework/Alamofire Referenced from: /Users/sandeep/Library/Developer/Xcode/DerivedData/CocoaPodsProjectworkspace-enpobdyluhbxdwazuvbfogcspfof/Build/Products/Debug-iphonesimulator/MyFramework.framework/MyFramework Reason: image not found

我尝试过的解决方案:

  • 在链接中将pods_frameworks.framework声明为可选 二进制文件.
  • 尝试更改框架动态库的RunPath搜索路径
  • 安装名称Running pod解散并再次运行pod安装.
  • 删除派生数据和重新链接框架都导致相同的结果 问题.
  • Declaring the pods_frameworks.framework as optional in linked binaries.
  • Tried changing RunPath Search path of framework Dynamic Library
  • Install name Running pod deintegrate and running pod install again.
  • Deleting derived data and relinking framework all lead to same problem.

有效的解决方案:

我意识到MyFramework.framework试图在错误的目录中查找Alamofire.framework,并且始终尝试使用该框架相对于项目/目标进行搜索.因此,我能找到的最简单的解决方案是按如下方式修改pod文件.

I realized that MyFramework.framework was trying to find the Alamofire.framework in a wrong directory and it was alway trying to search relative to project/target using the framework . So the simplest solution that I could find was to modify pod file as follow.

platform :ios, '9.0'
use_frameworks!

workspace 'CocoaPodsProjectworkspace'

def shared_pods
    pod 'Alamofire'
    pod 'SwiftyJSON'
end

target 'CocoaPodsProject' do
    project 'CocoaPodsProject.xcodeproj'
    shared_pods
    # Pods for CocoaPodsProject
end

target 'MyFramework' do
    project 'MyFramework/MyFramework.xcodeproj'
    shared_pods
end

target 'CocoaPodsProjectTests' do

end

target 'CocoaPodsProjectUITests' do

end 

如您所见,我将shared_pods添加到了主应用程序和框架项目以及它们各自的目标中.现在一切正常.我既不必将pods_framework设置为可选,也不必修改MyFramework的构建设置.

As you can see I added the shared_pods to both main app and my framework project and their respective targets. Now everything works smooth. I neither had to make pods_framework optional nor had to modify the build settings of MyFramework.

问题:

将共享存储库添加到所有要使用我的框架的项目及其目标中,似乎没有多余之处.有没有一种更好的方法可以指定Myframework.framework来读取其所有依赖关系,而不是从使用它的项目中读取?

Adding the shared repos to all the projects and their targets which wants to use my framework looks little redundant. Is there a better way I can specify Myframework.framework to read all its dependencies rather than reading from project using it?

我对CocoaPods Git仓库提出了同样的问题.但是,因为这并不是天生的问题,所以他们可能不会恢复原状.因此,将其作为问题发布在此处.发布链接: https://github.com/CocoaPods/CocoaPods/issues/6901 (如果有帮助的话).

I have raised a issue for the same on CocoaPods Git repo. But because it isn't inherently a issue they might not revert back. Hence posting it as a question here.Link to issue : https://github.com/CocoaPods/CocoaPods/issues/6901 if it helps.

推荐答案

通过为我的自定义框架创建可可豆荚并使用可可豆荚依赖性解决了该问题.

Solved it by creating a cocoa pod for my custom Framework and using cocoa pods dependency .

步骤1:清理Main/Parent项目

  • 从项目中删除了MyFramework(已作为子项添加 项目),然后删除在项目的嵌入式库中添加的MyFramework.framework常规设置.
  • 运行pod deintegrate(以取消集成已添加到
    的Pod 项目)
  • 现在该项目是干净的,没有添加任何吊舱 通过运行pod init
  • 初始化Pod
  • Removed MyFramework from the project (which was added as sub project) and remove MyFramework.framework added in embedded library of projects General settings.
  • Run pod deintegrate (to de-integrate the pod already added to
    project)
  • Now that the project is clean and does not have any pod added initialized the pod by running pod init

第2步:为我的框架创建Pod

  • Using cd command navigate to MyFramework project and once you are in MyFramework's root folder run pod spec create MyFramework
  • This will create a file named MyFramework.podspec in the MyFramework's root folder. Open MyFramework.podspec using any of the editor tool and update it as shown in tutorial https://www.raywenderlich.com/126365/ios-frameworks-tutorial

本教程中没有的最重要的步骤是如何添加 我们的框架需要建立的可可豆荚依赖关系.原来 多数民众赞成在最简单的部分.就我而言,我需要SwiftyJSONAlamofire所以在我添加的.podspec文件中,

Most important step which is not there in this tutorial is how to add cocoa pods dependency that our framework needs to build. Turns out thats the most easiest part. In my case I needed SwiftyJSON and Alamofire so in .podspec file I added,

s.dependency'Alamofire'

s.dependency 'Alamofire'

s.dependency'SwiftyJSON'

s.dependency 'SwiftyJSON'

第3步:

  • 现在打开Main/Parent项目Podfile并进行更新,如下所示 在下面.
  • Now open the Main/Parent projects Podfile and update it as shown below.
target 'CocoaPodsProject' do
  use_frameworks!
  pod 'TestFramework', :path => 'TestFramework/'
end

它所做的是,它告诉主项目添加TestFramework作为依赖项,并将TestFramework安装在主项目的Framework文件夹中.因为TestFramework本身具有对Alamofire& SwiftyJSON当您运行pod install时,它不仅会安装Alamofire,还会安装SwiftyJSON并将其添加到TestFramework's Framework文件夹中.

What it did is, it tells main project to add TestFramework as dependency and installs TestFramework in Framework folder of main project. Because TestFramework in itself has dependency to Alamofire & SwiftyJSON when you run pod install it will not only install Alamofire but also installs SwiftyJSON and adds it to TestFramework's Framework folder.

仅此而已.现在您的TestFramework可以访问AlamofireSwiftyJSON,并且Main/Patent项目可以访问TestFramework

Thats all. Now your TestFramework can access Alamofire and SwiftyJSON and Main/Patent project can access TestFramework

如果您现在要更新TestFramework代码,请在完成开发之前将其添加为主项目的子项目(这是必要的,因为如果您打开TestFramework.xcproj,您将看不到Alamofire/Swifty JSON.您必须打开Parent项目的工作区本身,因此是此解决方案).一旦完成开发,如果您决定删除TestFramework作为子项目,则可以执行此操作:)

If you want to now update TestFramework code, till u finish develop add it as subproject to Main project (This is necessary because if you open TestFramework.xcproj u won't see Alamofire/Swifty JSON. You have to open the Parent project's workspace itself hence this solution). Once u are done with development if u decide to remove the TestFramework as subproject u can do that :)

最后,如果您决定向应用程序添加其他扩展名,可以说您添加今天"扩展名,您要做的就是按照以下步骤修改您的Podfile.

Finally If you decide to add additional extensions to app, Lets say u add Today extension all u have to do is to modify ur Podfile as follow.

target 'CocoaPodsProject' do
  use_frameworks!
  pod 'TestFramework', :path => 'TestFramework/'
end

target 'CocoapodsToday' do
  use_frameworks!
  pod 'TestFramework', :path => 'TestFramework/'
end

呜呜呜:)现在,将您的框架添加到所需的多个扩展中:)希望对您有所帮助.

Woo hooo :) Now add your frameworks to as many extensions you want :) Hope it helps.

这篇关于这是使用嵌入式可可豆荚创建动态/嵌入式框架的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆