xcode 用于 iOS 和 OS X 开发的共享代码库

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

Shared code base for iOS and OS X development

iosxcodemacosmultiplatformcode-sharing

提问by poundev23

We have a fairly rich e-learning app, built mostly using cocos2d. Currently we are in alpha and want to setup our project structure so we can also build a Mac version to target the Mac App store. It is about 80% cocos2d with some intitial screens in UIKit which will have to be ported to Mac (re-written).

我们有一个相当丰富的电子学习应用程序,主要使用 cocos2d 构建。目前我们处于 alpha 阶段,想要设置我们的项目结构,以便我们也可以构建一个 Mac 版本以面向 Mac App Store。大约 80% 的 cocos2d 与 UIKit 中的一些初始屏幕必须移植到 Mac(重新编写)。

What is the recommended setup for targeting both the Mac and iOS app stores from a single code base? I assume the choices are:

从单个代码库同时针对 Mac 和 iOS 应用商店的推荐设置是什么?我假设选择是:

  1. Create 2 xCode projects in the same application source code root folder and use each project to build a single target. This would be: Project.xcodeproj and ProjectMac.xcodeproj
  2. Add a new Mac target to our existing iPad application project and then fiddle with target membership to get the desired results. This would be just: Project.xcodeproj
  1. 在同一个应用程序源代码根文件夹中创建 2 个 xCode 项目,并使用每个项目构建单个目标。这将是:Project.xcodeproj 和 ProjectMac.xcodeproj
  2. 向我们现有的 iPad 应用程序项目添加一个新的 Mac 目标,然后调整目标成员资格以获得所需的结果。这将只是:Project.xcodeproj

Further complicating the situation is that we currently use cocos2d as a static library for the iOS app. We also have a library called CoreInfrastructure that has a lot of code we use across all our projects. Recently I have figured out that I can create a project to simultaneously build a framework targeting Mac and a library targeting iOS from the same code base. This is done by starting with a framework project and adding a target to build a static lib for iOS.

更复杂的情况是我们目前使用 cocos2d 作为 iOS 应用程序的静态库。我们还有一个名为 CoreInfrastructure 的库,其中包含我们在所有项目中使用的大量代码。最近我发现我可以创建一个项目,以从相同的代码库同时构建一个面向 Mac 的框架和一个面向 iOS 的库。这是通过从框架项目开始并添加目标来为 iOS 构建静态库来完成的。

So just wanted to get everyone's opinion and insight. Anyone know of any caveats to watch out for in the above choices? Anyone who is building for Mac and iOS app stores simultaneously care to share their structure? Adding a target worked on our library code ... is that the way to go for the application as well?

所以只是想得到大家的意见和见解。任何人都知道在上述选择中需要注意的任何警告?任何同时为 Mac 和 iOS 应用商店构建的人都愿意分享他们的结构吗?添加一个对我们的库代码起作用的目标......这也是应用程序的方法吗?

Are there any issues doing archive and distribution builds for either choice?

为这两种选择进行归档和分发构建是否有任何问题?

Thanks in advance.

提前致谢。

采纳答案by torrey.lyons

For the applications use two separate projects. Using multiple targets for iOS and Mac in one project is very useful if they are sharing a library or framework. However, in your top level application almost nothing is shared. The UIKit code will need to be totally rewritten to use AppKit, the dependencies will be different, and even most of the project settings will vary.

对于应用程序,使用两个单独的项目。如果 iOS 和 Mac 共享一个库或框架,在一个项目中使用多个目标是非常有用的。但是,在您的顶级应用程序中,几乎没有任何内容是共享的。UIKit 代码需要完全重写才能使用 AppKit,依赖项会有所不同,甚至大部分项目设置也会有所不同。

Of course if you really want to see everything at once you can put both platform specific application projects and all the shared dependent library/framework projects in a single workspace. This is more a question of work style. If you want to switch back and forth between the two frequently this makes the most sense. If you want to simplify what you are looking at you can put them in separate workspaces that share many of the same projects. Separate workspaces has the disadvantage that a project can only be open in one workspace at a time so you effectively can only work on one at a time.

