创建 Xcode 模板可能相当复杂,因为大多数可以编辑的项目都可以编写脚本。重申一下你已经知道的,应该在 ~/Library/Developer/Xcode/Templates 目录中创建一个新文件夹。这是 Xcode 在项目创建时扫描的位置之一。例如,将此文件夹命名为“自定义模板”将使其在 Xcode 的项目创建对话框中以该名称显示。在这个新文件夹中创建另一个名为“baseApp.xctemplate”的文件夹。一旦我们的所有文件都在其中,这将使名为 baseApp 的模板出现。
通过复制 Xcode 的现有模板之一来启动新模板是最简单的。主要是因为从头开始编写最重要的 TemplateInfo.plist 可能很麻烦。这些文件位于 Xcode 应用程序中。在查找器中显示其内容并导航到:
/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application
从“单一视图应用程序”模板将所有 3 个文件复制到之前创建的 baseApp.xctemplate 文件夹。下一步至关重要,但此后第一个模板几乎已准备就绪。双击 TemplateInfo.plist 文件以在 Xcode 中打开它并将 Identifier 键的最后一部分更改为 baseApp。就是这样 - Xcode 中的新项目对话框现在应该如下图所示。更改您的 TemplateIcon.tiff 是个好主意。除此之外,BaseApp 还提供 Objective-c 以及 Swift、Storyboards、CoreData 支持等选项。
现在基于新的 baseApp 模板创建一个名为“xibApp”的新 Xcode 项目(无选项)。然后按照以下 4 个步骤操作:
• 删除 Main.storyboard 和 ViewController(移至垃圾箱)
• 删除 Info.plist 中的“主情节提要文件基本名称”条目
• 基于 UIViewController 添加一个名为 ViewController 的新 Cocoa Touch 类,并选中该框以包含一个 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 文件并将 AppDelegate、Info.plist、ViewController.swift 和 ViewController.xib 文件从 xibApp 文件夹复制到 xibApp.xctemplate 文件夹。双击xibApp文件夹中的TemplateInfo.plist,在Xcode中打开,将标识符重命名为“com.apple.dt.unit.xibApp”。需要解决的另一件事是此文件中的第一个祖先。它设置为“com.apple.dt.unit.storyboardApplication”,如果离开,所有生成的项目都会变成故事板项目。为了解决这个问题,我们将创造我们自己的祖先。
Xcode 使用祖先层次结构来包含整个项目树。任何扩展名为 .xctemplate 的文件夹都可以作为祖先,只要它位于 Xcode 目录中 - 无论是应用程序本身还是在 ~/Library/Developer/Xcode/ 中。此外,这些目录必须包含正确设置的 TemplateInfo.plist 文件。因此,获取位于 Xcode 应用程序内容(Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application)中的“Cocoa Touch Application Base.xctemplate”中的 TemplateInfo.plist。将此文件放在“自定义模板”文件夹内的新文件夹中,并将文件夹命名为“自定义 Xib Base.xctemplate”。此文件夹的名称将成为我们祖先的名称。 Xcode 将删除名称中的所有空格并将其变为“customXibBase”,这就是我们新祖先的扩展名:“com.apple.dt.unit.customXibBase”。由于 Custom Xib Base.xctemplate 文件夹可能成为其他模板的祖先,因此最好将其移动到 Custom Templates 目录中一个新的恰当命名的子文件夹(即“共享”)。
回到 xibApp.xctemplate 目录中的 TemplateInfo.plist。双击它,单击 Ancestors 旁边的显示三角形,并在第一项下将“storyboardApplication”替换为“customXibBase”,以便整行显示为“com.apple.dt.unit.customXibBase”。最后,我们需要为我们包含的 4 个文件提供定义和节点,如下图所示(AppDelegate、Info.plist、ViewController.swift 和 ViewController.xib)。保存文件并退出 Xcode。全部做完。
如果一切顺利,现在应该有一个名为 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 的模板,创建一个新的自定义祖先,并允许通过添加新定义和节点将任意数量的文件添加到模板中。