0

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.

iOS Developer
  • 311
  • 4
  • 25

1 Answers1

0

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];
battlmonstr
  • 5,841
  • 1
  • 23
  • 33
  • so what should i do so i can get the data from json from main bundle? – iOS Developer Jun 09 '18 at 10:55
  • 1
    Make sure that it is inside the bundle. I describe how to do it in the first paragraph - open MyApp.app and see if it is there. If not - you need to mark it as a resource in Xcode. (google: copy resources build phase) – battlmonstr Jun 09 '18 at 10:57
  • 1
    Add the file into the project, and then drag it [here](https://cdn-images-1.medium.com/max/800/1*8T5E1yUEtei3iD5WbV9rYQ.png) – battlmonstr Jun 09 '18 at 11:05
  • but i can not add the files as i am downloading can you give me an working example with code plz – iOS Developer Jun 09 '18 at 11:22
  • 1
    You can't download into the bundle, it is READONLY! What are you trying to do? – battlmonstr Jun 09 '18 at 11:24
  • I am downloading four json files, they are downloaded here at : /var/mobile/Containers/Data/Application/63E66EE9-9A1B-4D4D-AEF6-F8C54D159ED0/Library/NoCloud/MyApp/MyFolder/DTS.json – iOS Developer Jun 09 '18 at 11:28
  • 1
    To get this path later on, you need to get the path to "Library" and then append "NoCloud/MyApp/MyFolder/DTS.json" to it. – battlmonstr Jun 09 '18 at 11:31
  • so how can i define my bundle path??? like NSString *filePathDTS = [[NSBundle mainBundle] pathForResource:@"DTS":@"json"]; – iOS Developer Jun 09 '18 at 11:36
  • 1
    Forget about the bundle. Bundle != Library. Get the path to the library [like this](https://stackoverflow.com/questions/3762941/getting-an-ios-applications-library-path-reliably) and then append "NoCloud/MyApp/MyFolder/DTS.json". – battlmonstr Jun 09 '18 at 13:03
  • it retrieves the same path after appending that is `/var/mobile/Containers/Data/Application/EF9E024B-B2D4-41E7-9248-E56E1643B838/Library/NoCloud/MyApp/My_Folder/DTS.json` – iOS Developer Jun 11 '18 at 08:33
  • 1
    Of course, if you haven't move the file it should still be there at the same path and readable by stringWithContentsOfURL. – battlmonstr Jun 11 '18 at 08:36
  • dear but when i process further i mean when i define at bundle main path the file name it gives null how should i resolve this – iOS Developer Jun 11 '18 at 08:38
  • 1
    You can not use "[NSBundle mainBundle] pathForResource" for reading "Library". You use pathForResource for reading the "bundle", and you use some other functions (like stringWithContentsOfURL) for reading inside the "Library". – battlmonstr Jun 11 '18 at 08:42
  • please help me how should i replace this line `NSString *filePathDTS = [[NSBundle mainBundle] pathForResource:DTSValuePathOkName ofType:@"json"];` this line gives me null how can i replace it as `pathForResource` – iOS Developer Jun 11 '18 at 08:49
  • it works until the contents and then shows null as i have mentioned in in detail please see the update of question in last section – iOS Developer Jun 11 '18 at 09:51
  • stringWithContentsOfURL already gives you contents, if you need NSData, use `[NSData dataWithContentsOfURL:urlDTSK];` instead of your lines 3-5 – battlmonstr Jun 11 '18 at 11:52