Watch Connectivity is a framework that implements two-way communication between an iOS app and its paired watchOS app.
Watch Connectivity
Implementing Two-Way Communication Using Watch Connectivity
This is a sample code for Watch Connectivity provided by Apple.
There are many communication methods for different purposes, and this is a project where you can experience them.
It has a lot of functions, and it is difficult to grasp.
This article describes how to prepare and use the sample code.
Preparation
As you can see on the sample code page, some preparation is required to run it.
TARGETS > General pane> update the Bundle Identifier filed with a new identifier.
Signing & Capabilities pane> Team drop-down menu> select appropriate profile
TARGETS > WatchKit App
Update Bundle identifier and developer team.
(Bundle identifier: Add ‘.watchkitapp’ to the name given by SimpleWatchConnectivity.)
TARGETS > WatchKit Extension
Update Bundle identifier and developer team.
(Bundle identifier: Add ‘.watchkitapp.watchkitextension’ to the name given by SimpleWatchConnectivity.)
Open the Info.plist file of the WatchKit App target.
Update the value of WKCompanionAppBundleIdentifier key with the new iOS app Bundle Identifier.
Open the Info.plist file of the WatchKit Extension target.
Update the value of the NSExtension > NSExtensionAttributes > WKAppBundle Identifier key with the new WatchKit App Bundle Identifier.
Usage
When you run it, you will see a screen like the following figure. The communication command menu is displayed at the bottom.
What you can experiment with for each item.
UpdateAppContext
updateApplicationContext(_:) | Apple Developer Documentation
Sends a dictionary of values that a paired and active device can use to synchronize its state.
In the sample, the text color changes each time the button is pressed.
SendMessage
sendMessage(_:replyHandler:errorHandler:) | Apple Developer Documentation
Sends a message of Dictionary object immediately to the paired and active device and optionally handles a response.
SendMessageData
sendMessageData(_:replyHandler:errorHandler:) | Apple Developer Documentation
Sends a data object immediately to the paired and active device and optionally handles a response.
TransferFile
transferFile(_:metadata:) | Apple Developer Documentation
Transfers a file to the counterpart asynchronously on a background thread.
If you send them in succession, files will be enqueued and sent in order.
In the sample code, ‘TransferFile’ on the iPhone sends the info.plist file to the Apple Watch.
‘TransferFile’ on the Apple Watch sends the debug log created by the Logger class to the iPhone.
The part that describes the file in TestDataProvider.swift
var file: URL {
#if os(watchOS)
return Logger.shared.getFileURL() // Use the log file for transferFile.
#else
// Use Info.plist for file transfer.
// Change this to a bigger file to make the file transfer progress more obvious.
//
guard let url = Bundle.main.url(forResource: "Info", withExtension: "plist") else {
fatalError("Failed to find Info.plist in current bundle!")
}
return url
#endif
}
By default, the file sent from the iPhone is an Info.plist file.
If you change to a large size file such as an image file, it will take time to complete the transmission, so you can see the progress.
When you tap the number, another View will be displayed and the progress will be displayed.
By the way, be careful when sending and receiving files.
When a file is received, the WCSessionDelegate method session(_:didReceive:) is called.
The documentation says:
Remember to move the file referenced by the
file
parameter if you intend to keep it. If you don’t move the file synchronously during your implementation of this method, the system deletes the file when the method returns.
The file will be deleted at the timing of returning, so if you want to use the file later, you need to move or copy it.
TransferUserInfo
transferUserInfo(_:) | Apple Developer Documentation
transfers a Dictionary object, but it ensures that the delivery happens.
If you send them in succession, files will be enqueued and sent in order.。
TransferComplicationUserInfo
transferCurrentComplicationUserInfo(_:) | Apple Developer Documentation
Sends complication-related data from the iOS app to the WatchKit extension.
Apple Watch’s “complication” is a function that provides some of the information and functions of the application on the face.
To see the results of this item in the sample code, the following preparations are required.
There are two methods: setting from the iPhone Watch App and setting using only the Apple Watch.
Using iPhone’s Watch App
- iPhone’s Watch App > Face Gallery > add ‘Modular’.
- Choose a Modular watch face on the Apple Watch.
- iPhone’ Watch App > Select ‘Modular’ in My Faces > Select Middle in Complications.
- Select SimpleWC in SimpleWatchConnectivity
Using Apple Watch
- Choose a Modular watch face on the watch.
- Press the watch face to show the customization screen, tap the Edit button, then swipe right to show the configuration screen.
- Tap the middle area, then rotate the digital crown to find the
SimpleWatchConnectivity
complication. - Press the digital crown and tap the screen to go back and finish the configuration.
With these steps, the face is “modular” and has SimpleWatchConnectivity complication in the middle.
In the sample code, when you press ‘TransferComplicationUserInfo’ on your iPhone, random numbers will be displayed on your Apple Watch.