可以通过以下步骤实现:
let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text"], in: .import)
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
extension ViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let fileURL = urls.first else {
return
}
// 在这里可以对选中的文本文件进行处理,比如读取文件内容或进行其他操作
// 例如,可以使用以下代码读取文本文件内容并显示在TextView中
if let fileContent = try? String(contentsOf: fileURL) {
textView.text = fileContent
}
}
}
这样,当用户点击按钮并选择一个文本文件后,文本文件的内容将会显示在TextView中。
对于这个问题,腾讯云没有特定的产品或链接与之相关。
领取专属 10元无门槛券
手把手带您无忧上云