我基于iOS Document Based应用程序模板在Xcode 11.2中创建了一个新应用程序。然后,我将文档类型修改为自定义类型,并做了一些小的更改以在制作新文档时将分发包中的文件用作模板。我将其余的默认样板保留下来。我尝试构建该应用程序(同时具有iPhone和iPad目标)并尝试创建一个新应用程序。什么都不会发生,不会引发任何错误,并且不会调用委托函数didRequestDocumentCreationWithHandler
(我添加了一条print语句作为第一行来确保)。为什么是这样?我为改变这一点做了什么改变?
有趣的是,在进行任何更改之前,构建后,文档视图的“浏览”窗格具有“创建文档”图标(中间带有加号的空白方框),但是进行更改之后,和文件夹名称都消失了(“ Broken” |“预期”):
我怀疑错误是我的info.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>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>Lifreyan Layout</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>name.cableray.lifreyanlayout</string>
</array>
</dict>
</array>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportsDocumentBrowser</key>
<true/>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeDescription</key>
<string>Lifreyan Script Layout</string>
<key>UTTypeIconFiles</key>
<array/>
<key>UTTypeIdentifier</key>
<string>name.cableray.lifreyanlayout</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>lifreyanlayout</string>
<key>public.mime-type</key>
<string>name.cableray.lifreyanlayout</string>
</dict>
</dict>
</array>
<key>UTImportedTypeDeclarations</key>
<array/>
</dict>
</plist>
以防万一,我的文档浏览器代表:
import UIKit
import SwiftUI
class DocumentBrowserViewController: UIDocumentBrowserViewController, UIDocumentBrowserViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
allowsDocumentCreation = true
allowsPickingMultipleItems = false
}
// MARK: UIDocumentBrowserViewControllerDelegate
func documentBrowser(_ controller: UIDocumentBrowserViewController, didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Void) {
print("importing new")
let newDocumentURL = Bundle.main.url(forResource: "Default", withExtension: "lifreyanlayout")
// Set the URL for the new document here. Optionally, you can present a template chooser before calling the importHandler.
// Make sure the importHandler is always called, even if the user cancels the creation request.
if newDocumentURL != nil {
importHandler(newDocumentURL, .copy)
} else {
importHandler(nil, .none)
}
}
func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentsAt documentURLs: [URL]) {
guard let sourceURL = documentURLs.first else { return }
// Present the Document View Controller for the first document that was picked.
// If you support picking multiple items, make sure you handle them all.
presentDocument(at: sourceURL)
}
func documentBrowser(_ controller: UIDocumentBrowserViewController, didImportDocumentAt sourceURL: URL, toDestinationURL destinationURL: URL) {
// Present the Document View Controller for the new newly created document
presentDocument(at: destinationURL)
}
func documentBrowser(_ controller: UIDocumentBrowserViewController, failedToImportDocumentAt documentURL: URL, error: Error?) {
// Make sure to handle the failed import appropriately, e.g., by presenting an error message to the user.
}
// MARK: Document Presentation
func presentDocument(at documentURL: URL) {
let document = Document(fileURL: documentURL)
// Access the document
document.open(completionHandler: { success in
if success {
// Display the content of the document:
let view = DocumentView(document: document, dismiss: {
self.closeDocument(document)
})
let documentViewController = UIHostingController(rootView: view)
self.present(documentViewController, animated: true, completion: nil)
} else {
// Make sure to handle the failed import appropriately, e.g., by presenting an error message to the user.
}
})
}
func closeDocument(_ document: Document) {
dismiss(animated: true) {
document.close(completionHandler: nil)
}
}
}
我已经将我的项目与working examples进行了比较,但发现没有重大区别...
答案 0 :(得分:0)
我确定了问题:自定义UTI必须(出现)指定一个从public.data
或com.apple.package
派生的符合类型。
因此将其添加到UTI即可:
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
参考文献: