WatchOS app not detecting companion iOS app - ios

I am making an independent Apple Watch app (but with a companion iPhone app which is not necessary for the watch app to function.)
The app uses WatchConnectivity to sync data between devices if iPhone companion app is installed. I am able to call session.updateApplicationContext() on the iOS app and receive session(didReceiveApplicationContext) on the Watch app. But going the other way is not possible. I always get an NSError code of 7018 which means the iOS companion app is not installed.
I have made sure both watchOS and iOS app are installed in simulator (and tested on my actual devices, iPhone 11 & Watch series 5.) But getting the same failed results from watchOS -> iOS.
I noticed there is a new WCSession instance variable available for watchOS 6.0+ isCompanionAppInstalled. When I read this variable after my WCSession is activated on my watch app, it always return false.
Is there anything else I need to look out for? I am thinking maybe there is something I did wrong in all the different info.plist. But I have checked them multiple times.
my info.plist files:
In my watch app:
WKWatchKitApp is YES
WKCompanionAppBundleIdentifier is com.abc.myapp
In my watch extension:
App can run independently of companion iPhone app is YES
NSExtension
WKAppBundleIdentifier is com.abc.myapp.watchkitapp
NSExtensionPointIdentifier is com.apple.watchkit
iOS app bundle identifier is com.abc.myapp
watchOS app is com.abc.myapp.watchkitapp
watchOS app extension is com.abc.myapp.watchkitapp.watchkitextension
Is there anything else I might be overlooking?
Thank you very much for any help/insight you can offer into this.

Ok. I found a way to get rid of the error and have the watch connectivity work from watch to iPhone.
Originally I wanted the watch app to be independent so in the watch extension target in Xcode, the check box "Supports Running Without iOS App Installation" is checked. But as soon as I uncheck this box, WCSession.isCompanionAppInstalled returns true and error goes away. Syncing from watch to iPhone starts working.
So hopefully this is not intended behavior and will soon be fixed by Apple. (I filed a bug report.) But for now, I am just going to leave the independent watchOS app unchecked because I do want WatchConnectivity in case iPhone app is installed by user.
Thank you for reading and good luck with your programming.

If you have "App is only available as a standalone watchOS app" in Info.plist, delete it even if it is false. Leave only "App can run independently of companion iPhone app". Also don't forget to set "WKCompanionAppBundleIdentifier" for Watch App (not extension). At least it works on real device for me with these parameters.

This seems to be a bug. Sometimes when I start my WatchOS app, it gets WCSession.isCompanionAppInstalled = false all the time, even though the companion app on the iPhone is running, and other apps are able to communicate with the phone.
This seems to go away when I force-restart the app on the watch (press side button, then swipe the app to the left and press the big red button, then start the app again). After the restart, everything seems to work fine.
This has happened only on Testflight builds so far. If it happens in production builds, I'm going to file a bug.

My iOS app will not launch in the Simulator if I uncheck "Supports Running Without iOS App Installation".

Related

Debugging the iOS part of Apple Watch app in the Simulator

A couple of years ago I wrote and debugged an Apple Watch app using the watchOS 1 SDK. I am now working on a new watch App using the watchOS 3 SDK.
The Watch App part is able to be debugged. I can set break points and step through it. However, I use the WatchConnectivity framework to get information from the iOS app. This isn't working and I'm unable to debug it.
When I run the WatchApp scheme in Xcode, two simulators get launched, one for the iOS app and one for the WatchApp. I see the WatchApp starting but not the iOS app. If the iOS was previously running, it appears to be killed.
I put a break point on the application:didFinishLaunchingWithOptions: in the iOS app, method but this break point is never hit. Should it be.
I don't recall having these problems a couple of years ago when going through this.
Should running the WatchApp scheme automatically start the iOS app and allow it to be debugged? Should my didFinishLaunchingWithOptions: breakpoint get hit?
The other thing that is sort of strange and may be hint is that the External Displays list in the iOS simulator never shows watch app displays. I would think it should.
I've tried cleaning the project, restarting Xcode and even rebooting my computer. No joy.
No, the iOS and WatchApp have different targets, so running one from Xcode doesn't start the other one automatically.
You have to start one of them using Xcode in the usual way, then start the other app manually from its respective Simulator, than in Xcode use Debug>Attach to Process to see both apps in the debugger.

How to programmatically open Apple Watch companion app from iOS app

