Search code examples
ioswatchkitapple-watch

Watch app shared across multiple targets


My project has multiple build targets so I can facilitate "branded" builds that share the same code, but have unique bundle identifiers and assets per target. I've created a watch app for one of my app targets, which is working just fine. Now I'm trying to use the same watch app and extension for my other targets. I thought I could easily duplicate the watch targets (watch and extension) and simply change all the bundle id references to match the other targets but it seems like theres a hidden reference some where.

I just want to use one watch app for all my different branded targets.

Side note: the watch apps also would need to be branded correctly (asset folders).

Any ideas? Thanks.

Solution update

So, I solved it. When I duplicated my watch targets (watch and extension), and changed the bundle ids, target dependencies, everything to make it identical to the old target (but of course pointing to the new targets), for some reason the new watch target didn't point to the correct watch extension target. And there was no way to change it, anywhere. I tried to re-duplicate the targets and still had the same issue.

So what I did was to open the project.pbxproj file, search for any references to my watch extension (and bundle id) and manually update the connections. My watch target had a dependency to the wrong watch extension, and the new watch extension had a pointer to the old watch extension somehow. I later also found out I had to point the build to the correct productreference (appex reference) since this too was wrong.

So the new watch target, pointed to the wrong watch extension, and the correct watch extension pointed to the wrong appex (output file). All of this I had to change manually in the project file. It sounds extreme and hard, but it actually wasn't that difficult once you kept notes of the different UUIDs.

I hope this helps someone else who might stumble upon the same weird issue.


Solution

  • You definitely cannot use one Watch app for multiple targets. Because the only factor Xcode uses trying to find out whether to include a Watch companion app to the bundle or not is a bundle ID of the Watch app. So there is a one-to-one relationship.

    It follows you should duplicate each WatchOS app and extension target (changing only the bundle ID) for each iOS app target. But you still can share your code (including storyboards and assets) between targets by using Target Membership and it solves a problem of branded Watch apps either. Finally you get the minimum code duplication overhead.

    Update: I see you're trying to duplicate a Watch target using a right click on a target and choosing a Duplicate option. It really doesn't work for some reason.

    I just tried to clone a Watch target performing the following steps:

    1. File -> New -> Target -> WatchKit App.
    2. Specified the Embed in Companion Application the needed target.
    3. Added all files to the newly created target using the Target Membership option.

    After that a project has been built and ran without any problem with a new Watch target.

    This way you don't have to change the bundle ID manually - everything is created automatically and smoothly by Xcode.