簡體   English   中英

無法復制,因為您無權訪問ios中的應用程序包

[英]couldn’t be copied because you don’t have permission to access application bundle in ios

當我將文件從文件目錄復制到應用程序包目錄時,它會給出如下錯誤:

無法復制“圖像”,因為您無權訪問“資源”。

我的代碼

let fileManager = FileManager.default
let documentDirectoryPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("Images")
let bundleDirectoryPath = (Bundle.main.resourceURL?.appendingPathComponent("GCDWebUploader.bundle").appendingPathComponent("Contents").appendingPathComponent("Resources").appendingPathComponent("Images"))

do {
        try fileManager.copyItem(at: documentDirectoryPath!, to: bundleDirectoryPath!)
    } catch let error as NSError {
        print("Couldn't copy file to final location! Error:\(error.description)")
    }

為什么要復制到Bundle Dirctory ..沒有辦法在bundle目錄中寫入,因為bundle dir是使用SSL certificate簽名的代碼,你不能破壞它。 您可以使用文檔目錄本身來存儲文件。

磁盤上的bundle目錄( 在這里讀取)是代碼不可變的,即開發人員不能更改其內容。 應用程序的文檔目錄(沙箱)是您要保留的位置。

如果要將任何文件添加到bundle目錄,則必須在構建時使用構建階段選項卡中的復制資源完成。 這些通常是每次執行都不應更改的文件。

如果您嘗試添加到捆綁包的圖像是用於UI。 使用資產目錄

對於任何其他類型的文件(讀取或寫入),請使用文檔目錄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM