在我的viewController中,我有一个用于AVAudioPlayer的变量
var audioPlayer = AVAudioPlayer()
我想在我的watchKit应用程序中访问这个变量,这样我就可以在watchKit应用程序中播放和暂停AVAudioPlayer。喜欢
audioPlayer.play()
audioPlayer.pause()
如何从我的watchKit应用程序访问此变量?谢谢你的帮助!我使用的是Swift 3和Xcode 8。
发布于 2017-06-09 08:53:07
从watchOS 2开始,你不能使用AppGroups在你的iOS应用和WatchKit应用之间直接共享数据。
在两者之间进行通信的唯一选择是WatchConnectivity框架。使用WatchConnectivity,您可以使用即时消息通知iOS应用程序开始/停止播放。在iOS上,在AppDelegate中实现类似以下内容:
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
if let content = message["play"] as? [String:Any] {
audioPlayer.play()
replyHandler(["startedPlaying":true])
} else if let content = message["pause"] as? [String:Any] {
audioPlayer.pause()
replyHandler(["pausedMusic":true])
}
}
在你的手表应用程序中,你需要发送带有AppDelegate的session(_:didReceiveMessage:replyHandler:)
中指定的内容的消息。如果你不需要向手表应用程序发回响应,你可以只使用session(_:didReceiveMessage:)
并去掉replyHandler
部件。
https://stackoverflow.com/questions/44434040
复制