打开文件改为跳转系统文件应用
This commit is contained in:
@@ -4,6 +4,11 @@
|
|||||||
<dict>
|
<dict>
|
||||||
<key>ITSAppUsesNonExemptEncryption</key>
|
<key>ITSAppUsesNonExemptEncryption</key>
|
||||||
<false/>
|
<false/>
|
||||||
|
<key>LSApplicationQueriesSchemes</key>
|
||||||
|
<array>
|
||||||
|
<string>mobilenotes</string>
|
||||||
|
<string>shareddocuments</string>
|
||||||
|
</array>
|
||||||
<key>UIApplicationSceneManifest</key>
|
<key>UIApplicationSceneManifest</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>UIApplicationSupportsMultipleScenes</key>
|
<key>UIApplicationSupportsMultipleScenes</key>
|
||||||
|
|||||||
+10
-36
@@ -6,7 +6,6 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
import UniformTypeIdentifiers
|
|
||||||
|
|
||||||
class ViewController: UIViewController {
|
class ViewController: UIViewController {
|
||||||
|
|
||||||
@@ -97,48 +96,23 @@ extension ViewController: UITableViewDelegate {
|
|||||||
let viewController = FormatNoteViewController()
|
let viewController = FormatNoteViewController()
|
||||||
navigationController?.pushViewController(viewController, animated: true)
|
navigationController?.pushViewController(viewController, animated: true)
|
||||||
case "打开文件":
|
case "打开文件":
|
||||||
openFiles()
|
openFilesApp()
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func openSystemNotesApp() {
|
private func openSystemNotesApp() {
|
||||||
guard let url = URL(string: "mobilenotes://") else { return }
|
openURLIfPossible("mobilenotes://")
|
||||||
|
}
|
||||||
|
|
||||||
|
private func openFilesApp() {
|
||||||
|
openURLIfPossible("shareddocuments://")
|
||||||
|
}
|
||||||
|
|
||||||
|
private func openURLIfPossible(_ string: String) {
|
||||||
|
guard let url = URL(string: string) else { return }
|
||||||
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