支持外部 Markdown 文件预览与格式化分发
This commit is contained in:
@@ -20,6 +20,10 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
||||
let navigationController = UINavigationController(rootViewController: rootViewController)
|
||||
window?.rootViewController = navigationController
|
||||
window?.makeKeyAndVisible()
|
||||
|
||||
if let url = connectionOptions.urlContexts.first?.url {
|
||||
handleSharedFile(url)
|
||||
}
|
||||
}
|
||||
|
||||
func sceneDidDisconnect(_ scene: UIScene) {
|
||||
@@ -50,6 +54,38 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
||||
// to restore the scene back to its current state.
|
||||
}
|
||||
|
||||
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
|
||||
guard let url = URLContexts.first?.url else { return }
|
||||
handleSharedFile(url)
|
||||
}
|
||||
|
||||
private func handleSharedFile(_ url: URL) {
|
||||
guard url.isFileURL,
|
||||
let navigationController = window?.rootViewController as? UINavigationController else {
|
||||
return
|
||||
}
|
||||
|
||||
let shouldStopAccessing = url.startAccessingSecurityScopedResource()
|
||||
defer {
|
||||
if shouldStopAccessing {
|
||||
url.stopAccessingSecurityScopedResource()
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
let content = try String(contentsOf: url, encoding: .utf8)
|
||||
DispatchQueue.main.async {
|
||||
let viewController = PreviewViewController()
|
||||
viewController.initialText = content
|
||||
viewController.fileName = url.lastPathComponent
|
||||
viewController.title = url.lastPathComponent
|
||||
navigationController.pushViewController(viewController, animated: true)
|
||||
}
|
||||
} catch {
|
||||
print("Failed to read shared file: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user