0

I have a problem with WatchConnectivity.

In my App, I send a Dictionary with Data to my AppleWatch. Everything works on my Watch. There the User can change Some Data (I have a List with Artists where the user can check or uncheck the Artists). After a change the List should be send back to my iPhone via WatchConnectivity. I use the function updateApplicationContext. In my AppDelegate Class the Dictionary is being read out correctly, but after that i want to set my Artists to the NSUserDefaults, but if I debug, there are no changed Values in my NSUserDefaults.

Anyone has an Idea how to send Complex Objects like Arrays etc. back to the Phone.

Here is my AppDelegate

class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {

var window: UIWindow?


func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]){


    if(applicationContext["myUserProfile"] != nil){
        Utils().importMyUserProfileFromSession(applicationContext, getCompletionHandler: {(artists) -> Void in

            Utils().saveArtists(artists, key: "profileArtistsFromWatch")
            //in artists  the Values are correct, so i save them here
            //but if i want to read them out(load and save Function works!) the Array is empty...
            let artists1 = Utils().loadArtists("profileArtistsFromWatch")
            })   
    }
}

}

Here is my Function importMyUserProfileFromSession

  func importMyUserProfileFromSession(applicationContext: [String: AnyObject], getCompletionHandler : (artists: [Artist]) -> Void){
    let me = self.getProfileData()
    var myArtists = me.artists


    if(applicationContext["myUserProfile"] != nil){
        let dictUser :[String: AnyObject] = applicationContext["myUserProfile"] as! [String: AnyObject]
        // print(dictUser)
        var artists : [Artist] = [Artist]()
        if dictUser["artists"] != nil{
            let dictArtists :[Int: AnyObject] = dictUser["artists"] as! [Int: AnyObject]
            for j in 0..<dictArtists.count{
                let dictArtist :[String: AnyObject] = dictArtists[j] as! [String: AnyObject]
                var a : Artist = Artist()
                if dictArtist["id"] != nil{
                    let indexOfArtist = self.getArtistById(dictArtist["id"] as! Int, artists: myArtists)
                    if(indexOfArtist != -1){
                        a.id = myArtists[indexOfArtist].id as! Int
                        print(a.id)
                    }
                    a.idInApp = dictArtist["id"] as! Int
                }
                if dictArtist["name"] != nil{
                    a.name = dictArtist["name"] as! String
                }
                if dictArtist["image"] != nil{
                    let imageData: NSData = dictArtist["image"] as! NSData
                    a.imageData = imageData

                }
                if dictArtist["active"] != nil{
                    let active: Bool = dictArtist["active"] as! Bool
                    a.display = active

                }

                artists.append(a)
            }
            getCompletionHandler(artists: artists)   
        }
    }

}
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
hannes
  • 519
  • 4
  • 11
  • 23
  • If `artists` holds the new values, the problem is likely in `saveArtists()` or `loadArtists()`. You should [examine what actually was saved in `NSUserDefaults`](http://stackoverflow.com/q/1676938/4151918). –  Feb 24 '16 at 01:03
  • You do know that `standardUserDefaults` is different for watch and phone? In order for this to work you want App Groups enable, on the watchExtension and the phone, and use `[[NSUserDefaults alloc]initWithSuiteName:APP_GROUP_ID]` instead of just `[NSUserDefaults standardUserDefaults]` – 4oby Feb 24 '16 at 08:56
  • I trink i found the error. I saved my Artistids, i got Form facebook, AS Integer. And i think threy were to big to save AS an Integer, so i saved them As a String, i trink now it works... Stupid Problem...but thanks for your answer. – hannes Feb 24 '16 at 08:59
  • Watch Groups are deprecated in WatchOS 2, i tried that yesterday, but it does not work anymore... – hannes Feb 24 '16 at 09:00

0 Answers0