简体   繁体   中英

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

When i am copy files from document directory to application bundle directory it gives error like this :

My code

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)")
    }

Why copying to Bundle Dirctory.. There is no way to write in the bundle directory because the bundle dir is code signed with SSL certificate and you can't break it. You can use the documents directory itself for storing files.

The bundle directory(Read here ) on disk is immutable by code, ie a developer cannot change its contents. The documents directory(sandbox) of your application is where you want to keep things.

If you want to add any files to the bundle directory, it must be done at build time using copy resources in the build phases tab. These are usually files that should not be changed for every execution.

If the images you are trying to add to the bundle are for UI. use an asset catalog

For any other type of file (reading or writing) use the documents directory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM