如何在不启动文件选择器的情况下制作SwiftUI DocumentGroup应用程序



如果用户使用Xcode中的文档应用程序模板创建SwiftUI应用程序,macOS会用一个新文档启动它们。这很好。我可以使用它在新文档中显示入职UI。

然而,在iOS上运行相同的应用程序时,用户会收到股票文档视图控制器的欢迎,以创建或选择文档。

这没有帮助,因为我没有办法提供入职或任何其他自定义信息。

我确实注意到,如果您将WindowGroup添加到Scene,应用程序将显示该窗口组。但是我不知道如何让用户使用picker UI。

有人想过如何在这个基于DocumentGroup的应用程序上进行演示等操作吗?

这是一个完整的文档应用

import SwiftUI
import UniformTypeIdentifiers
@main
struct DocumentTestApp: App {
var body: some Scene {
DocumentGroup(newDocument: DocumentTestDocument()) { file in
ContentView(document: file.$document)
}
}
}
struct ContentView: View {
@Binding var document: DocumentTestDocument
var body: some View {
TextEditor(text: $document.text)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(document: .constant(DocumentTestDocument()))
}
}
extension UTType {
static var exampleText: UTType {
UTType(importedAs: "com.example.plain-text")
}
}
struct DocumentTestDocument: FileDocument {
var text: String
init(text: String = "Hello, world!") {
self.text = text
}
static var readableContentTypes: [UTType] { [.exampleText] }
init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents,
let string = String(data: data, encoding: .utf8)
else {
throw CocoaError(.fileReadCorruptFile)
}
text = string
}

func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
let data = text.data(using: .utf8)!
return .init(regularFileWithContents: data)
}
}

好吧,朋友们,这里有一个很好的方法,可以让事情顺利进行,进入关键窗口,设置入职/付费墙/任何你想要的!

import SwiftUI
@main
struct ExampleApp: App {
@StateObject var captain = Captain()

var body: some Scene {
DocumentGroup(newDocument: ExampleOfDocumentGroupAndOnboardingPaywallDocument()) { file in
ContentView(document: file.$document)
}
}
}
class Captain: ObservableObject {
var onboardingSheet: UIViewController?
@objc func loadData() {
onboardingSheet = try? OnboardingOrPaywall(dismissHandler: dismissSheet).presentFromDocumentGroup()
}

func dismissSheet() {
onboardingSheet?.dismiss(animated: true)
}

init() {
NotificationCenter.default.addObserver(self,
selector: #selector(loadData),
name: UIApplication.didBecomeActiveNotification,
object: nil)
}
}
public protocol DocumentGroupSheet: View {}
struct OnboardingOrPaywall: DocumentGroupSheet {
var dismissHandler: () -> Void
var body: some View {
Button("Done") {
dismissHandler()
}
Text("Let me introduce you to this delicious app!")
}
}
public enum DocumentGroupSheetError: Error {

case noParentWindow
}
public extension DocumentGroupSheet {

func presentFromDocumentGroup() throws -> UIViewController  {
let window = UIApplication.shared.activeKeyWindows.first
let parent = window?.rootViewController
guard let parent = parent else { throw DocumentGroupSheetError.noParentWindow }
let sheet = UIHostingController(rootView: body)
sheet.modalPresentationStyle = .fullScreen
parent.present(sheet, animated: false, completion: nil)
return sheet
}
}
public extension UIApplication {

var activeWindowScenes: [UIWindowScene] {
connectedScenes
.filter { $0.activationState == .foregroundActive }
.compactMap { $0 as? UIWindowScene }
}

var activeWindows: [UIWindow] {
activeWindowScenes
.flatMap { $0.windows }
}
var activeKeyWindows: [UIWindow] {
activeWindows
.filter { $0.isKeyWindow }
}
}

从iOS 16开始,通过这里的文档,这似乎是可能的。

@main
struct Mail: App {
var body: some Scene {
WindowGroup(id: "mail-viewer") {
MailViewer()
}
}
}

struct NewViewerButton: View {
@Environment(.openWindow) private var openWindow

var body: some View {
Button("Open new mail viewer") {
openWindow(id: "mail-viewer")
}
}
}

因此,当场景出现时,您可以打开入职窗口。

默认情况下,应用程序显示第一个窗口场景,因此先放置登机窗口场景,然后放置DocumentGroup。在入职流程(成功路径(结束时的某个地方,调用文档控制器来创建新文档(DocumentGroup内部基于相同的NSDocumentController引擎(。

更新:下面是针对macOS的

*刚刚认识到最初的问题是针对iOS 的

因此一种可能的方法是

@main
struct DocumentTestApp: App {
var body: some Scene {
WindowGroup("On-Boarding") {
// ContentView()
// In some action at the end of this scene flow
// just close current window and open new document
Button("Demo") {
NSApp.sendAction(#selector(NSWindow.performClose(_:)), to: nil, from: nil)
NSDocumentController.shared.newDocument(nil)
}
}
DocumentGroup(newDocument: DocumentTestDocument()) { file in
ContentView(document: file.$document)
}
}
}

相关内容

最新更新