如何创建项目模板

how to create a project template

关于自定义模板的主题。 我正在自学如何使用 xcode 7 和 objective c 执行此操作,但我被卡住了。到目前为止,通过阅读 S.O 上的其他帖子。我设法通过复制单一视图应用程序并将其放入 xcode 包的正确目录来创建自定义模板。我的模板文件夹如下所示:

如果我点击 Main.storyboard 并说添加几个按钮并保存,然后在 xcode 中打开我的自定义模板,按钮就在那里,所以我知道我正在取得进展.不过,我想做的是创建视图控制器文件和 xib 并包含应用程序图标等......这样我就可以拥有一个正确定制的模板来启动我的所有项目。这就是我被困的地方。我不确定如何从简单文件 Main.storyboard 开始执行此操作。谁能指出我正确的方向。

特别是,我正在做的是根本没有故事板,而是从 xib 开始。我猜我需要对 TemplateInfo.plist 做点什么。我看了又看,找不到任何相关信息,所以只需重定向即可。 谢谢

创建 Xcode 模板可能会非常复杂,因为大多数可以编辑的项目都可以编写脚本。重申一下您已经知道的内容,应该在 ~/Library/Developer/Xcode/Templates 目录中创建一个新文件夹。这是项目创建时 Xcode 扫描的位置之一。例如,将此文件夹命名为 "Custom Templates" 将使它以该名称显示在 Xcode 的项目创建对话框中。在这个新文件夹中创建另一个名为 "baseApp.xctemplate" 的文件夹。一旦我们的所有文件都在其中,这将使一个名为 baseApp 的模板出现。

通过复制 Xcode 的现有模板之一来启动新模板最简单。主要是因为从头开始编写最重要的 TemplateInfo.plist 可能很麻烦。这些文件位于 Xcode 应用程序中。在取景器中显示其内容并导航至: /Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application

从 "Single View Application" 模板中将所有 3 个文件复制到之前创建的 baseApp.xctemplate 文件夹中。下一步很关键,但在那之后第一个模板就准备好了。双击 TemplateInfo.plist 文件在 Xcode 中打开它,并将 Identifier 键的最后一部分更改为 baseApp。就是这样 - Xcode 中的新项目对话框现在应该如下图所示。更改您的 TemplateIcon.tiff 是个好主意。除此之外,BaseApp 还包含 Objective-c 和 Swift、故事板、CoreData 支持等选项。

现在根据新的 baseApp 模板(无选项)创建一个名为 "xibApp" 的新 Xcode 项目。然后执行以下 4 个步骤:

• 删除 Main.storyboard & ViewController(移至回收站)

• 删除 Info.plist

中的 "Main storyboard file base name" 条目

• 添加一个新的 Cocoa Touch Class called ViewController based on UIViewController 并选中复选框以包含 xib 文件

• 将 AppDelegate 中的 didFinishLaunchingWithOptions 函数替换为:

    window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
    window?.backgroundColor = UIColor.whiteColor()

    let viewController = ViewController()

    window!.rootViewController = viewController
    window!.makeKeyAndVisible()

    return true

您现在应该可以 运行 该应用程序了。如果一切正常,退出 Xcode。复制自定义模板目录中的 baseApp.xctemplate 并将其重命名为 "xibApp.xctemplate"。然后删除该目录下的Main.storyboard文件,将xibApp文件夹中的AppDelegate、Info.plist、ViewController.swift和ViewController.xib文件复制到xibApp.xctemplate文件夹中。双击xibApp文件夹中的TemplateInfo.plist,在Xcode中打开,将标识符重命名为"com.apple.dt.unit.xibApp"。需要解决的另一件事是此文件中的第一个祖先。它设置为 "com.apple.dt.unit.storyboardApplication" 并且如果离开将使所有生成的项目成为故事板项目。为了弥补这一点,我们将创建自己的祖先。

Xcode 使用祖先层次结构来包含整个项目树。任何扩展名为 .xctemplate 的文件夹都可以作为祖先,只要它位于 Xcode 目录中 - 应用程序本身或 ~/Library/Developer/Xcode/ 中。此外,这些目录必须包含正确设置的 TemplateInfo.plist 文件。因此,在 "Cocoa Touch Application Base.xctemplate" 中获取 TemplateInfo.plist,它位于 Xcode 应用程序的内容中 (Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application)。将此文件放在 "Custom Templates" 文件夹内的新文件夹中,并将文件夹命名为 "Custom Xib Base.xctemplate"。这个文件夹的名字将成为我们祖先的名字。 Xcode 将删除名称中的所有空格并将其变为 "customXibBase",这就是我们的新祖先的扩展名:"com.apple.dt.unit.customXibBase"。由于 Custom Xib Base.xctemplate 文件夹可能成为其他模板的祖先,因此将其移动到 Custom Templates 目录(即 "shared")中一个新的适当命名的子文件夹可能是个好主意。

