1

I am trying to send a value from my watch extension to my iPhone app. From various sources, it seems the best way is to use UserDefaults, however I am getting some weird errors and i'm not able to retrieve the data I set into the userDefaults. I am using app groups and that has all been setup correctly. Here is my code

Watch Extension:

@IBAction func okBtnClicked() {
        print("Ok")

        let userDefaults = UserDefaults(suiteName: "group.iFindUserApp")
        //userDefaults?.setValue("OK", forKey: "status")

        userDefaults?.set(10, forKey: "testInt")
        userDefaults?.synchronize()
    }

And here is my viewcontroller in the iPhone app:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! FriendTableViewCell
        //cell.configureCell(user: usersArr[indexPath.row])
        let user = usersArr[indexPath.row]
//        cell.nameLabel.text = "\(user.firstName) \(user.lastName)"
        var defaults = UserDefaults(suiteName: "group.iFindUserApp")
        var test = defaults?.value(forKey: "testInt")
        cell.nameLabel.text = "\(test)"
        return cell
    }

The specific error I get is Using kCFPreferencesAnyUser with a container is only allowed for System Containers, detaching from cfprefsd

I tried changing my groupName to TeamId.groupName, and that just silenced the warning, but I was still unable to get a value (I just got nil).

Any help would be greatly appreciated!

Varun Vu
  • 305
  • 2
  • 6
  • 14
  • The best way isn't using `UserDefaults` with groups, is using the `WatchConnectivity` framework – Victor Sigler Nov 30 '16 at 02:11
  • Can you post a solution on how to do it using `WatchConnectivity`? – Varun Vu Nov 30 '16 at 04:28
  • From WatchOS2, you can no longer do this (appgroup). You must use WatchConnectivity only. – GeneCode Nov 30 '16 at 05:21
  • You should use updateApplicationContext(), sendMessage() etc. See the documentation: https://developer.apple.com/library/content/documentation/General/Conceptual/WatchKitProgrammingGuide/SharingData.html#//apple_ref/doc/uid/TP40014969-CH29-SW1 ( – Jens Peter Dec 01 '16 at 10:36

0 Answers0