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)
}
}
}