回到xibApp.xctemplate目录里面的TemplateInfo.plist。双击它,单击 Ancestors 旁边的显示三角形,将第一项下的 "storyboardApplication" 替换为 "customXibBase",以便整行显示为 "com.apple.dt.unit.customXibBase"。最后,我们需要为我们包含的 4 个文件提供定义和节点,如下图所示(AppDelegate、Info.plist、ViewController.swift 和 ViewController.xib)。保存文件并退出 Xcode。全部做完。 如果一切顺利,现在在 Custom Templates 类别下应该有一个名为 xibApp 的新项目模板!我认为这是一种生成新模板的可行但有点老套的方法。它可能会与 Xcode 的较新版本一起中断。但它是进一步探索项目模板的良好开端。

xibApp.xctemplate的最终TemplateInfo.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Kind</key>
    <string>Xcode.Xcode3.ProjectTemplateUnitKind</string>
    <key>Identifier</key>
    <string>com.apple.dt.unit.xibApp</string>
    <key>Ancestors</key>
    <array>
        <string>com.apple.dt.unit.customXibBase</string>
        <string>com.apple.dt.unit.coreDataCocoaTouchApplication</string>
    </array>
    <key>Concrete</key>
    <true/>
    <key>Description</key>
    <string>This template provides a starting point for an application that uses a single view. It provides a view controller to manage the view, and a storyboard or nib file that contains the view.</string>
    <key>SortOrder</key>
    <integer>1</integer>
    <key>Options</key>
    <array>
        <dict>
            <key>Identifier</key>
            <string>languageChoice</string>
            <key>Units</key>
            <dict>
                <key>Objective-C</key>
                <dict>
                    <key>Nodes</key>
                    <array>
                        <string>ViewController.h:comments</string>
                        <string>ViewController.h:imports:importCocoa</string>
                        <string>ViewController.h:interface(___FILEBASENAME___ : UIViewController)</string>
                        <string>ViewController.m:comments</string>
                        <string>ViewController.m:imports:importHeader:ViewController.h</string>
                        <string>ViewController.m:extension</string>
                        <string>ViewController.m:implementation:methods:viewDidLoad(- (void\)viewDidLoad)</string>
                        <string>ViewController.m:implementation:methods:viewDidLoad:super</string>
                        <string>ViewController.m:implementation:methods:didReceiveMemoryWarning(- (void\)didReceiveMemoryWarning)</string>
                        <string>ViewController.m:implementation:methods:didReceiveMemoryWarning:super</string>
                    </array>
                </dict>
                <key>Swift</key>
                <dict>
                    <key>Nodes</key>
                    <array>
                        <string>ViewController.swift:comments</string>
                        <string>ViewController.swift:imports:importCocoa</string>
                        <string>ViewController.swift:implementation(___FILEBASENAME___: UIViewController)</string>
                        <string>ViewController.swift:implementation:methods:viewDidLoad(override func viewDidLoad(\))</string>
                        <string>ViewController.swift:implementation:methods:viewDidLoad:super</string>
                        <string>ViewController.swift:implementation:methods:didReceiveMemoryWarning(override func didReceiveMemoryWarning(\))</string>
                        <string>ViewController.swift:implementation:methods:didReceiveMemoryWarning:super</string>
                    </array>
                </dict>
            </dict>
        </dict>
    </array>
    <key>Definitions</key>
    <dict>
        <key>AppDelegate.swift</key>
        <dict>
            <key>Path</key>
            <string>AppDelegate.swift</string>
            <key>TargetIndices</key>
            <array>
                <integer>0</integer>
            </array>
        </dict>
        <key>Info.plist</key>
        <dict>
            <key>Path</key>
            <string>Info.plist</string>
            <key>TargetIndices</key>
            <array>
                <integer>0</integer>
            </array>
        </dict>
        <key>ViewController.swift</key>
        <dict>
            <key>Path</key>
            <string>ViewController.swift</string>
            <key>TargetIndices</key>
            <array>
                <integer>0</integer>
            </array>
        </dict>
        <key>ViewController.xib</key>
        <dict>
            <key>Path</key>
            <string>ViewController.xib</string>
            <key>TargetIndices</key>
            <array>
                <integer>0</integer>
            </array>
        </dict>
    </dict>
    <key>Nodes</key>
    <array>
        <string>AppDelegate.swift</string>
        <string>Info.plist</string>
        <string>ViewController.swift</string>
        <string>ViewController.xib</string>
    </array>
</dict>
</plist>

这个答案有什么问题吗?请告诉我。这将为您提供基于 xib 的模板,创建新的自定义祖先,并允许通过添加新定义和节点向模板添加所需数量的文件。