ios - 如何在 SwiftUI Mac 应用程序的帮助菜单中显示用户指南?
问题描述
我编写了一个 SwiftUI 应用程序并将其发布在 Mac App Store 上。我还编写了详细的用户指南和各种技术指南,以帮助用户了解如何使用该应用程序并解释其显示。所有这些 .md 文件都包含在我的应用程序中。如何允许帮助菜单打开和显示这些文件?
解决方案
我不知道其他人是如何做到这一点的,但这是我会使用的方法:
import SwiftUI
@main
struct MacApp: App {
@State var showHelp = false
var body: some Scene {
WindowGroup {
ContentView()
.sheet(isPresented: self.$showHelp) { HelpView() }
}.commands {
CommandGroup(replacing: .help) {
Button(action: {showHelp = true}) {
Text("App Help")
}
}
}
}
}
对于 iOS:
@main
struct IOSApp: App {
@State var showHelp = false
var body: some Scene {
WindowGroup {
ContentView(showHelp: $showHelp)
.sheet(isPresented: self.$showHelp) { HelpView() }
}
}
}
传递showHelp
(或作为ObservableObject/EnvironmentObject
),然后设置showHelp=true
,例如单击按钮时Image(systemName: "info.circle")
。
struct HelpView: View {
@Environment(\.dismiss) var dismiss
@State var mdText = AttributedString()
var body: some View {
VStack (alignment: .leading, spacing: 20) {
Button(action: {dismiss()}) {
Text("Done").foregroundColor(.blue)
}.padding(10)
ScrollView {
Text("Welcome to my app help")
Text(mdText)
}.frame(width: 333, height: 333)
}
.onAppear {
// get your .md files contents
do {
mdText = try AttributedString(markdown: "***help file contents***")
} catch {
// error
}
}
}
}
EDIT1:读取md
您的应用程序中包含的文件。
.onAppear {
var str: String = ""
if let mdFile = Bundle.main.path(forResource: "README", ofType: "md") {
do {
str = try String(contentsOfFile: mdFile)
mdText = try AttributedString(markdown: str)
} catch {
// deal with error
}
}
}
EDIT2: ios 应用程序的内容视图:
struct ContentView: View {
@Binding var showHelp: Bool // <--- here
var body: some View {
Button(action: {showHelp = true}) {
VStack {
Image(systemName: "info.circle").resizable().frame(width: 60, height: 60)
Text("Info").font(.caption)
}.foregroundColor(Color.blue)
}
}
}
推荐阅读
- javascript - 谷歌应用脚本从电子表格中读取不同的时区
- c# - 如果 GetEnumerator() 的返回类型不同,IEnumerable.GetEnumerator() 的首选实现是如何返回 GetEnumerator()?
- windows - Cabinet文件的AuthentiCode结构
- git - 如何通过 curl 将提交推送到 git 存储库?
- python - 使用 to_frame() 后如何获取行的 ID
- php - How to make an address Default from multiple addresses in PHP
- google-cloud-platform - Google Cloudrun 上的 Apache Tika Docker 映像(堆大小问题)
- python - sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)无法适应类型“电影”
- asp.net - HTML选择在Firefox中刷新后不显示选定的选项
- python - 如何将 Pyinstaller 添加到路径?