Mac Catalyst 下的 UIDocumentBrowserViewController 问题

问题描述 投票:0回答:1

UIDocumentBrowserViewController
显示白色空白屏幕,然后显示文本文件。

它开始发生在 macOS Sonoma 14.4.1 上。

func showDocumentPicker() {
    guard let dapa = UTType("com.my.dap.dapa"), let dapf = UTType("com.my.dap.dapf") else { return }
    
    let documentPicker = UIDocumentBrowserViewController(forOpening: [dapa, dapf])
    documentPicker.delegate = self
    documentPicker.allowsDocumentCreation = false
    documentPicker.allowsPickingMultipleItems = false
    
    // Create a close button
    let closeButton = UIBarButtonItem(barButtonSystemItem: .close, target: self, action: #selector(closeDocumentPicker))
    documentPicker.navigationItem.leftBarButtonItem = closeButton
    
    let navigationController = UINavigationController(rootViewController: documentPicker)
    present(navigationController, animated: true, completion: nil)
}

黑屏出现几秒钟:

swift mac-catalyst
1个回答
0
投票

文档向您的应用程序添加文档浏览器指出:

始终将文档浏览器指定为应用程序的根视图控制器。不要将文档浏览器放置在导航控制器、选项卡栏或拆分视图中,也不要以模态方式呈现文档浏览器。

您的代码违反了该要求,因为您尝试将文档浏览器放入导航控制器中并以模态方式显示它。

我可以毫无问题地创建一个 iOS 项目并让它显示

UIDocumentBrowserViewController
作为应用程序主窗口的根视图控制器。该浏览器在 iOS 设备上运行时可以运行,并且在 macOS 14.4.1 下通过 Mac Catalyst 在 Mac 上运行时也可以运行。我确信它也适用于其他版本,但这就是我目前拥有的。

以下是创建工作示例应用程序的步骤:

  1. Xcode -> 文件 -> 新建 -> 项目...

  2. 选择iOS应用程序。

  3. 为项目命名。选择 Swift 作为语言,选择 Storyboard 作为用户界面。接下来保存项目

  4. 删除主故事板。

  5. 删除 Info.plist 中对 Main 的引用

  6. 在目标构建设置中删除对 Main 的引用。

  7. 添加 Mac (Mac Catalyst) 并删除 Mac (Made for iPad)。

  8. 在签名和功能下,转到应用程序沙箱部分并向文件访问部分添加一些“只读”和/或“读/写”权限。

  9. 通过使用以下代码更新

    scene(_:willConnectTo:options:)
    方法来更新 SceneDelegate.swift 文件:

    guard let winScene = (scene as? UIWindowScene) else { return }
    
    let win = UIWindow(windowScene: winScene)
    let bvc = UIDocumentBrowserViewController(forOpening: [ .image ]) // Set whatever file types you need
    win.rootViewController = bvc
    win.makeKeyAndVisible()
    self.window = win
    

在 Mac 或 iOS 设备/模拟器上构建并运行以查看文档浏览器。

一旦开始工作,您就可以设置

delegate
以及文档浏览器的任何其他所需属性。

© www.soinside.com 2019 - 2024. All rights reserved.