当然,如果您真的想一次查看所有内容,您可以将特定于平台的应用程序项目和所有共享的依赖库/框架项目放在一个工作区中。这更多是工作作风的问题。如果您想经常在两者之间来回切换,这是最有意义的。如果您想简化您正在查看的内容,您可以将它们放在共享许多相同项目的单独工作区中。单独的工作区的缺点是一个项目一次只能在一个工作区中打开,因此您一次只能有效地处理一个。

回答by Tomasz B?k

WWDC session "Sharing code between iOS and OS X"answers all the basic questions in this topic. iWorkteam presented how they have got away with creating Pages, Keynote and Numbers with shared code base for both iOS and OS X.

WWDC 会议“在 iOS 和 OS X 之间共享代码”回答了本主题中的所有基本问题。iWork团队展示了他们如何使用 iOS 和 OS X 的共享代码库创建 Pages、Keynote 和 Numbers。

The key for their project was using:

他们项目的关键是使用:

  • separate Xcode targets for iOS and OS X
  • separate project for the shared code in a form of a .framework
  • target dependency on the framework from the point above
  • iOS 和 OS X 的单独 Xcode 目标
  • .framework形式的共享代码的单独项目
  • 从上面的点对框架的目标依赖

I encourage to watch the video or read the transcript from this session:

我鼓励观看视频或阅读本次会议的成绩单:

WWDC 2014 Sharing code between iOS and OS X

WWDC 2014 在 iOS 和 OS X 之间共享代码

ASCIIWWDC transcript

ASCIIWWDC 成绩单

回答by rayvinly

I recently used kstenerud's iOS Universal Frameworkto build a shared framework codebase that works for both iOS and Mac apps. I just needed to manually add a target for a Cocoa framework after I had created a project for an iOS framework. That way I can develop the sharable code once in the framework and link it in both the iOS and Mac apps. You can even make the framework contain UIKit-specific code for your iOS app and AppKit-specific code for your Mac apps. I wrote about it in my blogif you are interested.

我最近使用kstenerud 的 iOS 通用框架构建了一个适用于 iOS 和 Mac 应用程序的共享框架代码库。在为 iOS 框架创建项目后,我只需要手动为 Cocoa 框架添加目标。这样我就可以在框架中开发一次可共享的代码,并将其链接到 iOS 和 Mac 应用程序中。您甚至可以让框架包含适用于 iOS 应用程序的 UIKit 特定代码和适用于 Mac 应用程序的 AppKit 特定代码。如果你有兴趣,我在我的博客上写过它。

回答by justin

I just use multi-platform static library targets for the shared sources. You will need to expand that to the dependencies, however. If you have platform dependent implementations, you may want to create supplemental export libraries for those symbols.

我只是将多平台静态库目标用于共享源。但是,您需要将其扩展到依赖项。如果您有平台相关的实现,您可能需要为这些符号创建补充导出库。

So your structure might take this form:

因此,您的结构可能采用以下形式:

  • CoreInfrastructure- cross platform static library.
  • PlatShared- cross platform static library.
  • PlatSpecific-OS X- OS X static library (or framework).
  • PlatSpecific-iOS- iOS static library.
  • CoreInfrastructure- 跨平台静态库。
  • PlatShared- 跨平台静态库。
  • PlatSpecific-OS X- OS X 静态库(或框架)。
  • PlatSpecific-iOS- iOS 静态库。

The OS X app links to CoreInfrastructure, PlatShared, PlatSpecific-OSX, Cocos for OS X, and system libs.

OS X 应用程序链接到 CoreInfrastructure、PlatShared、PlatSpecific-OSX、OS X 的 Cocos 和系统库。

The iOS app links to CoreInfrastructure, PlatShared, PlatSpecific-iOS, Cocos for iOS, and sys libs.

iOS 应用程序链接到 CoreInfrastructure、PlatShared、PlatSpecific-iOS、Cocos for iOS 和 sys libs。

Problem (I've found) is, there are a lot of people who have not had much/any experience developing and maintaining complex project structures in Xcode. It's a pain (IMO) to setup duplicate targets, and properly maintain them as they grow -- even when they all refer to the same source files. That's why i prefer minimal targets and proper dependency structure.

问题(我发现)是,有很多人没有太多/任何在 Xcode 中开发和维护复杂项目结构的经验。设置重复的目标并在它们增长时正确维护它们是一种痛苦(IMO)——即使它们都引用相同的源文件。这就是为什么我更喜欢最小的目标和适当的依赖结构。