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.
Related
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.
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.
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
I'm implementing simple one-line communication between iOS app and watchKit with sendMessage.
The issue is - the communication is continuous.
The user presses a button on appleWatch, same action happens on Phone e.t.c.
Unfortunately, after two or three actions - the iOS app stops responding.
As advised by some on the forums - i've implemented backgroundTaskWithExpirationHandler, however, it doesn't work (doesn't give me even a promised three minute timeout).
Here's my code in the iOS app AppDelegate
func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
//recieve messages from watch
print(message["b"]! as? String)
let sweetN = message["b"]! as? String
//var sweetB = message["abs"]! as? [Int : Bool]
dispatch_async(dispatch_get_main_queue(), {
let taskID = self.beginBackgroundUpdateTask()
if sweetN == "peeks"{
if WCSession.isSupported(){
let message = [ "gettingData": "datareceived" ]
session.sendMessage(message, replyHandler: { replyDict in }, errorHandler: { error in })
}
}
self.endBackgroundUpdateTask(taskID)
})
}
func beginBackgroundUpdateTask() -> UIBackgroundTaskIdentifier {
return UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({})
}
func endBackgroundUpdateTask(taskID: UIBackgroundTaskIdentifier) {
UIApplication.sharedApplication().endBackgroundTask(taskID)
}
This worked for me.
You have to set the WCSession in the app delegate like:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
replyHandler(["message": "received!"])
}
and in the apple watch: the file extension delegate.swift do the same:
override init() {
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}
So I am using Watch Connectivity to request an array from the iPhone to the Watch.
The idea was to sendMessage from the watch, and the iPhone will reply with the array within the didReceiveMessage method.
However the iPhone does not seem to be responding, I thought the iPhone would open the application when I send the message from the Watch. I have tried even opening the application when I sendMessage but still no luck. When I wait long enough I get the following error message:
Error Domain=WCErrorDomain Code=7012 "Message reply took too long."
UserInfo={NSLocalizedDescription=Message reply took too long.,
NSLocalizedFailureReason=Reply timeout occured.}
Does anybody know where I may be going wrong ?
Apple Watch
import WatchKit
import Foundation
import CoreData
import WatchConnectivity
class BookmarkedInterfaceController: WKInterfaceController, WCSessionDelegate {
var session : WCSession!
var objects: [AnyObject]!
#IBOutlet var table: WKInterfaceTable!
override func willActivate() {
super.willActivate()
//Check if session is supported and Activate
if (WCSession.isSupported()) {
session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
sendMessageToIphone()
}
func sendMessageToIphone() {
if WCSession.defaultSession().reachable {
print("WCSession is reachabe")
let messageDict = ["Request": "iPhone Can You Give Me The Array"]
WCSession.defaultSession().sendMessage(messageDict, replyHandler: { (replyDict) -> Void in
print(replyDict)
}, errorHandler: { (error) -> Void in
print(error)
})
}
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
//recieving message from iphone
print("recieved message from iphone \(message)")
objects.append(message["Array"]!)
print("Objects array = \(objects)")
}
The console outputs
WCSession is reachabe
Array nil
iPhone App Delegate
import UIKit
import CoreData
import WatchConnectivity
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {
var window: UIWindow?
var session : WCSession!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//Check if session is supported and Activate
if (WCSession.isSupported()) {
session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
print("did recieve message from Watch")
let applicationData = ["Array":["One", "Two", "Three"]]
replyHandler(applicationData)
}
Nothing from the iPhone is being executed. Even when I manually open the app.
If you want the reply to the message the watch sent to contain the requested data, you should change your code to the following:
Watch
func sendMessageToIphone() {
if WCSession.defaultSession().reachable {
print("WCSession is reachabe")
let messageDict = ["Request": "iPhone Can You Give Me The Array"]
WCSession.defaultSession().sendMessage(messageDict, replyHandler: { (replyDict) -> Void in
print("Array \(replyDict["array"])")
}, errorHandler: { (error) -> Void in
print(error)
})
}
}
Phone
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
print("did recieve message from Watch")
let applicationData = ["Array":["One", "Two", "Three"]]
//If identifier from recievedMessage is for Objects
replyHandler(applicationData)
}
And separately, the reason why the sendMessage from the phone is not received by the watch is because you've implemented the wrong delegate method for the sendMessage invocation you are using.
If you call sendMessage with a nil replyHandler then this delegate method will be invoked on the receiving side:
func session(session: WCSession, didReceiveMessage message: [String : AnyObject])
If you call sendMessage with a non-nil replyHandler then this delegate method will be invoked on the receiving side:
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void)
You must activate the session before sending the message .Also you must set the delegate before you activate the session because you may lose some pending messages.
iphone side :
import UIKit
import WatchConnectivity
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {
var window: UIWindow?
var session : WCSession!
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
print("did recieve message from Watch")
let applicationData = ["Array":["One", "Two", "Three"]]
//If identifier from recievedMessage is for Objects
session.sendMessage(applicationData, replyHandler: { reply in
print("Got reply: \(reply)")
}, errorHandler: { error in
print("error: \(error)")
})
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
return true
}
}
iWatch extension InterfaceController
You must activate the session in the interface controller willactivate method.
import WatchKit
import Foundation
import WatchConnectivity
class InterfaceController: WKInterfaceController, WCSessionDelegate {
var session : WCSession!
var objects: [AnyObject]!
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
objects = []
sendMessageToIphone()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
func sendMessageToIphone() {
if WCSession.defaultSession().reachable{
print("WCSession is reachabe")
let messageDict = ["Request": "iPhone Can You Give Me The Array"]
WCSession.defaultSession().sendMessage(messageDict, replyHandler: { (replyDict) -> Void in
print(replyDict)
}, errorHandler: { (error) -> Void in
print(error)
})
}
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
//recieving message from iphone
print("recieved message from iphone \(message)")
objects.append(message["Array"]!)
print("Objects array = \(objects)")
}
}
Note. Run the iphone application first. Then Run the extension and keep the iphone app in foreground.