hide .json files when included in Cocoa Touch Framework hide .json files when included in Cocoa Touch Framework json json

hide .json files when included in Cocoa Touch Framework


There are many ways:

  • You can encrypt that file end decrypt again before using.
  • Change json file type to picture or something else to make sure no one know this is JSON file.


You can add json file content to sources as base64 encoded string.

struct JSONFiles {    internal static let fileOneBase64String = "iVBORw0KGgoAAAANSUhEUgAAAMEAAADLCAYAAADX..."    static var fileOneJSON: [String: Any] {        let data = Data(base64Encoded: fileOneBase64String)!        return try! JSONSerialization.jsonObject(with: data, options: []) as!  [String: Any]    }}

You can use it later anywhere in you framework by

print(JSONFiles.fileOneJSON)

Hope it help you

UPDATE:Why as base64 encoded string?

The main benefit base64 encoded string - you don't need to escape special symbols contained in JSON string. Don't need to remove new line symbols/intendation symbols from json string before add to swift source file. See example

let string: String = "{ \"name\":\"John\", \"email\": \"hello@stackoverflow\" \"age\":30, \"car\":null }"

But if json is more complex? If it contains 10 nested levels, arrays, dictionaries?

You can cover by UnitTests that value contained in fileOneBase64String can be decoded to JSON object

import XCTest@testable import MyAwesomeFrameworkclass FrameworkTests: XCTestCase {    func testThatBase64EncodedJSONStringCanBeDecoded() {        let data = Data(base64Encoded: JSONFiles.fileOneBase64String)        XCTAssertNotNil(data, "Unable to convert base64 string to Data")        do {            _ = try JSONSerialization.jsonObject(with: data, options: [])        } catch {            XCTFail("Expected decoded JSON ")        }    }}