1

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

“Images” couldn’t be copied because you don’t have permission to access “Resources”.

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)")
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Bhumika
  • 876
  • 7
  • 20
  • Why do you want to move images to your bundle directory? – Puneet Sharma Feb 10 '17 at 06:08
  • I have implemented simple webserver with the use of GCDWebUploader.And Its done successfully.but now i want to display uploaded image from iphone to web.At that time i got issue of resource not found.So for that i got solution of copy resource.but doing that i am getting issue of permission. – Bhumika Feb 10 '17 at 06:27
  • Does this mean you have an image in your documents directory which you want to upload to external server(using GCDWebUploader)? If that is the case, why don't you show your upload code, how you are accessing image from sandbox? – Puneet Sharma Feb 10 '17 at 07:02
  • Are you know about GCDWebServer?see this http://stackoverflow.com/questions/29345909/what-is-gcdwebserver-for – Bhumika Feb 10 '17 at 07:28
  • I read about GCDWebserver on github. It is a neat library. Now I understand, you want to fetch image from your iOS app, acting as a web server. I have still not understood, why you want to copy the image file from documents directory to bundle directory. – Puneet Sharma Feb 10 '17 at 09:49
  • I don't want to copy file from document directory to application resource folder but while i am display image in web at that time i getting error in console like "Failed to load resource: the server responded with a status of 404".so i am trying to copy document directory to resource folder. but it is not possible. – Bhumika Feb 10 '17 at 10:21
  • So the problem is in implementation of GCDWebUploadServer. You must debug that or ask a separate question posting and explaining a bit of your code and I am sure someone who has worked on that framework will help you out. – Puneet Sharma Feb 10 '17 at 10:41
  • 1
    Yes, I have already asked question in GCDWebUploader Github issue.Thanks. – Bhumika Feb 10 '17 at 10:44

3 Answers3

0

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.

Saranjith
  • 11,242
  • 5
  • 69
  • 122
0

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

Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
-1

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

Shravya Boggarapu
  • 572
  • 1
  • 7
  • 23