I want to know a better way to send (with iPhone) message to Watch (in background). Currently i use:
我想知道一种更好的方式发送(带iPhone)消息给Watch(在后台)。目前我使用:
session.update(applicationContext: package, completion: nil)
for sending messages to Watch and
用于向Watch和发送消息
func didReceivedApplicationContext(applicationContext:[String : Any])
to receive messages in Watch. The problem is that as is said in documentation "The system will transfer content at opportune times" and i can't control those "oportune times".
在Watch中接收消息。问题是,如文档中所述“系统将在适当的时间传输内容”,我无法控制那些“oportune时代”。
At this time, i check in iPhone class the state of Watch. If the Watch is on Background, i send data with updateApplicationcontext (which is not verry good), else, if Watch is on foreground, i send data with sendMessage
这时,我在iPhone类中查看了Watch的状态。如果Watch在后台,我用updateApplicationcontext发送数据(这不是很好),否则,如果Watch在前台,我用sendMessage发送数据
Code:
码:
if session.isWatchReachable()
{
session.send(message: package, completion: nil)
}
else
{
session.update(applicationContext: package, completion: nil)
}
So is a better way to transfer data in background?
那么在后台传输数据的更好方法是什么?
1 个解决方案
#1
1
Well you have to choose between background transfers and sending data immediately. Using the WatchConnectivity
framework there is no way to schedule data transfer to happen at an exact moment in time.
那么你必须在后台传输和立即发送数据之间做出选择。使用WatchConnectivity框架无法在准确的时刻安排数据传输。
You have to choose between using a method that can send data even while your app is in the background or sending data immediately from the foreground.
您必须选择使用即使您的应用程序在后台也可以发送数据的方法,或者从前台立即发送数据。
If you need a guarantee that the data will be transferred by the time your app wakes up from the background and for your use case updateApplicationContext
proved to be insufficient during testing, you can use a workaround.
如果您需要保证在应用程序从后台唤醒时传输数据,并且在测试过程中使用updateApplicationContext证明不足,则可以使用解决方法。
The workaround is to use updateApplicationContext
to transfer data in the background and in your Watch app's ExtensionDelegate
class, use the applicationDidBecomeActive()
method to check whether the data transfer has happened in the background and if it did not happen, use the sendMessage
function to request the data immediately from the other app and send back the requested data as a response.
解决方法是使用updateApplicationContext在后台和Watch应用程序的ExtensionDelegate类中传输数据,使用applicationDidBecomeActive()方法检查数据传输是否在后台发生,如果没有发生,请使用sendMessage函数请求来自其他应用程序的数据立即发送回请求的数据作为响应。
To implement this, your function should look something like this on your Watch app:
要实现这一点,您的功能应该在Watch应用程序中看起来像这样:
func applicationDidBecomeActive(){
if !haveReceivedData { //Bool value that should be updated to true once data is received from the phone
session.sendMessage(["Requesting data":True], replyHandler: {
//parse the data, update the UI
}, errorHandler: { error in
print("Error requesting data",error)
})
}
}
And on your iPhone app you do the following:
在您的iPhone应用程序上,您可以执行以下操作:
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
if let isRequestingData = message["Requesting data"] as? Bool, isRequestingData == true {
replyHandler(["Requested data":data])
}
}
#1
1
Well you have to choose between background transfers and sending data immediately. Using the WatchConnectivity
framework there is no way to schedule data transfer to happen at an exact moment in time.
那么你必须在后台传输和立即发送数据之间做出选择。使用WatchConnectivity框架无法在准确的时刻安排数据传输。
You have to choose between using a method that can send data even while your app is in the background or sending data immediately from the foreground.
您必须选择使用即使您的应用程序在后台也可以发送数据的方法,或者从前台立即发送数据。
If you need a guarantee that the data will be transferred by the time your app wakes up from the background and for your use case updateApplicationContext
proved to be insufficient during testing, you can use a workaround.
如果您需要保证在应用程序从后台唤醒时传输数据,并且在测试过程中使用updateApplicationContext证明不足,则可以使用解决方法。
The workaround is to use updateApplicationContext
to transfer data in the background and in your Watch app's ExtensionDelegate
class, use the applicationDidBecomeActive()
method to check whether the data transfer has happened in the background and if it did not happen, use the sendMessage
function to request the data immediately from the other app and send back the requested data as a response.
解决方法是使用updateApplicationContext在后台和Watch应用程序的ExtensionDelegate类中传输数据,使用applicationDidBecomeActive()方法检查数据传输是否在后台发生,如果没有发生,请使用sendMessage函数请求来自其他应用程序的数据立即发送回请求的数据作为响应。
To implement this, your function should look something like this on your Watch app:
要实现这一点,您的功能应该在Watch应用程序中看起来像这样:
func applicationDidBecomeActive(){
if !haveReceivedData { //Bool value that should be updated to true once data is received from the phone
session.sendMessage(["Requesting data":True], replyHandler: {
//parse the data, update the UI
}, errorHandler: { error in
print("Error requesting data",error)
})
}
}
And on your iPhone app you do the following:
在您的iPhone应用程序上,您可以执行以下操作:
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
if let isRequestingData = message["Requesting data"] as? Bool, isRequestingData == true {
replyHandler(["Requested data":data])
}
}