我向iOS Target,Watchkit App Target和Watchkit App Extension Target添加了应用程序组权利. (这是应用程序组名称:group.com.lombax.fiveminutes)
然后,我尝试使用iOS App和WatchKit Extension访问共享文件夹URL:
延期:
@implementation ExtensionDelegate - (void)applicationDidFinishLaunching { // Perform any final initialization of your application. NSURL *test = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.lombax.fiveminutes"]; }
iOS应用:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. NSURL *test = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.lombax.fiveminutes"]; // ... }
但是,测试NSURL是不同的:
在iOS上:
file:///Users/Lombardo/Library/Developer/CoreSimulator/Devices/38B983DB-342F-4A47-8C26-5D2C92CDB666/data/Containers/Shared/AppGroup/8DEE182E-AFE6-47DD-BA2B-6B0520158A8B/
观看:
file:///Users/Lombardo/Library/Developer/CoreSimulator/Devices/BF52D613-25FF-4092-A5B2-9C3F1B616160/data/Containers/Shared/AppGroup/CECB5EFC-7FBD-4C84-A878-1314CB7CF211/
因此我无法在iOS App和WatchKit Extension之间共享数据.
因为我的Apple Watch上没有WatchOS 2.0,所以我可以尝试使用真正的设备.
任何建议?
谢谢
UPDATE
我做了一些其他测试:
>安装了WatchOS 2,问题仍然存在于真实设备上.
这是我iPhone的商店网址:
NSURL
* @”file:///private/var/mobile/Containers/Shared/AppGroup/3D05D159-94D6-409C-9A38-90E0830D0C3F/FiveMinutes.sqlite”
这是我的手表的商店网址:
NSURL
* @”file:///private/var/mobile/Containers/Shared/AppGroup/F1E89377-F456-4FC2-BAAC-3DD705EF381A/FiveMinutes.sqlite”
>在模拟器上,如果我对其中一个URL进行硬编码,iOS模拟器和Watch模拟器都能够读写相同的.sqlite文件并共享内容.但是,这在真实设备上是不可能的,因为Watch扩展无法写入iOS路径:
URL:file:///private/var/mobile/Containers/Shared/AppGroup/3D05D159-94D6-409C-9A38-90E0830D0C3F/FiveMinutes.sqlite options:(null) … returned error Error Domain=NSCocoaErrorDomain Code=512 “The file couldn’t be saved.” UserInfo={reason=Failed to create file; code = 2} with userInfo dictionary {
reason = “Failed to create file; code = 2”;
}
解决方法
我做的第一件事是创建一个新项目,从一个基础iOS项目开始,然后添加一个Watch OS 1(旧版本)App Target.
在这种情况下,目录是相同的,他们可以沟通:
Watch Path: file:///Users/Lombardo/Library/Developer/CoreSimulator/Devices/BF52D613-25FF-4092-A5B2-9C3F1B616160/data/Containers/Shared/AppGroup/30B39103-CEEB-4C64-9531-FB27DC40180D/
iOS Path file:///Users/Lombardo/Library/Developer/CoreSimulator/Devices/BF52D613-25FF-4092-A5B2-9C3F1B616160/data/Containers/Shared/AppGroup/30B39103-CEEB-4C64-9531-FB27DC40180D/
然后,我做了每个程序员应该做的第一件事:阅读文档.
在WatchOS 2过渡指南的第一页中有这样一句话:
Your extension now stores files and data on Apple Watch. Any data that is not part of your Watch app or WatchKit extension bundle must be fetched from the network or from the companion iOS app running on the user’s iPhone. You cannot rely on a shared group container to exchange files with your iOS app. Fetching files involves transferring them wirelessly to Apple Watch.