我希望你们聪明人能帮助我,因为网上的大部分数据都已经过时了。我有一个显示财务信息的iPhone应用程序。我想在手表屏幕上展示这一点。
我可以让应用程序发送最新信息的字典,如果glance屏幕和手机应用程序都打开,扫视就会实时更新。
我想知道如何使用扫视屏幕来询问手机应用程序的最新信息。手机应用程序可能会被关闭,因此需要唤醒它,然后要求提供当前信息。
我使用的是swift 7、WatchOS 2.2和iOS9.3
堆栈溢出上的很多信息都引用了watchOS 1,所以不再起作用。
我期待着您的解决方案。
发布于 2016-05-20 05:31:52
研究一下WCSession,因为有不同的方法可以发送不同类型的数据。这个实现正在发送一个字典。
必须在手表和电话设备上设置WCSession。didFinishLaunchingWithOptions:中的AppDelegate和我在其init方法中使用ExtensionDelegate。在使用WCSession时,请务必使用import WatchConnectivity。将AppDelegate用作下面的WCSessionDelegate。
// AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Setup session on phone
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
return true
}
// WCSessionDelegate method receiving message from watch and using callback
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
// Reply with a dictionary of information based on the "message"
replyHandler(["Key": "Value"])
}
}在手表上设置WCSession:
// ExtensionDelegate.swift
override init() {
let session = WCSession.defaultSession()
session.activateSession()
}将包含字典的消息发送到手机,以便接收回调中的信息:
// GlanceController.swift
WCSession.defaultSession().sendMessage(["Please give Glance data": "Value"], replyHandler:{ (response) in
// Extract data from response dictionary
}) { (error) in
// Handle error
}https://stackoverflow.com/questions/37075213
复制相似问题