Search code examples
iosobjective-cionic-frameworkfilepathmainbundle

Json file is null when prints in bundle resource ios ionic objective c


I am running an ionic project's ios build app.xcproj in xcode. I am downloading json files from server in the following location: /var/mobile/Containers/Data/Application/63E66EE9-9A1B-4D4D-AEF6-F8C54D159ED0/Library/NoCloud/MyApp/Timing/DTS.json and I have checked I have files by printing:

NSURL *urlDTS = [NSURL fileURLWithPath: DTS_Path];
NSString *fileContentDTS = [NSString stringWithContentsOfURL:urlDTS encoding:NSUTF8StringEncoding error:nil];
NSLog(@"Json data is here of DTS  %@", fileContentDTS);

This prints whole file ok, after that when i get the resource path for further operations it shows null

NSString *filePathDTS = [[NSBundle mainBundle] pathForResource:getParameterDTSValueName ofType:@"json"];
NSLog(@"This is the dts path %@", filePathDTS);

Even I have retrieved the files location as /var/mobile/Containers/Data/Application/63E66EE9-9A1B-4D4D-AEF6-F8C54D159ED0/Documents/DTS.json

by following this Link:

NSString *documentdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *tileDirectory = [documentdir stringByAppendingPathComponent:@"xxxx/DTS"];
NSLog(@"Tile Directory: %@", tileDirectory);

Update

This is what file contains: [{"value":0}]

NSURL *libraryDirURL = [[NSFileManager.defaultManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *urlDTSK = [libraryDirURL URLByAppendingPathComponent:@"NoCloud/MyApp/MyFolder/DTS.json"];
NSString *filePathDTS = [NSString stringWithContentsOfURL:urlDTSK encoding:NSUTF8StringEncoding error:nil];
NSLog(@"This is Dts PATH %@", filePathDTS);
NSData *dataDTS = [NSData dataWithContentsOfFile:filePathDTS];
NSLog(@"here is DTS data  %@", dataDTS); //this shows null
NSDictionary *jsonDTS = [NSJSONSerialization JSONObjectWithData:dataDTS options:kNilOptions error:nil];
NSLog(@"here is jason DTS %@", jsonDTS);
NSMutableArray *DTSvalue = [jsonDTS valueForKeyPath: @"Value"];
DTSValueIs = DTSvalue[0];
NSLog(@"here is DTS Value first%@", DTSvalue[0]);
NSLog(@"here is DTS value is%@", DTSValueIs);

This shows This is Dts contents [{"value":0}] 2018-06-11 17:04:40.940006+0500 Muslims 365[3356:819935] here is DTS data (null)

Please guide me how can i retrieve data from json file by providing resource in main bundle??? as it shows null.


Solution

  • pathForResource is the right way to get a path inside your app bundle. Note that this location is READONLY. You can not download a file and save it there. pathForResource is only accessing files that you have during compilation, and it returns nil - it means that a file is not found. When you build your project, it produces a "bundle" - a folder named "MyApp.app". In Xcode you can see it in "Products" group on the left, and you can right-click and open this in Finder, then right-click - show contents - to see what's inside. This way you can visualize the structure of you main bundle. If your file is inside a subdirectory there, you need to use pathForResource:ofType:inDirectory:.

    "Library" folder inside the container is a good place to store runtime data. For example files that you download. This location is WRITABLE, and it is empty when you start the app for the first time. If you have a file in your bundle that you need to modify, you can copy it from the bundle to "Library". Note that if you download some files that can be re-downloaded again later, it is better to use "Caches" (a subfolder of "Library"). If you come from the Windows world, the "Library" idea is similar to "AppData".

    "Documents" is a location meant for files that are visible by the user, and can be edited or managed by the user. It is also writable. You can think of it as of "Documents" on macOS, or "My Documents" on Windows, but just for a particular app.

    If your file is in the "Library", later on you can get the path of library and load the file as string like so:

    NSURL *libraryDirURL = [[NSFileManager.defaultManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
    NSURL *urlDTS = [libraryDirURL URLByAppendingPathComponent:@"NoCloud/MyApp/MyFolder/DTS.json"];
    NSString *fileContentDTS = [NSString stringWithContentsOfURL:urlDTS encoding:NSUTF8StringEncoding error:nil];