基于iOS 11 UIDocumentBrowserViewController的应用文档未显示在“最近”标签页
Hey Robin, let's tackle this head-on—package-type documents like your FlipBooks have a few quirks when it comes to showing up in the Recents tab of UIDocumentBrowserViewController. Here are the most likely fixes to get them appearing:
1. Double-Check Your Info.plist Document Type Configuration
First, make sure your custom UTI and package type are properly registered so the system recognizes your FlipBooks as valid, indexable documents.
In your Info.plist, verify these settings under CFBundleDocumentTypes:
<key>CFBundleDocumentTypes</key> <array> <dict> <key>CFBundleTypeName</key> <string>FlipBook</string> <key>LSHandlerRank</key> <string>Owner</string> <!-- Critical: Marks your app as the owner of this type --> <key>LSItemContentTypes</key> <array> <string>com.yourcompany.flipbook</string> <!-- Your custom UTI --> </array> </dict> </array> <!-- Also define your custom UTI --> <key>UTExportedTypeDeclarations</key> <array> <dict> <key>UTTypeDescription</key> <string>FlipBook Image Package</string> <key>UTTypeIdentifier</key> <string>com.yourcompany.flipbook</string> <key>UTTypeConformsTo</key> <array> <string>com.apple.package</string> <!-- Conforms to package type --> </array> <key>UTTypeTagSpecification</key> <dict> <key>public.filename-extension</key> <array> <string>flipbook</string> <!-- Your custom file extension --> </array> </dict> </dict> </array>
If LSHandlerRank is set to something lower (like Alternate), the system might not prioritize your app's documents for the Recents tab.
2. Ensure You're Updating Document Metadata After Changes
Package-type documents are directories, so the system doesn't automatically detect content changes the way it does for single-file docs. After creating or editing a FlipBook, you need to explicitly tell the system to reindex the document.
Add this code after saving your document's content:
let fileManager = FileManager.default let documentURL = // URL to your FlipBook package directory // Update the modification date to trigger indexing try? fileManager.setAttributes([.modificationDate: Date()], ofItemAtPath: documentURL.path) // If using UIDocument, make sure you call this to mark changes as done yourDocument.updateChangeCount(.done)
This tells the system's spotlight index that the document has been modified and should be included in Recents.
3. Verify Your Documents Are Stored in Valid Locations
Documents saved to temporary directories or non-indexable locations won't show up in Recents. Make sure you're creating/saving FlipBooks in:
- The app's local Documents directory:
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] - iCloud Drive (if enabled): The URL provided by UIDocumentBrowser's creation workflow
Avoid using NSTemporaryDirectory() or cache directories for user-editable documents.
4. Force the Document Browser to Reload Its Data
Sometimes the browser's cache doesn't refresh immediately after creating a document. After saving, call:
documentBrowserViewController.reloadData()
This will trigger the browser to re-fetch the latest document list from the system.
5. Test System Indexing with NSMetadataQuery
To confirm if the system is actually indexing your FlipBooks, run a quick metadata query:
let query = NSMetadataQuery() query.predicate = NSPredicate(format: "%K LIKE %@", NSMetadataItemContentTypeKey, "com.yourcompany.flipbook") query.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope, NSMetadataQueryLocalDocumentsScope] NotificationCenter.default.addObserver(forName: .NSMetadataQueryDidFinishGathering, object: query, queue: .main) { _ in print("Found \(query.resultCount) FlipBook documents") query.disableUpdates() } query.start()
If this returns zero results, your UTI configuration or metadata updates are the issue. If it returns your documents, the problem is likely with the document browser's cache (reloading data should fix it).
Walk through these steps one by one—most of the time, it's either a missing LSHandlerRank setting or forgetting to trigger metadata updates after package changes.
内容的提问来源于stack exchange,提问作者Robin

