【问题标题】:Passing Data from Watch to iPhone using Appgroups使用 Appgroups 将数据从 Watch 传递到 iPhone
【发布时间】:2017-11-27 01:16:09
【问题描述】:

我正在做一个项目,使用 Appgroups 将数据从 Apple Watch 应用程序传递到 iPhone 应用程序。我的代码不工作,我不知道为什么。希望有人可以帮助我! :)

发送数据 Apple Watch

@IBAction func senddata() {
    let group = "group.pairedapp"
    let shared = UserDefaults(suiteName: group)
    let ok = "works"
    shared!.setValue(ok, forKey: "status")

    shared!.synchronize()    
}

在 iPhone 上获取数据

@IBAction func getWatchData(_ sender: Any) {    
    let group = "group.pairedapp"
    let shared = UserDefaults(suiteName: group)

    let get = shared!.value(forKey: "status")
    if get != nil {
        print("works")
    }
    else{
        print("OO NO!")
    }
}

【问题讨论】:

标签: ios swift nsuserdefaults watchkit appgroups


【解决方案1】:

自从引入watchOS2 以来,watchOS 应用程序不再被视为其iOS 对应项的扩展,因此您不能使用AppGroups 在两者之间共享数据。

您应该使用watchOS2+ 上的WatchConnectivity 框架在您的watchOSiOS 应用程序之间共享数据。

有关详细信息,请参阅 WatchKit 编程指南的 Sharing Data 部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多