WC WCSession counterpart app not installed - ios

Making a connection between iOS and iWatch devices, xCode writes [WC] WCSession counterpart app not installed.
After a lot of research, I've found a solution, maybe it will be helpful for someone.
- Check your WatchKit Extention target.
- Uncheck "Supports Running Without iOS App Installation"
- First run iwatch simulator, than run ios simulator with checkmark

I have spent around 3-4 hr to fix this issue, it seems that somehow my Apple watch.app is missing from my Target > Frameworks so after clicking plus icon and adding it back, it doesn't report "WC WCSession counterpart app not installed" anymore

Finally, I made it. For anyone else, who has this problem:
In iOS Target: Make sure in General>Frameworks, Libraries & Embedded Contents> Add YourWatchApp.app if it's not in the list. (For the loop problem, do the next step)
In Watch Target: Go to BuildPhases>Copy Bundle Resources> and remove YouriOSApp.app from the list if exists.
You must set delegate and activate it from both WatchApp and iOSApp as follows:
self.session = WCSession.default
self.session.delegate = self
self.session.activate()
if you are using a helper class, Your class should implement NSObject as well:
class ConnectivityHelper: NSObject, WCSessionDelegate {
Make sure there is some seconds between calling activate method and sending message.
If you are using a reply handler when sending message from Watch, Then you must implement didReceiveMessage method in iOS App which has replyHandler, else replyHandler on the watch returns an error and the iOS app won't receive the message.
Check out this complete code that is working: iOS Part:
import WatchConnectivity
class PhoneConnectivity: NSObject {
override init() {
super.init()
let session = WCSession.default
session.delegate = self
session.activate()
}
}
extension PhoneConnectivity: WCSessionDelegate {
//MARK: Delegate Methodes
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
}
func sessionDidBecomeInactive(_ session: WCSession) {
}
func sessionDidDeactivate(_ session: WCSession) {
}
// Receiver
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
//without reply handler
}
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: #escaping ([String : Any]) -> Void) {
//with reply handler
}
// end Receiver
}
send message with a test string first like:
WCSession.default.sendMessage(["TEST", "TEST2"], replyHandler: { dictionary in
print(">WC> reply recieved: \(dictionary.first!.value)")
}, errorHandler: { error in
print(">WC> Error on sending Msg: \(error.localizedDescription)")
})
Most developers suggest that make session active ASAP (in iOS using didFinishLaunchingWithOptions in appDelegate of iOSApp & applicationDidFinishLaunching in WKExtensionDelegate of WatchApp)

It only worked for me by installing the Watch App from the watch settings app.
If I install the Watch App from xcode, the iOS app will give me the companion error message.

Related

Check if Apple Watch is not currently connected

I am trying to detect if a user's Apple watch is currently active using this code:
if WCSession.isSupported() {
let session = WCSession.default
session.delegate = self
session.activate()
}
But even after I power off the watch, I get a callback to session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) with activationState == .activated.
Is this expected? Is there any way to actually check if the watch and the iPhone are connected?
EDIT: I also note that if I run the same code inside a simulator, I get the exact same callback with activationState == .activated, yet isPaired == false. It sounds to me like .activated basically is the default state. I'm not even sure how to get .deactivated
EDIT 2: I actually do not have a WatchKit extension and would rather not create one for simplicity. I am simply trying to detect if a paired watch is in range, powered on and communicating so I can tell if my apps local notifications might be delivered to the watch.
You are looking for: isPaired property of WCSession.
After calling activateSession and getting callback you can check if Watch is paired there.
If you need to know if app is installed on Watch - check isWatchAppInstalled.
EDIT
I understand what you're asking now. The issue is that if you check isPaired you get true, even if the app is launched and the watch is not connected.
Example:
import WatchConnectivity
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if WCSession.isSupported() {
let session = WCSession.default
session.delegate = self
session.activate()
}
return true
}
}
extension AppDelegate: WCSessionDelegate {
func session(
_ session: WCSession,
activationDidCompleteWith activationState: WCSessionActivationState,
error: Error?) {
switch session.activationState {
case .notActivated:
print("Session no activated")
case .inactive:
print("Session inactive")
case .activated:
print("Session activated")
#unknown default:
break
}
if let err = error {
print("ERROR: \(err.localizedDescription)")
}
if session.isPaired {
print("Current phone is paired to an apple watch!")
}
print("reachable: \(session.isReachable)")
}
func sessionDidBecomeInactive(_ session: WCSession) {
print("Session just became inactive")
}
func sessionDidDeactivate(_ session: WCSession) {
print("session did deactivate")
}
}
This results in the following output with the watch powered on or off
Session activated
Current phone is paired to an apple watch!
reachable: false
Note isReachable is always false because you don't have a watch extension.
Looking at this question there is no solution to this problem. I don't think you can determine this information without a watch extension / app.
Also note that you cannot access the WKInterfaceDevice outside of the watch app.

