iPhone and Apple Watch not sharing App Group
Asked Answered
I

1

7

I've been pulling my hair out over this problem for two days now. I generate a small movie file in the iPhone App that I want to send from the iPhone to play on an Apple Watch. As I proof of concept, I put a similar .mov in the Bundle of the Apple Watch App and was able to play it with no problems. So I figured, easy peasy, now when I want to send a new little clip to the watch, I will just put it in an App Group Shared container and access it from there instead of from the Bundle. Nope!

I have been through tons of S.O. posts, tried many different approaches and nothing works as expected.

Before you ask, I have re-done the App Group several times, deleted the old ones from iTunes Connect, re-generated provisioning profiles, cleaned the targets numerous times, deleted the DerivedData several times, deleted the apps and started over several times and tried virtually every little tweak and fix I could find on S.O. Nothing helps.

I do think I know what the problem is, but have no idea how to conquer it.

Here is the code that I use in both the iPhone App and the Watch App to get a path to the shared container (this snippet if from the Watch side):

let fileManager = FileManager.default
    let sharedContainer = fileManager
        .containerURL(
            forSecurityApplicationGroupIdentifier: "group.com.mycompany.myappname")

    let dirPath = sharedContainer?.path
     sharedFilePathOnWatch = (dirPath! as NSString).appendingPathComponent("watchPreview.mov")

    print ("Shared File Path on Watch: \(sharedFilePathOnWatch)")
    print ("Shared File Path from iPhone: \(sharedFilePath)")

The print statements show me the Shared file path that I send over to the watch from the iPhone (via Watch Connectivity) and the Shared Path that the Watch App resolves.

What I get when this runs it the following:

Shared File Path on Watch: /private/var/mobile/Containers/Shared/AppGroup/94AA7797-528D-4E96-9623-23285BE0742B/watchPreview.mov

Shared File Path from iPhone: /private/var/mobile/Containers/Shared/AppGroup/4D43C314-CCBD-4861-BF90-69A8AC71198C/watchPreview.mov

Now I was expecting that there would be only one shared container and that both the iPhone and the Watch should resolve the same path, but this is not the case. I've also noticed that these paths remain constant for the two apps over multiple runs. It's not a dynamic address that is changing every time I run as I suspected for awhile.

As a last ditch attempt to get this to function, I figured that since I had a path to the shared container on the iPhone and the shared container on the Apple Watch, maybe I could just move the file from one to the other because, well, they are supposed to be sharing. But no, even that doesn't work.

So the big questions are:

  1. Why do I resolve two different paths on the shared Apps
  2. How the heck do I get my little movie file from the iPhone to the Apple Watch
Ivie answered 10/9, 2017 at 12:33 Comment(0)
G
15

With the introduction of watchOS2 and native watchOS apps, Apple Watch apps are no longer just extensions of their iOS counterpart and hence they don't share AppGroups.

You cannot use AppGroups for sharing data from watchOS2 onwards. You have to use the WatchConnectivity framework for sending data between your iOS and watchOS apps.

See the Keeping your WatchOS content up-to-date page for more information on the topic.

Greatly answered 10/9, 2017 at 13:48 Comment(3)
Ok, I've read this (several times before asking my question), but the confusing part is in the second section on sharing files at runtime, which is what I'm trying to do, states this: Use a shared app group to share media files between the Watch app interface and WatchKit extension at runtime. An app group creates a secure container that multiple processes can access. Normally, each process runs in its own sandbox environment, but an app group lets both processes share a common directory. and then proceeds to give instructions for setting up an app group exactly like I have done.Ivie
That is for sharing files between the WatchKit app target and the WatchKit extension targets and not between the iOS and watchOS apps.Greatly
Wow. I can't believe apple's docs aren't more clear about this. I've spent a couple of days trying to figure this out.Delphine

© 2022 - 2024 — McMap. All rights reserved.