iOS:使用C / C++将文件写入文档文件夹,并使用Files/ iTunes或第三方工具轻松检索



我咨询过这个问题:

使用 C/C++ 在 iOS 中写入文件

它的答案是:

https://stackoverflow.com/a/39022407/987846

我似乎已将文件写入磁盘,即所有文件打开和写入完成。但是,当我希望看到写入磁盘的文件出现在iOS上的 Files.app 或macOS上的iTunes中时,我在那里找不到。特别是,我的应用程序没有出现在iTunes的文件共享类别中。

我还尝试了第三方工具,例如:

https://github.com/ios-control/ios-deploy

调用其文件下载命令给我一个空文件夹

./ios-deploy --download=/Documents --bundle_id com.mycompany.myapp -2 dest_dir

那么关闭应用程序后我应该如何检索该文件?

好吧,我自己想通了。

我需要通过键入新行将目标属性添加到我的Info.plist

UIFileSharingEnabled

或直接从属性列表中选择

Application supports iTunes file sharing

使值YES。 然后我的应用程序将显示在iTunesFile Sharing类别中。 保存到"文档"文件夹中的文件将显示在那里。

为了完整起见,下面是可以插入到 ObjC 的项目中的 ObjC/C 代码片段。


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingPathComponent:@"file.ext"];
const char* filePath = [file UTF8String];
FILE* pFile = NULL;
pFile = fopen(filePath, "ab");
float data = 9.9f;
fwrite(&data, sizeof(float), 1, pFile);    
fclose(pFile);
}

最新更新