After restarting Apple Watch WCSession 'isRechable' returning false every time

I want to display data from firebase realtime database in watch app.
I have one method to fetch data from firebase in iOS parent App.
From watch app I am sending message to iOS parent app, iOS parent app will fetch data from firebase and reply to watch app with that data.
In iOS parent App activated WCSession as below,
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Watch Connectivity setup
if WCSession.isSupported() {
let session = WCSession.default
session.delegate = self
session.activate()
}
return true
}
Same way activated session in ExtensionDelegate in Watch Extension,
ExtensionDelegate.swift
func applicationDidFinishLaunching() {
// Perform any final initialization of your application.
if WCSession.isSupported() {
let session = WCSession.default
session.delegate = self
session.activate()
}
}
Now in InterfaceController class sent message to iOS parent app and get data in reply.
InterfaceController.swift
override func awake(withContext context: Any?) {
super.awake(withContext: context)
getData()
}
func getData() {
if WCSession.default.isReachable {
session.sendMessage(["Request": "GetData"], replyHandler: { (dictReply) in
print(dictReply)
}) { (error) in
print(error)
}
} else {
let action = WKAlertAction(title: "Ok", style: .cancel) {
}
self.presentAlert(withTitle: "Error", message: "iPhone is not reachable, try reconnecting watch with iPhone and try again.", preferredStyle: .alert, actions: [action])
}
}
Handled received message in iOS parent app and sent data in reply as dictionary.
AppDelegate.swift
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: #escaping ([String : Any]) -> Void) {
guard let request = message["Request"] as? String else {
replyHandler([:])
return
}
switch request {
case "GetData":
// Call method to get data from firebase realtime database and send response dictionary in replyHandler for demo purpose currently passed test data
replyHandler(["test" : "Hello..!!!"])
default:
replyHandler([:])
}
}
Now first time when run in device its working fine, bt after restarting apple watch opening watch app and its giving me error in alert "iPhone is not reachable, try reconnecting watch with iPhone and try again." that means WCSession.default.isReachable returning false every time. Application stuck here and just showing black screen on Apple watch.
Also If I run app from xcode then its working every time.
I am using iPhone 8 and Apple Watch series 1

session(_:activationDidCompleteWith:error:) not called on Apple Watch (but is called in Simulator)

