0

I am trying to parse data from a JSON file which was created from a .sh script that is executed via CLI prior to building/running the project.
The following snippet does not work since my file was created from the .sh script and never added to my application bundle.

func fetchDatadogClientId() -> String? {
        guard let url = Bundle.main.url(forResource: "client_token_key", withExtension: "json"),
              let data = try? Data(contentsOf: url),
              let datadogResponse = try? JSONDecoder().decode(DatadogResponse.self, from: data)
        else {
            return nil
        }
        
        return datadogResponse.clientToken
}

Would there be a way to read from this JSON file outside the application bundle or perhaps add the file to the project bundle using a script that would be run during the Build Phases?
Any suggestions are appreciated. Thanks in advance.

DanielRM
  • 636
  • 7
  • 24
  • If the .sh runs during the build phase you should be able to add the resulting JSON to the bundle so it ends up as part of the final build. – HangarRash Jan 27 '23 at 04:07
  • @HangarRash unfortunately the .sh is manually triggered via CLI. I couldn't include it in build phases since there was some authentication to do via a 3rd party lib (HashiCorp Vault) which proved problematic. – DanielRM Jan 27 '23 at 04:18
  • 1
    Just be sure to run the .sh before the build. Then the file will exist and you can include it as part of the build so it ends up in the app bundle. – HangarRash Jan 27 '23 at 04:25
  • @HangarRash thanks for your input. Would there be a way to automate this rather than manually going into Copy Bundle Resources and adding the file? I'm not sure this would work with our current CI setup – DanielRM Jan 27 '23 at 04:38
  • 2
    You could start with a dummy file, add it to the project, and ensure it's targeted. Then your .sh simply needs to update the file when needed so the next build picks up the change just like it would for any other changed file. A completely different option is not to bundle the token as part of the build. You could setup your app to access the key remotely from your server, for example. – HangarRash Jan 27 '23 at 04:40
  • 1
    See [How to store critically sensitive information such as secret, key, token, encryptionKey in iOS application](https://stackoverflow.com/questions/43111555/how-to-store-critically-sensitive-information-such-as-secret-key-token-encryp) for more ideas. – HangarRash Jan 27 '23 at 04:41
  • @HangarRash this would work pretty well. Thank you x1000 – DanielRM Jan 27 '23 at 05:18

0 Answers0