Sil*_* St 4 ios watchkit ios9 watchos-2
如果打开iPhone上的应用程序,我如何从watchOS 2查看?
我想NSUserDefaults通过手表向iPhone 发送消息sendMessage(当收到消息时能够更新手机上的界面)当两个应用程序都在运行时我想发送NSUserDefaults即使只有watchOS 2应用程序正在运行.
根据我的阅读,我发现了这个:
/** The counterpart app must be reachable for a send message to succeed. */
@property (nonatomic, readonly, getter=isReachable) BOOL reachable;
Run Code Online (Sandbox Code Playgroud)
它总是可以从我检查的内容到达.
可达表示苹果手表和iPhone通过蓝牙或wifi连接.这并不一定意味着iPhone应用程序正在运行.如果可达,则当您尝试从Apple Watch发送消息时,它将在后台启动iPhone应用程序.您需要尽快分配WKSession委托,因为委托方法(sendMessage)很快就会触发.我想你要说的是如果可以的话调用sendMessage,而不是使用transferUserInfo方法.要做到这一点,首先在你的苹果手表上:
func applicationDidFinishLaunching() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
// NOTE: This should be your custom message dictionary
// You don't necessarily call the following code in
// applicationDidFinishLaunching, but it is here for
// the simplicity of the example. Call this when you want to send a message.
let message = [String:AnyObject]()
// To send your message.
// You could check reachable here, but it could change between reading the
// value and sending the message. Instead just try to send the data and if it
// fails queue it to be sent when the connection is re-established.
session.sendMessage(message, replyHandler: { (response) -> Void in
// iOS app got the message successfully
}, errorHandler: { (error) -> Void in
// iOS app failed to get message. Send it in the background
session.transferUserInfo(message)
})
}
Run Code Online (Sandbox Code Playgroud)
然后,在您的iOS应用中:
// Do this here so it is setup as early as possible so
// we don't miss any delegate method calls
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.watchKitSetup()
return true
}
func watchKitSetup() {
// Stop if watch connectivity is not supported (such as on iPad)
if (WCSession.isSupported()) {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
// Handle the message from the apple watch...
dispatch_async(dispatch_get_main_queue()) {
// Update UI on the main thread if necessary
}
}
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
// Handle the message from the apple watch...
dispatch_async(dispatch_get_main_queue()) {
// Update UI on the main thread if necessary
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2635 次 |
| 最近记录: |