In recent time, My colleague and I have developed a WatchOS App prototype. Then Our boss wanted to merge that POC to the production branch. That means the end users will have a chance to uncover our POC. That shouldn’t happen.
WatchOS app is an extension, it doesn’t stand alone. It basically needs a host app. The WatchOS project usually is a sub-project which eventually its product will be attached to the main app. But XCode doesn’t give us an option to keep the extension out for Release Build. (The release build is the build which will be submitting it to App Store, and reaches out to end users)
Can we do the proccess differently? OK, I’m going to write a tutorial to handle this situation.
First of all, I create a new iOS app project and add an additional target for the WatchOS app. It will look like this
I’m going to explain a bit.
- We see 3 targets on the left-hand side (MyApp is the host app which will attach the WatchApp inside of it)
- In the project’s build phases, We see 2 sections marked with stars
- Section 1 – Target dependencies: This section basically triggers building the WatchApp target.
- Section 2 – Embed Watch Content: This is created by XCode automatically when creating the WatchApp target. What this section will do is embed the WatchApp into MyApp. This is the way We publish the WatchOS App. When building the project, We will find the app which includes the watch folder.
Because We don’t have much control with this section. We can’t customize it like ask it to embed the watch app for DEBUG build only, so We need to remove this section and handle by ourself.
And then We need a little bit of magic. I have prepared a script to fill out. Please take a look at the script below.
We need some more steps
It is not done yet, We need some more steps:
- On the top-left corner, click on + icon to add new “copy files phrase”.
- We must place the “Copy Files” phase ahead of the script section
- Set the destination to “Absolute Path”, the “Path” value will be $(TARGET_BUILD_DIR)
- Add the file which we need to copy. Click + icon at the bottom to add, then choose “WatchApp.app”
Ok, everything is in place, Press the shortcut CMD + R to build the iOS app. You would find the message “hk >>> Attach WatchApp.app successfully”