I created a new app in Xcode 11.2 based off the iOS Document Based app template. I then modified the document type to a custom type, and made a small change to use a file in the bundle as a template when making a new document. I left the rest of the default boilerplate alone. I tried building the app (both with an iPhone and iPad target) and tried to create a new app. Nothing happens, no errors are raised, and the delegate function didRequestDocumentCreationWithHandler
is not called (I added a print statement as the first line to make sure). Why is this? What did I change to break this?
Of interesting note, before making any changes, after building, the "browse" pane of the document view had an icon for "Create Document" (a blank square with a plus in the middle), but after making changes, that and the folder name have both vanished (Broken | Expected):
I suspect the error is something subtle in my 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>
Just in case, my Document Browser Delegate:
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)
}
}
}
I've compared my project with working examples, and can't find any major differences...
I determined the issue: Custom UTIs must (it appears) specify a conforming type that descends from either public.data
or com.apple.package
.
So adding this to the UTI worked:
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
References: