尝试使用 FileManager 复制文件时出错:不允许操作

如何解决尝试使用 FileManager 复制文件时出错:不允许操作

我是 iOS 中 FileManager 的新手。我正在尝试创建一个简单的应用程序:有一个文件列表,当用户按下一个单元格时,文件就会打开。用户还可以添加文件

不久前我 asked a question 关于我遇到的另一个错误,我得到了一个 answer that work on a simulator,但它在真实设备上不起作用!

我的代码:(错误storeAndShare()

typealias UrlLink = String

extension UrlLink {
var url: URL {
    var url = try! FileManager.default.url(for: .documentDirectory,in: .userDomainMask,appropriateFor: nil,create: true)
    url.appendpathComponent(self)
    return url
}
}

class TableViewController: UITableViewController,UIDocumentPickerDelegate {

var names: [UrlLink] = []

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.cleaRSSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
     self.navigationItem.rightBarButtonItem = self.editButtonItem
}

func urlForFilename(_ filename: String) -> URL {
    var url = try! FileManager.default.url(for: .documentDirectory,create: true)
    url.appendpathComponent(filename)
    return url
}

@IBAction func newButtonpressed(_ sender: Any) {
    let types = UTType(tag: "pdf",tagClass: .filenameExtension,conformingTo: nil)!
    let controller = UIDocumentPickerViewController(foropeningContentTypes: [types])
    controller.delegate = self
    self.present(controller,animated: true,completion: nil)
}

func documentPicker(_ controller: UIDocumentPickerViewController,didPickDocumentsAt urls: [URL]) {
    for url in urls {
        self.storeAndShare(withURL: url)
    }
}

// MARK: - Table view data source

override func tableView(_ tableView: UITableView,commit editingStyle: UITableViewCell.EditingStyle,forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        let removedname = self.names.remove(at: indexPath.row)
        do {
            try FileManager.default.removeItem(atPath: removedname.url.path)
        } catch let err {
            print("Error while trying to remove the file \(err)")
        }
        
        tableView.deleteRows(at: [indexPath],with: .automatic)
    }
}

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation,return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation,return the number of rows
    return names.count
}

override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .default,reuseIdentifier: nil)

    cell.textLabel?.text = names[indexPath.row]
    
    if FileManager.default.fileExists(atPath: names[indexPath.row].url.path) {
        cell.accessoryType = .disclosureIndicator
    } else {
        cell.accessoryType = .none
    }

    return cell
}

lazy var documentInteractionController: UIDocumentInteractionController = {
    let vc = UIDocumentInteractionController()
    vc.delegate = self
    return vc
}()

func getSelf() -> TableViewController {
    return self
}

func share(url: URL) {
    documentInteractionController.url = url
    documentInteractionController.uti = url.typeIdentifier ?? "public.data,public.content"
    documentInteractionController.name = url.localizedname ?? url.lastPathComponent
    documentInteractionController.presentPreview(animated: true)
}

override func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
    share(url: self.names[indexPath.row].url)
}

func storeAndShare(withURL url: URL) {

    let directoryURL = try! FileManager.default.url(for: .documentDirectory,create: true)
    var newURL = directoryURL.appendingPathComponent(url.lastPathComponent)
    
    var index = 1
    let originalNewURL = newURL
    while FileManager.default.fileExists(atPath: newURL.path) {
        newURL = originalNewURL
        let comp: Nsstring = newURL.lastPathComponent as Nsstring
        newURL.deleteLastPathComponent()
        let newName: String = "\(comp.deletingPathExtension) \(index).\(comp.pathExtension)"
        newURL.appendpathComponent(newName)
        index = index + 1
    }
    
    do {
        try FileManager.default.copyItem(at: url,to: newURL)
        dispatchQueue.main.async { [self] in
            names.append(newURL.lastPathComponent)
            tableView.insertRows(at: [IndexPath(row: names.count - 1,section: 0)],with: .automatic)
        }
    } catch {
        print("ERROR: \(error).") // ERROR: Error Domain=NSCocoaErrorDomain Code=257 "The file “I am a file and you put documents in me.pdf” Couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/תיקיות/I am a file and you put documents in me.pdf,NSUnderlyingError=0x281ab2790 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}.
    }
}

}

extension TableViewController: UIDocumentInteractionControllerDelegate {
/// If presenting atop a navigation stack,provide the navigation controller in order to animate in a manner consistent with the rest of the platform
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
    guard let navVC = self.navigationController else {
        return self
    }
    return navVC
}
}
extension URL {
var typeIdentifier: String? {
    return (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier
}
var localizedname: String? {
    return (try? resourceValues(forKeys: [.localizednameKey]))?.localizedname
}
}

基本上,当用户添加文件时,我的应用会尝试将其复制到文档文件夹中,以便将其存储在那里。

尝试将所选文件复制到文档文件夹时出现以下错误

Error Domain=NSCocoaErrorDomain Code=257 “无法打开文件“我是一个文件,你把文档放在 me.pdf 中”,因为你没有查看它的权限。 UserInfo={NSFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/תיקיות/我是一个文件,你把文档放在me.pdf,NSUnderlyingError=0x281ab2790 {Error Domain=NSPOSIXErrorDomain Code=1”不允许操作"}}

我好像没有查看文件的权限。但这很奇怪,因为我刚刚选择了带有 UIDocumentInteractionController文件,而且正如我之前所说,它在模拟器上运行良好。

我如何获得复制文件的权限?提前致谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?