简体   繁体   English

保存到watchOS 2文件夹

[英]Saving to watchOS 2 document folder

My iOS app sends a database to WatchKit (watchOS 2) . 我的iOS应用程序将数据库发送到WatchKit (watchOS 2) In WatchKit i use the following code to get the watch document folder to save the new file in: WatchKit我使用以下代码获取监视文档文件夹以保存新文件:

NSURL* fileURL = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:@"MyDatabase.sqlite"];

This results in an URL: 这会产生一个URL:

@"file:///var/mobile/Containers/Data/PluginKitPlugin/47E13085-9030-4ED2-906F-01CBA323F07A/Documents/MyDatabase.sqlite"

However, when I call the function: 但是,当我调用该函数时:

[[NSFileManager defaultManager] replaceItemAtURL:fileURL withItemAtURL:url backupItemName:nil options:0 resultingItemURL:nil error:&error];

It throws an error saying: 它抛出一个错误说:

Error Domain=NSCocoaErrorDomain Code=512 "The file “MyDatabase.sqlite” couldn't be saved in the folder “Documents”." 错误域= NSCocoaErrorDomain代码= 512“文件”MyDatabase.sqlite“无法保存在”文档“文件夹中。” Error Domain=NSCocoaErrorDomain Code=513 "You don't have permission to save the file “MyDatabase.sqlite” 错误域= NSCocoaErrorDomain代码= 513“您无权保存文件”MyDatabase.sqlite“

How can I find a folder I'm allowed to save to? 如何找到我可以保存的文件夹? Naturally this code worked fine in the simulator so I assume I'm missing something not too obvious. 当然这个代码在模拟器中运行良好,所以我想我错过了一些不太明显的东西。

BR Frederik BR弗雷德里克

I believe your issue is that apple watch extensions can't save files to their documents dir. 我相信你的问题是Apple Watch扩展无法将文件保存到他们的文档目录中。 I was able to save files by using app Groups. 我可以使用应用程序组保存文件。 Add an app group to your entitlements and then you should be able to write to the following dir: 将应用程序组添加到您的权利中,然后您应该能够写入以下目录:

let path = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.my.cool.watch.app")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM