swift - Why isn't iPhone transferring an array to Apple Watch? -
i have array stored on iphone need sent watch in background whenever updated on phone. i've followed tutorial on raywenderlich.com can't seem work.
phone code:
import uikit import foundation import watchconnectivity class viewcontroller: uiviewcontroller, wcsessiondelegate { var array1 = ["hi", "this", "is", "a", "test"]() var array2 = ["1", "2", "3", "4", "5"]() var session: wcsession? private func startsession() { if wcsession.issupported() { session = wcsession.defaultsession() session?.delegate = self session?.activatesession() } } private func sendtowatch() { if let session = session session.reachable { session.transferuserinfo(["array1": array1]) } if let session = session session.reachable { session.transferuserinfo(["array2": array2]) } } sendtowatch() }
and watch code:
import watchkit import foundation import watchconnectivity var array1 = [string]() var array2 = [string]() class interfacecontroller: wkinterfacecontroller, wcsessiondelegate { var session: wcsession? @iboutlet var table: wkinterfacetable! override func awakewithcontext(context: anyobject?) { super.awakewithcontext(context) startsession() table!.setnumberofrows(array1.count, withrowtype: "tablerowcontroller") var index = 0 while index < array1.count { let row = table.rowcontrolleratindex(index) as! tablerowcontroller row.rowlabel.settext(array1[index]) row.datelabel.settext(array2[index]) index++ } } private func startsession() { if wcsession.issupported() { session = wcsession.defaultsession() session?.delegate = self session?.activatesession() } } func session(session: wcsession, didreceiveuserinfo userinfo: [string : anyobject]) { if let received1 = userinfo["array1"] as? [string] { dispatch_async(dispatch_get_main_queue(), { () -> void in array1 = received1 }) } if let received2 = userinfo["array2"] as? [string] { dispatch_async(dispatch_get_main_queue(), { () -> void in array2 = received2 }) } } }
the table on watch works fine if enter dummy data arrays, know set correctly. i'm having problems transfer/receiving.
second attempt:
i'm trying use updateapplicationcontext()
opposed transferuserinfo()
. i've removed session.reachable
check , changed code.
phone code:
private func sendtowatch() { { let applicationdict = ["array1": array1] let applicationdict2 = ["array2": array2] try wcsession.defaultsession().updateapplicationcontext(applicationdict) try wcsession.defaultsession().updateapplicationcontext(applicationdict2) } catch { print(error) } }
watch code:
func session(session: wcsession, didreceiveapplicationcontext applicationcontext: [string : anyobject]) { //i can't figure out how retrieve data! print(applicationcontext) // returns nil array1 = applicationcontext["array1"] as! [string] //returns nil , crashes app array2 = applicationcontext["array2"] as! [string] //returns nil , crashes app }
third attempt:
i'm able sometimes either array1 or array2 appear on watch. of course, crashes app both arrays not received. can me this?
watch code:
func session(session: wcsession, didreceiveapplicationcontext applicationcontext: [string : anyobject]) { dispatch_async(dispatch_get_main_queue()) { () -> void in if let retrievedarray1 = applicationcontext["array1"] as? [string] { array1 = retrievedarray1 print(array1) } if let retrievedarray2 = applicationcontext["array2"] as? [string] { array2 = retrievedarray2 print(array2) } self.table!.setnumberofrows(array1.count, withrowtype: "tablerowcontroller") var index = 0 while index < array1.count { let row = self.table.rowcontrolleratindex(index) as! tablerowcontroller row.rowlabel.settext(array1[index]) //my crash happens either here or.... row.datelabel.settext(array2[index]) //here because both arrays aren't being received , unreceived array out of index index++ } } }
session.reachable true when watch app in foreground (there few rare exceptions).
if want send data watch app in background, should using updateapplicationcontext
, transferuserinfo
or transferfile
(which 1 choose depends on sending , size).
so like:
updated code in response 3rd edit/question originator
phone code:
private func sendtowatch() { { let applicationdict = ["array1": array1, "array2": array2] try wcsession.defaultsession().updateapplicationcontext(applicationdict) } catch { print(error) } }
watch code:
func session(session: wcsession, didreceiveapplicationcontext applicationcontext: [string : anyobject]) { dispatch_async(dispatch_get_main_queue()) { () -> void in if let retrievedarray1 = applicationcontext["array1"] as? [string] { array1 = retrievedarray1 print(array1) } if let retrievedarray2 = applicationcontext["array2"] as? [string] { array2 = retrievedarray2 print(array2) } self.table!.setnumberofrows(array1.count, withrowtype: "tablerowcontroller") var index = 0 while index < array1.count { let row = self.table.rowcontrolleratindex(index) as! tablerowcontroller row.rowlabel.settext(array1[index]) row.datelabel.settext(array2[index]) index++ } } }
Comments
Post a Comment