Ios 使用Watch Connectivity调用updateApplicationContext的最佳位置?
一些好的博客文章详细介绍了手表的连接,并使用了一些简单的应用程序示例,当你点击iPhone上的UI时,这些示例会向手表发送数据 我的应用程序只是列出iPhone应用程序中的数据,所以我不需要立即发送数据,我只是想在应用程序加载或进入后台时发送它…为此,我在Ios 使用Watch Connectivity调用updateApplicationContext的最佳位置?,ios,swift,watchkit,apple-watch,watchconnectivity,Ios,Swift,Watchkit,Apple Watch,Watchconnectivity,一些好的博客文章详细介绍了手表的连接,并使用了一些简单的应用程序示例,当你点击iPhone上的UI时,这些示例会向手表发送数据 我的应用程序只是列出iPhone应用程序中的数据,所以我不需要立即发送数据,我只是想在应用程序加载或进入后台时发送它…为此,我在didFinishLaunching和didEnterBackground中进行了updateApplicationContext…但是我手表界面控制器中的数据源代理非常注意被触发…特别是只加载了一眼模拟器和从不在设备上运行。是否有更好的时间和
didFinishLaunching
和didEnterBackground
中进行了updateApplicationContext
…但是我手表界面控制器中的数据源代理非常注意被触发…特别是只加载了一眼模拟器和从不在设备上运行。是否有更好的时间和地点来推送信息
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
WatchSessionManager.sharedManager.startSession()
do {
try WatchSessionManager.sharedManager.updateApplicationContext(["peopleDict" : peopleDict])
} catch {
print(error)
}
return true
}
func applicationDidEnterBackground(application: UIApplication) {
do {
try WatchSessionManager.sharedManager.updateApplicationContext(["peopleDict" : peopleDict])
} catch {
print(error)
}
}
下面是我的WatchSessionManager
我曾经在我的extensionelegate
的applicationdidfishunching
中调用activiateSession
import WatchConnectivity
protocol DataSourceChangedDelegate {
func dataSourceDidUpdate(dataSource: DataSource)
}
class WatchSessionManager: NSObject, WCSessionDelegate {
static let sharedManager = WatchSessionManager()
private override init() {
super.init()
}
private var dataSourceChangedDelegates = [DataSourceChangedDelegate]()
private let session: WCSession = WCSession.defaultSession()
func startSession() {
session.delegate = self
session.activateSession()
}
func addDataSourceChangedDelegate<T where T: DataSourceChangedDelegate, T: Equatable>(delegate: T) {
dataSourceChangedDelegates.append(delegate)
}
func removeDataSourceChangedDelegate<T where T: DataSourceChangedDelegate, T: Equatable>(delegate: T) {
for (index, indexDelegate) in dataSourceChangedDelegates.enumerate() {
if let indexDelegate = indexDelegate as? T where indexDelegate == delegate {
dataSourceChangedDelegates.removeAtIndex(index)
break
}
}
}
}
// MARK: Application Context
// use when your app needs only the latest information
// if the data was not sent, it will be replaced
extension WatchSessionManager {
// Receiver
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
dispatch_async(dispatch_get_main_queue()) { [weak self] in
self?.dataSourceChangedDelegates.forEach { $0.dataSourceDidUpdate(DataSource(data: applicationContext))}
}
}
}
导入WatchConnectivity
协议数据源更改数据源{
func数据源IDUpdate(数据源:数据源)
}
类WatchSessionManager:NSObject,wcSessionLegate{
静态let sharedManager=WatchSessionManager()
私有重写init(){
super.init()
}
私有变量datasourcechangedelegates=[datasourcechangedelegate]()
私有let会话:WCSession=WCSession.defaultSession()
func startSession(){
session.delegate=self
会话.激活会话()
}
func addDataSourceChangedDelegate(委托人:T){
dataSourceChangedDelegates.append(委托)
}
func removeDataSourceChangedDelegate(委托人:T){
对于dataSourceChangedDelegates.enumerate()中的(index,indexDelegate){
如果让indexDelegate=indexDelegate作为?T,其中indexDelegate==委托{
dataSourceChangedDelegates.removeAtIndex(索引)
打破
}
}
}
}
//标记:应用程序上下文
//当应用程序只需要最新信息时使用
//如果数据未发送,则会被替换
扩展监视会话管理器{
//接受者
func会话(会话:WCSession,didReceiveApplicationContext applicationContext:[字符串:AnyObject]){
dispatch_async(dispatch_get_main_queue()){[weak self]in
self?.datasourcechangedelegates.forEach{$0.datasourcediddupdate(DataSource(数据:applicationContext))}
}
}
}
AsupdateApplicationContext
只存储最新的应用程序上下文,您可以随时更新它。这只手表只能得到最新的数据。没有包含旧上下文的队列
在监视端,激活会话和设置WCSessionDelegate的最安全位置是ExtensionDelegate
init
方法:
class ExtensionDelegate: NSObject, WKExtensionDelegate {
override init() {
super.init()
WatchSessionManager.sharedManager.startSession()
}
...
}
您的一瞥不会更新,因为当显示一瞥时,
applicationdFinishLaunching
没有被调用(因为只有一瞥启动时手表应用程序没有启动)谢谢,也许我误解了API。。。我认为updateApplicationContext会将数据发送到手表,当手表上的会话被激活时,dataSourceDelegate会观察更新的上下文并使其可用?换一种方式说,我怎样才能让更新的信息进入我的视线?我在ExtensionLegate中执行activateSession。不,您不会误解API的工作原理。您在ExtensionLegate中的什么位置激活会话?在ApplicationIDFinishLaunching
中,还是像我在回答中建议的那样重写init
?我将我的WatchSessionManager类添加到我的问题中,我更新了我的回答,以便它从ExtensionLegate的init方法调用您的WatchSessionManager。这仍然不起作用吗?所以将startSession从ApplicationIDFinishLaunching中取出,并仅将其放入init中?