如何在 macOS 上创建可加载的捆绑包?

huangapple go评论125阅读模式
英文:

How to create a loadable bundle on macOS?

问题

看起来您正在寻找创建一个插件的方法。如果我在Xcode中设置macOS项目时选择"Bundle"作为模板,似乎并没有创建所需的插件内容/结构。

根据苹果的文档,一个典型的可加载Bundle的目录结构如下:

  • MyLoadableBundle
    • Contents/
      • Info.plist
      • MacOS/
        • MyLoadableBundle
      • Resources/
        • Lizard.jpg
        • MyLoadableBundle.icns
        • English.lproj/
          • MyLoadableBundle.nib
          • InfoPlist.strings
        • Japanese.lproj/
          • MyLoadableBundle.nib
          • InfoPlist.strings

您想知道这个结构是如何生成的,是否有一种方法可以直接从Xcode创建可加载的Bundle。

更新

对于一个名为PluginTest的Bundle项目,其中有一个文件plugin.swift,根据我的理解,Xcode应该创建类似以下的结构:

  • PluginTest
    • Contents/
      • Info.plist
      • MacOS/
        • PluginTest

根目录是一个.bundle文件夹。

但实际上它看起来像这样:

  • PluginTest
    • PluginTest-Bridging-Header.h
    • plugin.swift
    • PluginTest.xcodeproj

根目录是一个普通的文件夹。

我已经尝试手动生成可执行文件,手动创建文档中显示的文件夹结构,并将可执行文件添加到MacOS文件夹,还手动更新Xcode中Info标签中的主要类字段。

英文:

Looking to make a plug-in.

If I select 'Bundle' as the template when setting-up a macOS project in Xcode, this doesn't seem to create the actual content / structure required for a bundle.

Looking at Apple's docs it says...

// The directory layout of a typical loadable bundle. 
- MyLoadableBundle
    Contents/
        Info.plist
        MacOS/
            MyLoadableBundle
        Resources/
            Lizard.jpg
            MyLoadableBundle.icns
            English.lproj/
                MyLoadableBundle.nib
                InfoPlist.strings
            Japanese.lproj/
                MyLoadableBundle.nib
                InfoPlist.strings

How / where is this generated?

Have tried manually creating this structure but the result is not loadable. Surely there is a way of creating a loadable bundle directly from Xcode?

*** update ***

So, for a Bundle project called PluginTest with one file, plugin.swift, my understanding is that Xcode should have created something that looks like...

- PluginTest
      Contents/
          Info.plist
          MacOS/
          PluginTest  

...where the root is a .bundle.

But, it looks like this...

- PluginTest
      PluginTest-Bridging-Header.h
      plugin.swift
      PluginTest.xcodeproj 

...where the root is a normal directory folder.

I have tried manually generating the executable, manually creating the folder structure shown in the docs and adding the executable to the MacOS folder, and manually updating the principal class field in the Info tab in Xcode.

答案1

得分: 1

将您的文件添加到项目中。最好创建一个名为“Resources”的组来存放资源。Xcode会在很大程度上负责创建目标的结构。

您可以通过查看构建阶段来获得更多控制,特别是“复制捆绑资源”阶段。

Info.plist文件将从目标的“Info”选项卡中生成。

为了更详细说明,我刚刚创建了一个捆绑项目。这是我在Xcode中看到的内容:

如何在 macOS 上创建可加载的捆绑包?

从上到下:

  • Resources是我创建的一个组,然后将PDF文件添加到其中,以创建一个资源。
  • File.swift是我添加的一个文件,包含Swift的struct,以便在捆绑中添加一些代码。
  • Products是我创建项目时Xcode创建的产品文件夹,其中包含构建的捆绑。请注意,这只是一个虚拟视图:实际上,构建的捆绑位于Xcode的派生数据文件夹下的某个构建目录中。
  • MyBundle-Bridging-Header.h是在我添加File.swift时创建的头文件。

当我将ground rent.pdf添加到Resources组时,文件打开对话框中有一些额外的选项,我忽略了它们,保持了默认设置。

构建的捆绑具有以下结构:

如何在 macOS 上创建可加载的捆绑包?

我通过在Xcode的产品文件夹中右键单击捆绑并选择“在Finder中显示”,然后在Finder中右键单击捆绑并选择“显示捆绑内容”来获取这些信息。它具有苹果文档所记录的正确结构,我在Xcode中没有做任何特殊设置。

需要注意的几点:Info.plist文件是由构建自动生成的,它反映了在MyBundle目标的信息选项卡中输入的值。

如何在 macOS 上创建可加载的捆绑包?

PDF文件位于Resources文件夹中,这是因为“复制捆绑资源”构建阶段。Xcode自动识别PDF作为资源并自动添加了该行。这不妨碍您将其他项目文件添加到列表中。您添加的任何内容都将被复制到捆绑的资源文件夹中。

英文:

Just add your files to the project. It's probably best to create a group called Resources for the resources. Xcode will more or less take care of creating the structure of the target.

You can get more control by looking at the build phases, the "copy bundle resources" phase in particular.

The Info.plist file will be generated from the "Info" tab on for the target.

To expand on this. I just created a bundle project. This is what I see in Xcode:

如何在 macOS 上创建可加载的捆绑包?

From the top:

  • Resources is a group I created and then added the pdf to, to make a resource
  • File.swift is a file I added containing a Swift struct so that there would be some code in the bundle.
  • Products is the product folder created by Xcode when I created the project and it contains the built bundle. NB this is a virtual view: the built bundle is actually located in a build directory somewhere under Xcode's derived data folder.
  • MyBundle-Bridging-Header.h is a header file that was created when I added File.swift

When I added ground rent.pdf to the Resources group, the file open dialogue had some extra options in it that I ignored and just left as defaulted.

The built bundle has the following structure:

如何在 macOS 上创建可加载的捆绑包?

This I obtained by right clicking on the bundle in the Products folder in Xcode and selecting "Show in Finder" and then right clicking on the bundle in the Finder and selecting "show bundle contents". It has the proper structure as documented by Apple and I had to do nothing special in Xcode to make it like that.

A couple of things to note: the file Info.plist was generated automatically by the build. It reflects the values entered into the info tab for the MyBundle target.

如何在 macOS 上创建可加载的捆绑包?

The pdf is in the Resources folder because of the "copy bundle resources" build phase.

如何在 macOS 上创建可加载的捆绑包?

Xcode figured out that the pdf was a resource and added that line automatically. That doesn't preclude me from adding other project files to the list. anything you do add there will be copied into the bundle's resource folder.

huangapple
  • 本文由 发表于 2023年3月7日 16:51:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定