을 당신은 내장을 사용할 수 있습니다 WatchConnectivity 프레임 워크에 쌍 애플 시계에 iOS 앱에서 메시지를 보낼 수 있습니다.
1) 먼저, 시계 연결 세션 iOS 앱과 WatchKit 확장 모두을 활성화합니다. iOS 측에서는 앱 대표단의 application didFinishLaunchingWithOptions
에서 수행 할 수 있습니다. 시계 측면에서는이 코드를 applicationDidFinishLaunching
WatchKit 확장 대리자 메서드에서 실행할 수 있습니다.
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
2) 이제 iOS 앱에서 메시지를 보내주십시오.
let session = WCSession.defaultSession()
session.sendMessage(["message from iOS app":""], replyHandler: { reply in
// Handle reply from watch (optional)
}, errorHandler: nil)
3) 당신의 WCSessionDelegate
대표 클래스의 session didReceiveMessage
방법을 구현하여 WatchKit 확장에 메시지를 수신합니다.
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
if let message = message["message from iOS app"] {
NSNotificationCenter.defaultCenter().postNotificationName("myapp.reload", object: self, userInfo: ["data": message])
}
}
iOS에서 메시지를 받으면 postNotificationName
방법으로 알림을 전송합니다.
4) 업데이트가 필요한 InterfaceController (또는이 업데이트 알림을 수신하려는 다른 곳)에서이 알림을 구독하십시오.
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "didReceiveReloadNotification:", name: "myapp.reload", object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self,
name: "myapp.reload", object: nil)
}
5) 마지막으로 알림 처리기 메서드를 구현합니다. 여기에서 UI를 업데이트 할 수 있습니다.
func didReceiveReloadNotification(notification: NSNotification) {
let userInfo = notification.userInfo as? [String: String]
if let userInfo = userInfo, data = userInfo["data"] {
// Update UI
}
}
참고 : 가독성을 위해 내가 "iOS 앱에서 메시지"알림 이름 "myapp.reload"와 메시지 키 인라인 텍스트 문자열을 사용하고 있습니다. 그러나 실제 응용 프로그램에서는 이러한 텍스트 문자열에 대한 속성을 사용하여 오타를 피하는 것이 좋습니다.
대단한 답변입니다! 비록 이것이 지원으로 문서화되어 있지 않다고 덧붙이고 싶습니다. 이것은 미래에 막을 수 있거나 시계 패키지 애플 리케이션을 받기 시작할 때 애플 인증을 통과하지 못할 수 있습니다. 하지만, 지금은 훌륭하게 작동합니다! –