Srinivasa Reddy
Srinivasa Reddy

Reputation: 77

Could not access the .json file of my cocoa framework in my iOS app

I added a .json file to my cocoa framework and imported this framework to my iOS app but could not access the .json file.

let url = Bundle.main.url(forResource: “filename”, withExtension: "json")
        do {
            let data = try Data(contentsOf: url!, options: .alwaysMapped)
            do {
                let object = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
} catch {
            }
        }
        catch {
        }

Here url is nil.

When I drag that .json file into my iOS app directory, it works well. So there is no issue with a .json format of the file.

Upvotes: 0

Views: 210

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You need to access it from the bundle of the framework not from the app main bundle

let bun = Bundle(identifier: <#frameworkBundleID#>)!
let url = bun.url(forResource: “filename”, withExtension: "json")

Upvotes: 1

Related Questions