I would like to give a hint to the user, that my iOS app supports the Apple Watch.
So I would like to link/open the Apple Watch companion app from within my iOS app, very much similar to opening the Settings App using ([[UIApplication sharedApplication] openURL: [NSURL URLWithString:UIApplicationOpenSettingsURLString]];)
This shall enable the user to directly navigate to the Watch companion app to setup my app for the watch.
I could not find any URL which would open the companion app in general or specific to a section of the companion app.
If direct linking is not supported, I am also interested in alternative approaches for this use case.
Thanks in advance!
EDIT:
I checked the WatchKitSettings Info.plist file in the simulator to see if it registers any URL schema, but it does not.
I guess it's not possible to programmatically launch the watch companion app from iOS. The opposite way would be possible: to launch the iOS app in the background upon receiving a message from the watch. See WWDC talk Introducing Watch Connectivity.
You could check WCSession.defaultSession().watchAppInstalled and ask the user to launch the app if it is true.
Previously, it was not possible to launch the WatchKit App from the iPhone app, but the iPhone app could be launched—only in the background—with the openParentApplication method.
As of WatchOS 2, however, it is now not possible for either app to cause the other to launch. Instead, there are new methods for queueing changes to be picked up when the other app is launched in the future.
One partial solution for your use case could be displaying a local notification which could be tapped to open the watch app. The primary flaw in such an approach is I do not believe there is a way to limit this notification to the Watch only, so it would also appear on the iPhone where if tapped it would bring the iPhone app to the foreground. Messy if implemented like this, and therefore not worth it I'd have thought, even if it could pass app store review.

Debug WatchKit on real Apple Watch - Nothing happens

I am struggling with debugging my WatchKit Extension/App on a real Apple Watch. Debugging both the iPhone App and the WatchKit Extension using the simulator the simulator is not problem:
Select the WatchKit App Profile and run in Simulator ==> App is launched on in Watch Simulator and I can use breakpoints in the Extension code to debug.
To debug the iPhone app as well I launch the app in the simulator and attach the debugger manually ==> I can use breakpoints in the iPhone Code to debug.
It is no problem to observe both the work of the iPhone app and the watch app. At least not in the simulator.
I would like to do the same thing on a real Apple Watch. But when I select the WatchKit App Profile and my real iPhone (instead of the Simulator) and click on "Run" nothing happens. This means Xcode seems to build and start the app but nothing happens on the devices. The status field in Xcode shows:
Building MyApp WatchKit App: MyApp WatchKit Extension
Building MyApp WatchKit App: Finishing...
Running MyApp on My iPhone 6
This is all. No debug Window, breakpoints are ignored or do not work and the app is not launched neither on the iPhone or on the Apple Watch.
I found other questions about problems with debugging on real devices (e.g. here) but they all deal with installation and signing issues. In my case both the iPhone App and the WatchKit App are installed without any problem. When I click the app icon on the Watch I can start and use the app. Problem is that I cannot debug this process.
Why do I need to debug the process on real devices? Well there is one thing I cannot test using the simulator: What happens when the Watch App tries to contact the iPhone app using openParentApplication:reply: when the iPhone app is not already running? This works perfectly in the simulator but on real devices the Watch Apps seems to get no reply from the iPhone app and simply waits forever.
I already found hints to solve this but without being able to debug the Watch App and to see how the code is executed I cannot be sure what is going on...
thank you for your answers. I finally managed to get the debugging running (most of the time) on my real Apple Watch. However it is quite cumbersome and not very reliable. There is not enough space in the comments so I will use this answer to describe my solution. Maybe it is help full to other:
Make sure, the App on your Watch is not running (as BalestraPatrick described). Launch the app and hold the side button a few seconds to bring up the "Turn off your Watch" dialog. Hold the side button for another few seconds to close the app and to return to the Watch homescreen.
Make sure the iPhone is not locked.
Select the WatchKit App target and your real iPhone and run the project.
Running the Watch App resulted quite often in an error SPErrorInvalidBundleNoGizmoBinaryMessage. Re-starting Xcode and cleaning both the Watch App and the App target solved this.
If the build of the Watch App succeed there will be a short message in Xcode but nothing on your iPhone or Watch. If you have made changes to the Watch App it will take a few seconds to refresh the app on the Watch. This is indicated by the progress-circle overlay over the app icon. If you made no changes ore once the app has been transfered: Launch the watch app manually by tapping the icon. There will be no automatic launch.
In most cases Xcode will recognize the app launch and attach its debugger to it. This will allow to use breakpoints, inspect the code, etc.
In my case I whanted to inspect how the iPhone App handles the application:handleWatchKitExtensionRequest:reply: call when the app was not active before. This is important because this cannot be done using the simulator. If the app takes to long to handle the request the Watch will receive no valid response.
After following the steps described above Xcode is only attached the watch app and will not hold on breakpoints in the iPhone app code. To do this, one has to manually attach the Debugger to the iPhone app process that is started when the watch app sends its call.
To be able to attach the debugger I added a delay to the apps main function: [NSThread sleepForTimeInterval:5]:
Select function in Watch App that will start the call to the iPhone App
The iPhone App will be launched in background. The delays gives you 5 seconds to attach the debugger.
Choose Debug\Attach To Process\Likely Targes\Your iPhone Appin Xcode to attach the debugger.
After the 5 seconds delay the process will continue and you will be able to use breakpoints in your iPhone app code as well.
Do not forget do remove the delay code when you finished testing :-)
NOTE:
You will not be able to see NSLog output (or any console output at all) from the iPhone App since attaching the debugger does NOT re-route the console output.
Happy testing with this awesome new Apple product :-P
I am having the same kind of issue but to improve a little bit the debugging experience I usually restart my devices a few times. Try to restart your Apple Watch or your iPhone.
Before launching your app from Xcode, make sure that the app is completely closed on your Apple Watch (not stuck in the loading screen for example). To do that, you have to force close the app: enter your app, keep the side button pressed until the menu to turn off the watch appears, then press the same side button for a few more seconds until the watch will go back to the homescreen and force close your app.
Now you can try to build and run the app from Xcode and it should work more reliably.
watchOS 3+:
The force quit is done by pressing and holding the side button (the button just below the Digital Crown) until the shutdown screen appears, and then
let go of the side button, then press and hold the Digital Crown.
Here's a technique that seems to be be the most reliable for me, even though it still only works about 25% of the time (Xcode 7 beta 4, 7A165t):
Run the phone target in debug (on your actual iPhone of course)
While the iPhone app is still running, switch to the watch target and run that in debug.
Keep an eye on your watch homescreen as the app installs. Once the app has installed, tap it to open it. This will sometimes "kickstart" the watch app and allow the debugger to catch it. At this point you should be able to debug both your iPhone app and watchOS app together.
This question has different answers. All are good.
The reason is: in my experience there are at least two different problems that prevent debugging on a real watch. Both require a different strategy.
This answer tries to help you to decide when to use which strategy.
XCode installs, tells you it is running for a short time and then stops showing "running...".
start with the answer of BalestraPatrick:
restart your devices.
Do not forget to restart your iPhone. This did the trick the first time I had this problem.
XCode installs, tells you it is running for a longer period of time: "running <projectName> on Apple Watch of <yourname>.
start with the answer of Jay Hickey or Andrei Herford
starting the watch app manually probably does the trick.
I also had times when XCode was able to start my watch app without any help from me.
Bonus:
Starting with XCode 7.1 it seems XCode doesn't always compile all necessary Swift files (more often than before). This is not watch specific, it also applies to iOS apps. The symptoms can look similar to those of this question. -> clean your project and then restart XCode.

