0

I looked at many SO question where the user wanted to access a document in his app directory. I am new to all of this and just want some clarification on the matter. I am building an open-source content blocker for iOS 9.

File tree

You can see the file tree. I wonder if it is possible to access the file called blockerList.json which is in the folder Adblock Content Blocker. My goal was to write the json file based on what the user want to build. I need to be able to modify the content of this file. Is this possible or should I stop trying and leave it like that?

Thanks for any help.

  • 2
    You would have to copy the file into the documents directory when the app is launched for the first time. You can't write to files in your app's bundle. – dan Aug 14 '15 at 15:08
  • It is not clear when you want to make changes to the file, at build time or after the app is installed on the iOS device? – zaph Aug 14 '15 at 15:17
  • I want to edit the file after the app is installed on the iOS device. – Justin Léger Aug 14 '15 at 15:22

2 Answers2

0

Note : You have readonly permission for application's bundle file.

  1. Firstly copy file in NSDocumentDirectory with backup attribute. Use refrerence copy file to document directory iphone and for How do I prevent files from being backed up to iCloud and iTunes?
  2. Check references ImportingJSONFile and Y6JSONFileManager-iOS to read and write changes in JSON File.
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
0

The app bundle is readonly as @dan said. So you have to copy the file from bundle into the Document directory then you able to modify the file at anytime.

Copy file into document like this:

class func copyFile(fileName: String) {
    let destPath: String = getPathInDocument(fileName)
    var fromPath = NSBundle.mainBundle().pathForResource("blockerList", ofType: "json")!

    var fileManager = NSFileManager.defaultManager()
    if !fileManager.fileExistsAtPath(destPath) {
        fileManager.copyItemAtPath(fromPath, toPath: destPath, error: nil)
    } else {
        println("The file allready exists at path:\(destPath)")
    }

}

class func getPathInDocument(fileName: String) -> String {
    return NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0].stringByAppendingPathComponent(fileName)
}

Hope this will help you!

Bannings
  • 10,376
  • 7
  • 44
  • 54
  • Will the modification affect the original json file? – Justin Léger Aug 14 '15 at 15:25
  • You cannot modify the original json file if it is in the bundle. Instead, you can modify the file in the Document directory. – Bannings Aug 14 '15 at 15:31
  • Do you think there is a way to include the json in the app so that it could be edited? In the app I just [load this json to make the content blocker](https://github.com/jusleg/OpenAdblock/blob/master/Open%20Adblock/Adblock%20Content%20Blocker/ActionRequestHandler.swift). Do think it would work If I generated the json inside the app and then load it? I am sorry for the noob questions, I am just starting iOS development. – Justin Léger Aug 14 '15 at 15:38
  • I don't think that is possible. Maybe this will helps you: http://stackoverflow.com/questions/12679448/does-an-ios-app-have-write-access-inside-its-bundle – Bannings Aug 14 '15 at 15:46
  • So it would be impossible to generate a file when the user open the app, modify the content of the .json based on what he wants to block? After that we could load the json and that would create the blocker – Justin Léger Aug 14 '15 at 16:02
  • Actually, you can generate a file into the Document directory and modify it. In the case, your generated file is read-write. – Bannings Aug 14 '15 at 16:11
  • I'll try that. Thanks for all the help. – Justin Léger Aug 14 '15 at 16:33