- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
一些详细介绍 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 时,这些示例会将数据发送到 watch 。
我的应用程序只是列出了来 self 的 iPhone 应用程序的数据,所以我不需要立即发送数据,我只是想在应用程序加载或进入后台时发送它...为此我制作了 updateApplicationContext
在 didFinishLaunching
和 didEnterBackground
中……但是我的 watch 界面 Controller 中的数据源委托(delegate)非常容易被触发……特别是 glance 只加载到模拟器上,从不加载到设备上。有没有更好的时间和地点来推送信息?
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
我以前在我的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))}
}
}
}
最佳答案
因为 updateApplicationContext
只存储最新的应用程序上下文,您可以随时更新它。 watch 只会获取最新的数据。没有包含旧上下文的队列。
在 watch 端,激活 session 和设置 WCSessionDelegate 的最安全位置是在 ExtensionDelegate
init
方法中:
class ExtensionDelegate: NSObject, WKExtensionDelegate {
override init() {
super.init()
WatchSessionManager.sharedManager.startSession()
}
...
}
您的 Glance 没有更新,因为当显示 Glance 时,applicationDidFinishLaunching
没有被调用(因为只有启动 Glance 时 watch 应用程序没有启动)
关于ios - 在哪里最好使用 Watch Connectivity 调用 updateApplicationContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33267431/
我在使用 Swift 时遇到 WatchOs 2.1 问题。我想将字典中的一些数据发送到我的 iPhone。但是,如果我使用要发送到我的 iPhone 的信息创建字典,那么如果我调试它,则该字典是空的
一些详细介绍 Watch Connectivity 的好博文(http://www.kristinathai.com/watchos-2-tutorial-using-application-cont
我有一个 iOS 购物 list 应用程序,其中添加项目并显示在 tableView 中。我想创建一个 Watch App Extension,但我在考虑在这种情况下使用的最佳函数调用是 update
在 watchOS 2 中,不再有共享钥匙串(keychain)。 如果我想将钥匙串(keychain)值从 iOS 应用程序发送到 Watch 应用程序,通过 WCSession updateApp
我正在调用方法: open func updateApplicationContext(_ applicationContext: [String : Any]) throws 这需要 [Strin
我是 Swift 的新手,开始了我的第一个应用。 我正在尝试使用 updateApplicationContext 将数据从 Apple Watch 传输到 iPhone,但只收到一个错误: [WCS
applicationContext 是否加密?或者它只是存储为未加密的 plist 文件?因为如果是后者,我就不应该使用它来传输访问 token 。 最佳答案 设备之间的通信是加密的,如 iOS S
我是一名优秀的程序员,十分优秀!