Pass text from iOS label to WatchOS label - swift-

倾然丶 夕夏残阳落幕 提交于 2019-12-25 16:44:27

问题


I've a problem, with my app.. I try to follow some guide like Passing data to Apple Watch app , but I'm not sure it will fit for my case. I've some label with text on my iOS app, then I want to show this text on my watchOS app label, and I don't know which is the best way to pass this text and keep it synchronized with the iOS app.. Thanks a lot for your help!


回答1:


Since watchOS2, you don't have any built in function for communicating between the iOS and watchOS app than the WatchConnectivity framework.

From the information provided in your question, the updateApplicationContext(_:) function seems to be the best solution for your problem. You can send a dictionary of data with this function and the system tries to make sure that the data is received by the time your app is displayed to the user. If the function is called several times before the app would be visible to the user (run in the foreground), the system overwrites the previous data, so the Watch app only receives the most recent data to display.




回答2:


For watchOS1, Since Watch App is included as an extension in your Host App. So, you can use App Group to share data between your Host App and your App Extension.

Refer to https://stackoverflow.com/a/44654185/5716829 for more on using App Groups.




回答3:


The right method for this is WCSession.updateApplicationContext(_ applicationContext: [String : Any]) https://developer.apple.com/documentation/watchconnectivity/wcsession/1615621-updateapplicationcontext "Sends a dictionary of values that a paired and active device can use to synchronize its state."



来源:https://stackoverflow.com/questions/44673494/pass-text-from-ios-label-to-watchos-label-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!