简体   繁体   English

即使使用AppGroup,我的Iwatch也无法从ios App中保存数据

[英]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.

相关问题 即使我要求在后台更新位置,为什么我的iOS应用程序会被杀死? - Why does my iOS app get killed even though I ask for location updates in the background? 即使我收到确认,图片也不会保存 - Image doesn't get saved, even though I get a confirmation 即使我续订了证书,发布我的iOS应用程序也会失败并显示“证书已过期” - Publishing my iOS app fails with “Certificate expired” even though I renewed my certificate 即使更新了我的iOS设备,我也无法运行我的应用程序 - I cannot run my app on my iOS devices even though they are updated 有没有办法授权我的应用程序访问 iWatch HealthKit 数据? - Is there a way of authorizing my app to access iWatch HealthKit data? 我可以从iWatch扩展程序访问Healthkit数据吗? - Can I access healthkit data from iWatch extension? AppGroup中的NSUserDefaults在Watch App中为空 - NSUserDefaults from AppGroup is empty in Watch App 即使我清除了数组,iOS UITextViews数据仍然存在 - iOS UITextViews data lingering even though I clear out an array 即使我没有在我的应用程序中使用APNS,APNS也会向苹果发出警告 - APNS warning from apple even though I am not using APNS in my app iOS App Store Connect 现在需要 iPad 屏幕截图,即使应用程序不支持 iPad - iOS App Store Connect is now requiring iPad screen shots even though app does not support iPad
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM