ios - 如何从我的 iphone 为我的应用程序浏览 pdf 文件,就像我能够通过 ibooks 浏览 pdf 文件一样?

标签 ios swift iphone ios12

就像使用 UIImagePickerViewController 浏览 UIIMage 一样。有没有办法在 Swift/目标代码中浏览另一个文件?

if let fileURL = NSBundle.mainBundle().URLForResource("MyImage", withExtension: "jpg") {
            // Instantiate the interaction controller
            self.docController = UIDocumentInteractionController(URL: fileURL)
        }
        else {
            shareButton.enabled = false
            print("File missing! Button has been disabled")
        }

最佳答案

您只能访问文档文件夹中的文件。访问所有文件

let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
do {
    let fileURLs = try fileManager.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil)
    // process files
} catch {
    print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")
}

如果您想要单个文件,请查看以下代码:

// Get the document directory url
let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

do {
    // Get the directory contents urls (including subfolders urls)
    let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: [])
    print(directoryContents)

    // if you want to filter the directory contents you can do like this:
    let mp3Files = directoryContents.filter{ $0.pathExtension == "mp3" }
    print("mp3 urls:",mp3Files)
    let mp3FileNames = mp3Files.map{ $0.deletingPathExtension().lastPathComponent }
    print("mp3 list:", mp3FileNames)

} catch {
    print(error.localizedDescription)
}

关于ios - 如何从我的 iphone 为我的应用程序浏览 pdf 文件,就像我能够通过 ibooks 浏览 pdf 文件一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54707909/

相关文章:

ios - 存储在序列化 View 中的子类 UILabel 不存储自定义变量

objective-c - 无法使用自定义颜色更改标签栏 tintColor

ios - 限制一次加载的单元格 Collection View 数量的方法

ios - 过滤 JSON 数据并打印

iphone - 将 CAShapeLayer 蒙版应用于 UIView

objective-c - 如何删除某些标点符号集并保留字符串中的其他标点符号?

iphone - iOS:可执行文件已使用无效的权利进行签名

ios - 仅写入 Swift SQLite 最后一个值

iphone - 连续/重复 MKMapView

iphone - Codesign 返回 xcode iphone 项目的退出代码 11 -- 找不到有关退出代码 11 的文档