You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UIDocumentPickerViewController/UIDocumentBrowserViewController显示异常求助

Hey Eric,

I took a look at your code and noticed a couple of critical issues that are almost certainly causing the display failure on iOS 14. Let’s break this down and fix it step by step:

1. Delegate Assignment Bug (Biggest Culprit)

You’re setting documentPicker.delegate = vc where vc is the picker instance itself. While your extension conforms to UIDocumentPickerDelegate, assigning the picker as its own delegate creates weird lifecycle conflicts and retain cycles that can prevent the system from rendering the picker UI correctly.

Fix: Assign the delegate to the extension’s static context instead, or better yet, let the presenting view controller handle delegate callbacks. For your current extension pattern, updating the delegate assignment to vc.delegate = self (using the extension’s delegate implementation) will resolve this.

2. Array Appending Mistake in Pre-iOS 14 Code

In the iOS <14 branch, you’re using types.append(solidInputTypes) where solidInputTypes is already an array. This nests the array inside types instead of adding individual elements, leading to invalid document type entries. The same issue exists with your default types array.

Fix: Use append(contentsOf:) instead of append() to add array elements correctly.

3. Presentation Style Compatibility

Using .fullScreen for the document picker on iOS 14+ can clash with the system’s preferred presentation behavior for this view controller. Switching to .pageSheet (the system default) often resolves rendering glitches.

Refactored Working Code

Here’s your extension with all the fixes applied:

extension UIDocumentPickerViewController {
    private static weak var instance: UIDocumentPickerViewController?
    private static var completion: (([URL]?)->Void)?
    
    @objc public class func show(on viewController: UIViewController, supportedDocumentTypes: [String]?, completion: @escaping ([URL]?)->Void) {
        let vc: UIDocumentPickerViewController
        
        if #available(iOS 14.0, *) {
            var types = [UTType]()
            if let supportedTypes = supportedDocumentTypes {
                types = supportedTypes.compactMap { UTType($0) }
            } else {
                types = [
                    .pdf,
                    UTType("com.microsoft.word.doc")!,
                    UTType("com.microsoft.excel.xls")!,
                    UTType("com.microsoft.powerpoint.ppt")!,
                    UTType("org.openxmlformats.wordprocessingml.document")!,
                    UTType("org.openxmlformats.presentationml.presentation")!,
                    UTType("org.openxmlformats.spreadsheetml.sheet")!
                ]
            }
            vc = UIDocumentPickerViewController(forOpeningContentTypes: types, asCopy: true)
        } else {
            var types = [String]()
            if let supportedTypes = supportedDocumentTypes {
                types.append(contentsOf: supportedTypes)
            } else {
                types = [
                    "com.adobe.pdf",
                    "com.microsoft.word.doc",
                    "com.microsoft.excel.xls",
                    "com.microsoft.powerpoint.ppt",
                    "org.openxmlformats.wordprocessingml.document",
                    "org.openxmlformats.presentationml.presentation",
                    "org.openxmlformats.spreadsheetml.sheet"
                ]
            }
            vc = UIDocumentPickerViewController(documentTypes: types, in: .open)
        }
        
        vc.allowsMultipleSelection = true
        vc.modalPresentationStyle = .pageSheet // Use system-preferred style
        vc.delegate = self // Correct delegate assignment
        Self.completion = completion
        Self.instance = vc
        
        viewController.present(vc, animated: true)
    }
}

extension UIDocumentPickerViewController: UIDocumentPickerDelegate {
    public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        Self.completion?(urls)
        Self.completion = nil
    }
    
    public func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        Self.completion?(nil)
        Self.completion = nil
    }
}

Extra Troubleshooting Tips

  • Make sure your app has the iCloud Documents entitlement enabled (even for local file access, this can sometimes be required for the picker to function).
  • Test on a physical iOS 14 device instead of the simulator — simulator-specific bugs can cause unexpected UI failures.
  • Confirm you’re presenting the picker from a view controller that’s fully loaded and part of the active view hierarchy.

Give these fixes a shot — I’m confident this will resolve the display issue you’re seeing!

内容的提问来源于stack exchange,提问作者Eric Xu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 14:47:31