spearman008
spearman008

Reputation: 87

Reading/Writing data that is accessible from iOS app extension

I'm working on an iOS Content Blocker, but I would like to have the user choose what lists they want to block (ads, tracks, adult sites etc.). I found that the app extension and the containing app's bundles are separate and have no access to each other's files, so a shared container is needed. I created an app group, but it seems like what I write there does not actually get written. What I am attempting to do is read a .json file from the bundle, and then write it to a sharedJson.json file that the content blocker extension can access.

func writeJsonToSharedJson(arrayOfStrings:[String]) -> Bool {
    let composedString = arrayOfStrings.joined(separator: "\n")
    let sharedJsonPath = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.alexspear.BlockerTestGroup")?.appendingPathComponent("sharedJson.json")
    //let sharedJsonPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("sharedJson.json")
    do {
        try composedString.write(to: sharedJsonPath!, atomically: true, encoding: .utf8)
    }
    catch {
        print("Could not write to sharedJson.json\n")
        return false
    }
    return verifyJsonWrite()
}

The result is that through the verifyJsonWrite() function, there is nothing there. Am I incorrect in assuming you can create a file in the app group container? I have also tried using FileManager's createFile function with the same result.

Upvotes: 1

Views: 780

Answers (0)

Related Questions