[英]My Iwatch does not get data saved from ios App even though I used AppGroup
I have read and followed several instructions and videos in attempt to get my iphone and iwatch app use the same Userdefaults storage. 我已阅读并遵循了一些说明和视频,以尝试使我的iphone和iwatch应用程序使用相同的Userdefaults存储。 I have created an appgroup and used the appgroup name like this 我创建了一个应用程序组并使用了这样的应用程序组名称
preferences = UserDefaults(suiteName: "group.com.xxxx.appname")!
but it still does not work as expected 但它仍然无法按预期工作
I have read through almost all(if not all) questions related to this on stackoverflow, but I am still not getting expected result. 我已经阅读了关于stackoverflow的几乎所有(如果不是全部)问题,但是我仍然没有得到预期的结果。
The last post I read suggested using WCSession, so I followed this url http://www.codingexplorer.com/watch-connectivity-swift-application-context/ 我阅读的最后一篇文章建议使用WCSession,因此我遵循了该URL http://www.codingexplorer.com/watch-connectivity-swift-application-context/
After all these attempts, my Iwatch is still unable to use the preferences/data I am setting on my ios app. 经过所有这些尝试后,我的Iwatch仍然无法使用我在ios应用程序上设置的首选项/数据。
Any response would be appreciated towards fixing this. 对于解决此问题的任何回应将不胜感激。
App Groups are no longer an option when attempting to access data on both of the devices. 尝试访问两个设备上的数据时,不再是“应用程序组”选项。
You now need to use the WatchConnectivity Framework. 现在,您需要使用WatchConnectivity框架。
Here's an example on how to use this: 这是有关如何使用此示例:
IOS iOS
import WatchConnectivity
class ViewController: UIViewController, WCSessionDelegate{
override func awake(withContext context: Any?) {
super.viewDidLoad()
if WCSession.isSupported(){
WCSession.default.activate()
WCSession.default.delegate = self
}
}
}
watchOS watchOS
import WatchConnectivity
class InterfaceController: WKInterfaceController, WCSessionDelegate{
override viewDidLoad(){
super.viewDidLoad()
if WCSession.isSupported(){
WCSession.default.activate()
WCSession.default.delegate = self
let data = ["data1": 1, "data2": 2]
WCSession.default.sendMessage(data, replyHandler: {reply in
print("Got reply \(reply)")
}, errorHandler: {error in
print("Got error \(error)")
})
}
}
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
print("Received \(message)") // ["data1": 1, "data2": 2]
replyHandler(["messagedReceived": true])
}
}
The above code isn't tested, but should be more than enough to get you started. 上面的代码未经测试,但足以使您入门。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.