首页新增打开文件入口
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
import UniformTypeIdentifiers
|
||||||
|
|
||||||
class ViewController: UIViewController {
|
class ViewController: UIViewController {
|
||||||
|
|
||||||
@@ -21,7 +22,8 @@ class ViewController: UIViewController {
|
|||||||
[
|
[
|
||||||
MenuItem(icon: "note.text", iconColor: .systemYellow, title: "打开备忘录"),
|
MenuItem(icon: "note.text", iconColor: .systemYellow, title: "打开备忘录"),
|
||||||
MenuItem(icon: "doc.text", iconColor: .systemBlue, title: "格式化日志"),
|
MenuItem(icon: "doc.text", iconColor: .systemBlue, title: "格式化日志"),
|
||||||
MenuItem(icon: "text.alignleft", iconColor: .systemGreen, title: "格式化笔记")
|
MenuItem(icon: "text.alignleft", iconColor: .systemGreen, title: "格式化笔记"),
|
||||||
|
MenuItem(icon: "folder", iconColor: .systemOrange, title: "打开文件")
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -94,6 +96,8 @@ extension ViewController: UITableViewDelegate {
|
|||||||
case "格式化笔记":
|
case "格式化笔记":
|
||||||
let viewController = FormatNoteViewController()
|
let viewController = FormatNoteViewController()
|
||||||
navigationController?.pushViewController(viewController, animated: true)
|
navigationController?.pushViewController(viewController, animated: true)
|
||||||
|
case "打开文件":
|
||||||
|
openFiles()
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -104,4 +108,37 @@ extension ViewController: UITableViewDelegate {
|
|||||||
guard UIApplication.shared.canOpenURL(url) else { return }
|
guard UIApplication.shared.canOpenURL(url) else { return }
|
||||||
UIApplication.shared.open(url, options: [:], completionHandler: nil)
|
UIApplication.shared.open(url, options: [:], completionHandler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func openFiles() {
|
||||||
|
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.item], asCopy: true)
|
||||||
|
documentPicker.delegate = self
|
||||||
|
documentPicker.allowsMultipleSelection = false
|
||||||
|
present(documentPicker, animated: true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension ViewController: UIDocumentPickerDelegate {
|
||||||
|
|
||||||
|
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
|
||||||
|
guard let url = urls.first else { return }
|
||||||
|
let fileName = url.lastPathComponent
|
||||||
|
|
||||||
|
var message: String?
|
||||||
|
if let data = try? Data(contentsOf: url),
|
||||||
|
let text = String(data: data, encoding: .utf8),
|
||||||
|
!text.isEmpty {
|
||||||
|
message = text
|
||||||
|
} else {
|
||||||
|
let attributes = try? FileManager.default.attributesOfItem(atPath: url.path)
|
||||||
|
let size = attributes?[.size] as? Int64 ?? 0
|
||||||
|
message = "无法以文本预览\n大小: \(size) 字节"
|
||||||
|
}
|
||||||
|
|
||||||
|
let alert = UIAlertController(title: fileName, message: message, preferredStyle: .alert)
|
||||||
|
alert.addAction(UIAlertAction(title: "确定", style: .default))
|
||||||
|
present(alert, animated: true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user