正在获取无法打开文件“ xxx”,因为导入时您无权查看它

问题描述

我是使用Swift和SwiftUI的新手,我正在尝试编写一个iOS应用,该应用读取一个文件夹,计算每个文件的哈希值,然后将其复制到外部驱动器上。

目前,我正在尝试导入一个文件并计算其哈希值。但是,我总是收到相同的错误,说我无权查看它。

这是我的代码

    //
//  ContentView.swift
//  FileExporter
//
//  Created by adrien le falher on 27/09/2020.
//

import SwiftUI
import CryptoKit

struct ContentView: View {
    
    @State private var document: MessageDocument = MessageDocument(message: "Hello,World!")
    @State private var isImporting: Bool = false
    @State private var isExporting: Bool = false
    @State private var isMoving: Bool = false
    
    
    var body: some View {
        vstack {
            GroupBox(label: Text("Message:")) {
                TextEditor(text: $document.message)
            }
            GroupBox {
                HStack {
                    Spacer()
                    
                    Button(action: { isImporting = true },label: {
                        Text("Import")
                    })
                    
                    Spacer()
                    
                    Button(action: { isExporting = true },label: {
                        Text("Export")
                    })
                    
                    Spacer()
                    
                    Button(action: { isMoving = true },label: {
                        Text("Export")
                    })
                    
                    Spacer()
                }
            }
        }
        .padding()
        .fileExporter(
              isPresented: $isExporting,document: document,contentType: .plainText,defaultFilename: "Message"
          ) { result in
              if case .success = result {
                  // Handle success.
              } else {
                  // Handle failure.
              }
          }
        .fileImporter(
            isPresented: $isImporting,allowedContentTypes: [.image],allowsMultipleSelection: false
        ) { result in
            do {
                
                print("ok")
                guard let selectedFile: URL = try result.get().first else { return }
                guard var fileBytes : String = try hashFile(selectedFile) else { return }
                //guard let message = String(data: try Data(contentsOf: selectedFile),encoding: .utf8) else { return }
                let message = fileBytes
                print(message)
                
                document.message = message
                
            } catch let error{
                print(error.localizedDescription)
                document.message = error.localizedDescription
            }
        }
        
        
    }
}
extension Data {
    init(reading input: InputStream) throws {
        self.init()
        input.open()
        defer {
            input.close()
        }

        let bufferSize = 1024
        let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize)
        defer {
            buffer.deallocate()
        }
        while input.hasBytesAvailable {
            let read = input.read(buffer,maxLength: bufferSize)
            if read < 0 {
                //Stream error occured
                throw input.streamError!
            } else if read == 0 {
                //EOF
                break
            }
            self.append(buffer,count: read)
        }
    }
}


func hashFile (_ fileURL : URL)  -> String {
    print(fileURL)
    var hashed = ""
    
    do {
        var fileBytes =  try Data (contentsOf: fileURL)
        //OU reading : InputStream(url: fileURL)!
            print("Filebytes is \(fileBytes)")
        print("Bytes" + String(fileBytes.base64EncodedString()))
            let hashed = SHA256.hash(data: fileBytes)
            return String(hashed.description)
       
        } catch let error {
            hashed = error.localizedDescription
        }
    
    
            
    return hashed
    
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        /*@START_MENU_TOKEN@*/Text("Hello,World!")/*@END_MENU_TOKEN@*/
    }
}

.fileImporter()似乎是新的,我在查找有关它的信息时遇到了麻烦。我不知道是否(或如何)向iOS授予访问文件的权限;文件选择器可以正常工作,所以我不确定我做错了什么。

任何帮助将不胜感激。

谢谢。

解决方法

在读取文件内容之前,必须首先在URL上调用方法startAccessingSecurityScopedResource()注意:不要忘了在结束后致电stopAccessingSecurityScopedResource()

您可以在Apple documentation中找到更多信息。

编辑:

以下是对我有用的代码:

.fileImporter(
        isPresented: $isImporting,allowedContentTypes: [.plainText],allowsMultipleSelection: false
    ) { result in
        do {
            guard let selectedFile: URL = try result.get().first else { return }
            if selectedFile.startAccessingSecurityScopedResource() {
                guard let fileContent = String(data: try Data(contentsOf: selectedFile),encoding: .utf8) else { return }
                defer { selectedFile.stopAccessingSecurityScopedResource() }
            } else {
                // Handle denied access
            }
        } catch {
            // Handle failure.
            print("Unable to read file contents")
            print(error.localizedDescription)
        }
    }