如何在后台唤醒iPhone应用程序以运行功能并将结果返回给Apple Watch? [英] How can I awake my iPhone app in the background to run a function and return the results to Apple Watch?
问题描述
我的Apple Watch应用程序中有一个表,该表由iPhone应用程序中的数据填充.
I have a table in my Apple Watch app that is populated by data from the iPhone app.
在iPhone应用程序上,我需要从互联网上获取此数据.在电话上,该函数称为retrieveData().我还具有一个名为sendToWatch()的函数,该函数可在手机应用程序打开时将该数据发送到手表.
On the iPhone app, I need to get this data from the internet. On the phone, the function is called retrieveData(). I also have a function called sendToWatch() that sends this data to the watch when the phone app is open.
我想找到一种无需打开电话应用程序即可运行retrieveData()和sendToWatch()的方法.在过去的几个小时中,我一直在研究这个问题,而且看起来非常复杂.
I'd like to find a way to run retrieveData() and sendToWatch() without needing to open the phone app. I've looked into this for the past few hours, and it seems very complicated.
我已经尝试过Apple文档中的openParentApp函数,但是我不知道如何设置或实现它.在过去的几个小时里,我尝试过在线搜索教程,但是一切都已经过时了,我没有运气.
I've tried the openParentApp function that is on Apple's docs, but I have NO IDEA how to set it up or how to implement it. I've tried searching for tutorials online for the past few hours, but everything is already outdated and I've had no luck.
有人知道有什么好的教程吗?
Does anybody know of any good tutorials for this?
推荐答案
如您所知,您不能在watchOS 2中使用openParentApp
.您可以使用 Watch Connectivity 代替openParentApp
.
As you know, you can NOT use openParentApp
in watchOS 2. You can use Watch Connectivity instead of openParentApp
.
参考 http://www.kristinathai. com/watchos-2-如何使用手表连接性在设备之间进行通信/
以下是一个示例.
1)从Apple Watch调用sendMessage
方法
1) Call sendMessage
method from Apple Watch
WCSession.defaultSession().sendMessage(applicationDict,
replyHandler: { ([String : AnyObject]) → Void in
// Handle reply
})
errorHandler: { (NSError) → Void in
// Handle error
});
2)didReceiveMessage
方法在您的iPhone中被调用
2) didReceiveMessage
method is called in your iPhone
运行retrieveData()并使用此方法将数据发送到Apple Watch.
Run retrieveData() and send data to Apple Watch in this method.
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
// run retrieveData()
// send data to Apple Watch
replyHandler(["retrievedData" : data])
}
3)在Apple Watch的replyHandler
中接收数据
3) Receive data in replyHandler
in Apple Watch
祝你好运!
这篇关于如何在后台唤醒iPhone应用程序以运行功能并将结果返回给Apple Watch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!