Gar*_*abo 2 ios swift apple-watch watchkit watchconnectivity
一些详细介绍Watch Connectivity的好博文(http://www.kristinathai.com/watchos-2-tutorial-using-application-context-to-transfer-data-watch-connectivity-2/和http:// natashatherobot .com/watchconnectivity-application-context /)使用简单的应用程序示例,当您点击iPhone上的UI时,将数据发送到手表.
我的应用程序仅列出从我的iPhone应用程序中的数据,所以我并不需要立即发送数据,我只是想送它时,应用程序加载或进入后台......为此我做了updateApplicationContext
的didFinishLaunching
和didEnterBackground
.. .然后我的监视接口控制器中的dataSource委托非常发现触发...特别是只能在模拟器上加载,而不会在设备上加载.是否有更好的时间和地点推送信息?
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)
}
}
Run Code Online (Sandbox Code Playgroud)
下面是我WatchSessionManager
以前打电话给activiateSession
我extensionDelegate
的appliciationDidFinishLaunching
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))}
}
}
}
Run Code Online (Sandbox Code Playgroud)
由于updateApplicationContext
只存储最新的应用程序上下文,您可以随时更新它.手表只能获得最新数据.没有具有旧上下文的队列.
在手表端,激活会话和设置WCSessionDelegate的最安全位置在ExtensionDelegate
init
方法中:
class ExtensionDelegate: NSObject, WKExtensionDelegate {
override init() {
super.init()
WatchSessionManager.sharedManager.startSession()
}
...
}
Run Code Online (Sandbox Code Playgroud)
您的Glance不会更新,因为当显示Glance时,applicationDidFinishLaunching
未被调用(因为仅在启动Glance时未启动监视应用程序)
归档时间: |
|
查看次数: |
1591 次 |
最近记录: |