ContainerURLForSecurityApplicationGroupIdentifier: gives different results on iPhone simulator and Watch

I created a WatchKit app with a default Xcode template. I added permissions to the iOS Target app group, the Target App App App Target, and the Watchkit app extension app. (this is the name of the application group: group.com.lombax.fiveminutes) Then I tried to access the URL of the public folder using the iOS application and the WatchKit extension:

Extension:

@implementation ExtensionDelegate - (void)applicationDidFinishLaunching { // Perform any final initialization of your application. NSURL *test = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.lombax.fiveminutes"]; } 

IOS app:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. NSURL *test = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.lombax.fiveminutes"]; // ... } 

However, test NSURL is different:

In iOS:

File: /// Users / Lombardo / Library / Developer / CoreSimulator / Devices / 38B983DB-342F-4A47-8C26-5D2C92CDB666 / data / Containers / Shared / AppGroup / 8DEE182E-AFE6-47DD-BA2B-6B0520158A8B /

on Watch:

File: /// Users / Lombardo / Library / Developer / CoreSimulator / Devices / BF52D613-25FF-4092-A5B2-9C3F1B616160 / data / Containers / Shared / AppGroup / CECB5EFC-7FBD-4C84-A878-1314CB7CF211 /

And for this reason, I cannot exchange data between the iOS application and the WatchKit extension.

I am trying to run a real device since I do not have WatchOS 2.0 on my Apple Watch. Any advice? Thanks

UPDATE I did some other tests:

  • Installed by WatchOS 2, the problem still persists on real devices.

This is the store address for my iPhone:

NSURL * @ "file: ///private/var/mobile/Containers/Shared/AppGroup/3D05D159-94D6-409C-9A38-90E0830D0C3F/FiveMinutes.sqlite"

And this is the store address for my Watch:

NSURL * @ "file: ///private/var/mobile/Containers/Shared/AppGroup/F1E89377-F456-4FC2-BAAC-3DD705EF381A/FiveMinutes.sqlite"

Two applications read and write to two different .sqlite .

  • In the simulator, if I hard-code one of the URLs, both the iOS simulator and the Watch simulator can read and write the same .sqlite file and share the contents. However, this is not possible on real devices, since the Watch extension cannot be written to the iOS path:

URL: file: ///private/var/mobile/Containers/Shared/AppGroup/3D05D159-94D6-409C-9A38-90E0830D0C3F/FiveMinutes.sqlite parameters: (null) ... returned error Domain error = code NSCocoaErrorDomain = 512 " File cannot be saved. " UserInfo = {reason = Failed to create file; code = 2} using the dictionary userInfo {reason = "Could not create file; code = 2"; }

+7
ios iphone ios-simulator watchkit apple-watch
source share
1 answer

Ok, I think I found my answer. I recalled that with the switch to Watch OS 2, the extension code now runs directly on the Apple Watch, rather than on a paired iPhone. Therefore, it seems obvious that the two devices do not use the same storage.

The first thing I did was create a new project, starting with the base iOS project, and then add the App Watch 1 application (old version). In this case, the directories were identical, and they could exchange data:

Monitoring path: file: /// Users / Lombardo / Library / Developer / CoreSimulator / Devices / BF52D613-25FF-4092-A5B2-9C3F1B616160 / data / Containers / General / AppGroup / 30B39103-CEEB-4C64-9531-FB27DC40180D /

File with iOS file system: /// Users / Lombardo / Library / Developer / CoreSimulator / Devices / BF52D613-25FF-4092-A5B2-9C3F1B616160 / data / Containers / General / AppGroup / 30B39103-CEEB-4C64-9531-FB27DC40180D /

Then I did the first thing every programmer should do: read the documents . The FIRST PAGE of the WatchOS 2 Migration Guide has the following suggestion:

Your extension now stores files and data in Apple Watch. Any data that is not part of your Watch application or add-on for WatchKit add-on must be obtained from the network or from an accompanying iOS application running on users iPhone. You cannot rely on a common group container for sharing files with your iOS application. Receiving files involves transferring them wirelessly to the Apple Watch.

+12
source share

All Articles