创建 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 文件。因此,请获取“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 目录中一个适当命名的新子文件夹(即“共享”)。
Back to the TemplateInfo.plist inside the xibApp.xctemplate directory. Double-click it, click the disclosure triangle next to Ancestors and under item one replace "storyboardApplication" with "customXibBase" so that the whole line reads "com.apple.dt.unit.customXibBase". Finally we need to provide Definitions and Nodes for the 4 files we are including as seen in the image below (AppDelegate, Info.plist, ViewController.swift and ViewController.xib). Save the file and quit Xcode. All done.
If everything went alright there should now be a new project template under the Custom Templates category called xibApp! I would consider this a workable but hackish approach towards generating new templates. It will probably break with newer versions of Xcode. But it's a good starter to further explore project templates.
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 的模板,创建新的自定义祖先,并允许通过添加新的定义和节点来向模板添加所需数量的文件。