迅速在后台运行的WatchConnectivity-ApplicationContext并非一直都在运行 [英] swift WatchConnectivity running in background - ApplicationContext is not running all the time

查看:196
本文介绍了迅速在后台运行的WatchConnectivity-ApplicationContext并非一直都在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一种更好的方式(通过iPhone)向Watch(在后台)发送消息.目前我使用:

I want to know a better way to send (with iPhone) message to Watch (in background). Currently i use:

session.update(applicationContext: package, completion: nil)

用于向Watch和

 func didReceivedApplicationContext(applicationContext:[String : Any])

以在Watch中接收消息.问题是,正如文档系统将在适当的时间传输内容"中所述,我无法控制那些适当的时间".

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".

这时,我在iPhone课上检查了Watch的状态.如果Watch在后台,则使用updateApplicationcontext发送数据(不是很好),否则,如果Watch在前台,则使用sendMessage发送数据

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

代码:

 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?

推荐答案

好,您必须在后台传输和立即发送数据之间进行选择.使用WatchConnectivity框架,无法安排数据传输在确切的时间进行.

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.

您必须选择一种方法,即使您的应用程序在后台,该方法也可以发送数据,或者立即从前台发送数据.

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.

如果您需要保证在应用程序从后台唤醒时将传输数据,并且对于用例updateApplicationContext在测试过程中被证明不足,则可以使用解决方法.

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在后台传输数据,并在Watch应用程序的ExtensionDelegate类中,使用applicationDidBecomeActive()方法检查数据传输是否在后台发生,以及是否没有发生,请使用sendMessage函数立即从另一个应用程序请求数据,然后将请求的数据作为响应发送回去.

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.

要实现此目的,您的函数应在Watch应用上看起来像这样:

To implement this, your function should look something like this on your Watch app:

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)
                })
    }
}

然后在您的iPhone应用程序上执行以下操作:

And on your iPhone app you do the following:

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])
        }
    }

这篇关于迅速在后台运行的WatchConnectivity-ApplicationContext并非一直都在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