簡體   English   中英

使用Appgroup將數據從Watch傳遞到iPhone

[英]Passing Data from Watch to iPhone using Appgroups

我正在一個項目中,該數據使用Appgroup將數據從Apple Watch App傳遞到iPhone App。 我的代碼無法正常工作,我不確定為什么。 希望有人可以幫助我! :)

發送數據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!")
    }
}

由於watchOS2的引入, watchOS應用程序不再僅被視為其iOS對應程序的擴展,因此您不能使用AppGroups在兩者之間共享數據。

您應該在watchOS2+上使用WatchConnectivity框架在watchOSiOS應用之間共享數據。

有關更多信息,請參見《 WatchKit編程指南》中的“ 共享數據”部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM