ios - 在捆绑资源 ios 离子目标 c 中打印时,Json 文件为空
问题描述
我正在运行一个ionic
项目的 ios构建app.xcproj
。xcode
我正在从以下位置的服务器下载 json 文件:/var/mobile/Containers/Data/Application/63E66EE9-9A1B-4D4D-AEF6-F8C54D159ED0/Library/NoCloud/MyApp/Timing/DTS.json
并且我通过打印检查了我有文件:
NSURL *urlDTS = [NSURL fileURLWithPath: DTS_Path];
NSString *fileContentDTS = [NSString stringWithContentsOfURL:urlDTS encoding:NSUTF8StringEncoding error:nil];
NSLog(@"Json data is here of DTS %@", fileContentDTS);
这可以打印整个文件,之后当我获得进一步操作的资源路径时,它会显示null
NSString *filePathDTS = [[NSBundle mainBundle] pathForResource:getParameterDTSValueName ofType:@"json"];
NSLog(@"This is the dts path %@", filePathDTS);
即使我已将文件位置检索为/var/mobile/Containers/Data/Application/63E66EE9-9A1B-4D4D-AEF6-F8C54D159ED0/Documents/DTS.json
通过以下链接:
NSString *documentdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *tileDirectory = [documentdir stringByAppendingPathComponent:@"xxxx/DTS"];
NSLog(@"Tile Directory: %@", tileDirectory);
更新
这是文件包含的内容:[{"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 is Dts contents [{"value":0}]
2018-06-11 17:04:40.940006+0500 Muslims 365[3356:819935] here is DTS data (null)
请指导我如何通过在主包中提供资源来从 json 文件中检索数据???因为它显示为空。
解决方案
pathForResource 是在应用程序包中获取路径的正确方法。请注意,此位置是只读的。您不能下载文件并将其保存在那里。pathForResource 仅访问您在编译期间拥有的文件,它返回 nil - 这意味着找不到文件。当你构建你的项目时,它会生成一个“bundle”——一个名为“MyApp.app”的文件夹。在 Xcode 中,您可以在左侧的“产品”组中看到它,您可以右键单击并在 Finder 中打开它,然后右键单击 - 显示内容 - 以查看里面的内容。通过这种方式,您可以可视化主包的结构。如果您的文件位于那里的子目录中,则需要使用pathForResource:ofType:inDirectory:。
容器内的“Library”文件夹是存储运行时数据的好地方。例如您下载的文件。这个位置是可写的,当你第一次启动应用程序时它是空的。如果您的包中有需要修改的文件,您可以将它从包中复制到“库”。请注意,如果您下载了一些可以稍后重新下载的文件,最好使用“Caches”(“Library”的子文件夹)。如果你来自 Windows 世界,“Library”的想法类似于“AppData”。
“文档”是用于存放用户可见的文件的位置,并且可以由用户编辑或管理。它也是可写的。您可以将其视为 macOS 上的“文档”或 Windows 上的“我的文档”,但仅适用于特定应用程序。
如果您的文件在“库”中,稍后您可以获取库的路径并将文件加载为字符串,如下所示:
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];
推荐阅读
- apollo - Apollo GraphQL 客户端:本地和远程状态模式
- delphi - TApplicationEvents 与 TIdTcpServer 问题?
- reactjs - Reactjs:this.handleEdit 不是函数
- javascript - TestCafe 有没有办法验证 Chrome 网络调用?
- python - Python中如何调整Pool的进程数
- spring - spring boot 2.1.3.RELEASE 发生非法反射访问操作
- javascript - 在 Puppeteer 中获取“语法错误:无效或意外令牌”
- google-cloud-platform - 如何提供对 Google SQL DB 的 Google Build 触发器访问?
- php - 如何自定义 Laravel HTTP 基本身份验证中的密码字段?
- shell - 在 shell 脚本中运行 ssh 命令后,如何在本地运行命令?