How to remove a partially installed app on Apple Watch

There is a partially installed app on my Apple Watch and I can't find a way to remove it. I have removed all the my apps that I've installed using Xcode to iPhone and the Watch still shows an icon that you see when a Watch app is about to be installed on the watch (see the screenshot blow)
You should be able to delete it using Jiggle Mode after a reboot.
In my case, the Apple Maps Watch app was partially loaded, and the only solution I found was resetting the watch and restoring from backup. This corrected the issue.
Aside: you can figure out which app it is that is partially installed by going to the Apple Watch iPhone app and choosing App Layout. The icons will all be complete there.

How can I avoid the error "Error Launching 'AppName' WatchKit Extension" when trying to build and run a WatchKit Extension on an actual Apple Watch?

When I try to build and run a WatchKit extension on my real Apple Watch, I sometimes get the following error message:
I've tried to build the parent application for the iPhone and then again for my WatchKit app but I still get the error message.
Does anyone know what causes this error message and how to deal with it?
This seems to happen when it takes a long time to install the WatchKit App on the actual watch and Xcode times out waiting for the process to start due to the long install time. It can take a long time for two reasons:
It sometimes takes a while for the Watch to "notice" the App needs updating before it begins the transfer.
If your Apple Watch app has a lot of images or other content it needs to transfer to the watch, the actual transfer over Bluetooth can take a long time.
If you want to avoid this error, here are some tips, adapting from this blog post, which can also show you how to speed up your app installs: http://www.sneakycrab.com/blog/2015/5/28/speeding-up-slow-install-times-when-debugging-on-a-real-apple-watch
Choose the iOS base scheme in Xcode to build and install on your phone. (Not the WatchKit App Extension)
Hit stop in Xcode.
Get iOS to notice it needs to begin installing the Watch app. There are two ways, either a) open the page for your app in the Apple Watch Companion App on your phone, or b) tapping on your app directly on the Watch (when you see the spinner, you can hit the crown to go back to the app icon screen, and you should see it start to load)
Once the app is installed on your watch, switch to the WatchKit Extension scheme and hit play, it will be waiting to attach debugging
Launch your app manually.
Make sure your iPhone bluetooth is turned on
Then I removed the app from my iPhone and re-launched it. That fixed it for me.

Resources