I've created a simple swift class:
import Foundation
import WatchConnectivity
class WatchCommunication : NSObject, WCSessionDelegate {
var session: WCSession!
override init() {
super.init()
if WCSession.isSupported() {
print("WCSession is supported")
self.session = WCSession.default
self.session.delegate = self
if session.activationState == WCSessionActivationState.activated {
print("activationState is activated")
} else {
print("activationState is not activated")
self.session.activate()
}
} else {
print("WCSession is not supported")
}
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
print("activationState", activationState.rawValue)
print(error)
}
}
In my Watch App Extension I load create an instance of this class
class ExtensionDelegate: NSObject, WKExtensionDelegate {
let watchCommunication: WatchCommunication = WatchCommunication()
// ...
When I test this code in the simulator, I see the following logged
WCSession is supported
activationState is not activated
activationState 2
nil
All working fine.
When I run the same application on my testing iPhone X and paired Apple Watch 3, the logs show
WCSession is supported
activationState is not activated
So it seems that the method
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
is never called on my Apple Watch.
Methods like
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
are also not called on device (but are working on the simulator).
On the iPhone X side of things, the actionDidCompleteWith method is called, with activationState 2 (activated) and updateApplicationContext does not throw an error.
There is however communication between the iPhone and Apple Watch, because the method https://developer.apple.com/documentation/healthkit/hkhealthstore/1648358-startwatchapp does indeed start a workout on the watch (and all Apple and third party watch apps work normally).
For further reference:
Restarting my Apple Watch (what I never do otherwise) did solve the issue...

How to get UserDefaults data to Apple Watch even if iPhone is not active

I need to get a number from userDefaults to use in an Apple Watch app to make some calculations, I'm using the WatchConnectivity framework to get this piece of information but what I don't like about the way I have it right now is that the Phone only sends the data to the Watch when the iPhone app is loaded (viewDidLoad), in other words is I launch the Watch app I need to open the iPhone app in order to get the data to the Apple Watch.
Is it possible to get data from iPhone when it is not active?
Here is the code I'm using:
iOS View Controller
class ViewController: UIViewController, WCSessionDelegate {
var session: WCSession!
override func viewDidLoad() {
super.viewDidLoad()
if WCSession.isSupported() {
session = WCSession.default()
session.delegate = self
session.activate()
}
}
func session(_ session: WCSession, activationDidCompleteWith activationState:WCSessionActivationState, error: Error?) {}
func sessionDidDeactivate(_ session: WCSession) { }
func sessionDidBecomeInactive(_ session: WCSession) { }
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: #escaping ([String : Any]) -> Void) {
// Reply to watch with data from user defaults
replyHandler(["dataFromiPhone": readDoubleFromUserDefaults()])
}
}
WatchOS InterfaceController
class InterfaceController: WKInterfaceController, WCSessionDelegate{
var session: WCSession!
var myNumber:Double = 0
override func willActivate() {
super.willActivate()
if (WCSession.isSupported()) {
session = WCSession.default()
session.delegate = self
session.activate()
}
getDataFromiPhone()
}
override func didDeactivate() {
super.didDeactivate()
}
/// Delegate required method
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {}
func getDataFromiPhone(){
//Send Message to phone - I'm not sure if I need this message
let messageToSend = ["askiPhone":"Hi Phone, send me data from userDefaults."]
session.sendMessage(messageToSend, replyHandler: { replyMessage in
/// handle the reply
let dataFromPhone = replyMessage["dataFromiPhone"] as? String
DispatchQueue.main.async {
self.myNumber = Double(dataFromPhone!)!
}
}, errorHandler: {error in
/// catch any errors here
print("ERROR: \(error)")
})
}
}
I would recommend using App Groups for this. Add an App Group under "Capabilities" for your app's target and the watch extension's target:
And then set your UserDefaults using that App Group:
let appGroupName = "group.mobilemind.SpeedDial"
let appGroupUserDefaults = UserDefaults(suiteName: appGroupName)!
appGroupUserDefaults.set(NSNumber(value: newValue), forKey: "hasRated")
appGroupUserDefaults.synchronize()
Use UserDefaults this way on the app and the watch extension and both will be able to get and set the UserDefaults for the app group.
Edit
This approach does not work on WatchOS 2.0+. However, this approach still works on other types of app extensions.

Watch connectivity session issues

I am trying to send a message from a watch extension to the phone in order to update a complication.
AppDelegate.swift
var session = WCSession.defaultSession()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
if WCSession.isSupported(){
session.delegate = self
session.activateSession()
}
return true
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
replyHandler(["scheduleNames":scheduleNames, "scheduleData":scheduleData])
}
ExtensionDelegate.swift
override init(){
super.init()
if WCSession.isSupported(){
session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
self.sendMessage()
}
func sendMessage(){
print("Attempting to send message")
session.sendMessage(["Sender": "Complication"], replyHandler: {
reply in
print(reply.description)
}, errorHandler: {
error in
print(error.description)
})
}
But when I run the watch simulator (and the phone app is not open), I receive
Error Domain=WCErrorDomain Code=7007 "WatchConnectivity session on paired device is not reachable."
This also comes on the physical phone and watch.
What could be causing this?
UPDATE
This only happens when I make the call from the extension. The watch app can send and receive the message fine. Also, it works when called from applicationDidFinishLaunching() but not anything else in the extension delegate.
UPDATE
I fixed the previous and now get
WatchConnectivity session has no delegate.
Thanks to this answer, I figured out the issue. Calling from the Complication (which is what I was doing) in the requestedUpdateDidBegin() executes an asynchronous method in an asynchronous method, resulting in the update function ending before the sendMessage function returns.

Resources