ios - Sending data to apple Watch in swift -


in app, sending data (i.e 'uilabels' , 'uiimage') apple watch. being sent. in watch main view data being displayed sent ios app, have button opens table view in have taken 1 imageview , 4 labels. want same data displayed in table view i.e every time sent data ios app watch app, each entry should displayed have sent ios app , should saved. want data displayed same being saved in ios app below image ios app image:

watch app simulator layout

source code ios app

class ticketdetailviewcontroller: uiviewcontroller, wcsessiondelegate {     let dic1  = nsmutabledictionary()     var defaults = nsuserdefaults()     var databasepath = nsstring()     var holding_ticket_category: string = ""     var holding_image: uiimage?     var hold_ticketname: string = ""     var hold_ticketdate: string = ""     var hold_tickettime: string = ""     var session: wcsession!     var imagedata: nsdata!      @iboutlet weak var ticket_grey: uiimageview!     @iboutlet weak var cropped_frame: uiimageview!     @iboutlet weak var display_image: uiimageview!     @iboutlet weak var ticket_type_name: uilabel!     @iboutlet weak var ticket_date: uilabel!     @iboutlet weak var ticket_time: uilabel!     @iboutlet weak var ticket_category: uilabel!      override func viewdidload()     {         super.viewdidload()         if wcsession.issupported(){             self.session  = wcsession.defaultsession()             self.session.delegate = self             self.session.activatesession()         }          defaults = nsuserdefaults.standarduserdefaults()         display_image.image = self.holding_image         ticket_type_name.text = hold_ticketname         ticket_date.text = hold_ticketdate         ticket_time.text = hold_tickettime         ticket_category.text = holding_ticket_category          let filemgr =  nsfilemanager.defaultmanager()         let dirpaths = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true)         let docsdir = dirpaths[0]         var ticketdb: fmdatabase          databasepath = (docsdir nsstring).stringbyappendingpathcomponent("ticket_pass.sqlite")          if !filemgr.fileexistsatpath(databasepath string)         {                  ticketdb = fmdatabase(path: databasepath string)             if ticketdb.open()             {                   let sql_stmt = "create table if not exists ticket (id integer primary key autoincrement, imagepath text, imagename text, ticket_category text, ticket_type text, date text, time text)"                 if !ticketdb.executestatements(sql_stmt)                 {                     print("error: \(ticketdb.lasterrormessage())")                 }                 ticketdb.close()             } else             {                  print("error: \(ticketdb.lasterrormessage())")             }         }else{             ticketdb = fmdatabase(path: databasepath string)             if ticketdb.open()             {                 let sql_stmt = "create table if not exists ticket (id integer primary key autoincrement, imagepath text, imagename text, ticket_category text, ticket_type text, date text, time text)"                 if !ticketdb.executestatements(sql_stmt)                 {                     print("error: \(ticketdb.lasterrormessage())")                 }                 ticketdb.close()             } else             {                  print("error: \(ticketdb.lasterrormessage())")             }         }     }      func saveimagetofolder()     {          imagedata = uiimagepngrepresentation(holding_image!)!         let imagename = "image_\(nsdate.timeintervalsincereferencedate()).png"         let imagepath = (self.getdocumentsdirectory() nsstring).stringbyappendingpathcomponent(imagename)         print("imagepath:- \(imagepath)")         print("image size: \(holding_image?.size)")          imagedata.writetofile(imagepath, atomically: true)         nsuserdefaults.standarduserdefaults().setobject(imagepath, forkey: "imagepath")         nsuserdefaults.standarduserdefaults().setobject(imagedata, forkey: "imagedata")         nsuserdefaults.standarduserdefaults().setobject(imagename, forkey: "imagename")         nsuserdefaults.standarduserdefaults().synchronize()        }      func getdocumentsdirectory() -> string{         var paths = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true)         let documentspath = paths[0]         print("document path:- \(documentspath)")         return documentspath      }      @ibaction func addtickets(sender: uibutton)     {         let ticketdb = fmdatabase(path: databasepath string)         if ticketdb.open()         {                 //self.saveimagetofolder()                 let insertsql = "insert ticket (imagepath, imagename, ticket_category, ticket_type, date, time) values ('\(nsuserdefaults.standarduserdefaults().objectforkey("imagepath")!)', '\(nsuserdefaults.standarduserdefaults().objectforkey("imagename")!)', '\(ticket_category.text!)', '\(ticket_type_name.text!)', '\(ticket_date.text!)', '\(ticket_time.text!)')"                  let result = ticketdb.executeupdate(insertsql,withargumentsinarray: nil)                  if !result                 {                         print("error: \(ticketdb.lasterrormessage())")                  } else                 {                     let alt = pmalertcontroller(title: "success!", description: "your data saved database!", image: uiimage(named: ""), style: .alert)                      alt.addaction(pmalertaction(title: "ok!", style: .default, action: { (action) -> void in                      self.navigationcontroller?.poptorootviewcontrolleranimated(true)                      }))                      self.presentviewcontroller(alt, animated: true, completion: nil)                     print(databasepath)                }         }     }      @ibaction func deletetickets(sender: uibutton) {         let alt = pmalertcontroller(title: "delete ticket details!", description: "are sure?", image: uiimage(named: ""), style: .alert)          alt.addaction(pmalertaction(title: "cancel", style: pmalertactionstyle.default, action: { (action) -> void in              self.dismissviewcontrolleranimated(true, completion: nil)          }))         alt.addaction(pmalertaction(title: "ok", style: pmalertactionstyle.default, action: { (action) -> void in              self.navigationcontroller?.poptorootviewcontrolleranimated(true)         }))         self.presentviewcontroller(alt, animated: true, completion: nil)     }       @ibaction func sendtowatch(sender: anyobject)     {          let ticketdb = fmdatabase(path: databasepath string)         if ticketdb.open()         {             self.saveimagetofolder()             let insertsql = "insert ticket (imagepath, imagename, ticket_category, ticket_type, date, time) values ('\(nsuserdefaults.standarduserdefaults().objectforkey("imagepath")!)', '\(nsuserdefaults.standarduserdefaults().objectforkey("imagename")!)', '\(ticket_category.text!)', '\(ticket_type_name.text!)', '\(ticket_date.text!)', '\(ticket_time.text!)')"              let result = ticketdb.executeupdate(insertsql,withargumentsinarray: nil)              if !result             {                  print("error: \(ticketdb.lasterrormessage())")              } else             {                  let alt = pmalertcontroller(title: "success!", description: "your data saved!", image: uiimage(named: ""), style: .alert)                  alt.addaction(pmalertaction(title: "ok!", style: .default, action:                 { (action) -> void in                           let ticketdb = fmdatabase(path: self.databasepath string)                         if ticketdb.open()                         {  //                            let querysql = "select * ticket order id desc limit 1"                              let querysql = "select * ticket "                              let result: fmresultset? = ticketdb.executequery(querysql, withargumentsinarray: nil)                              if let result = result                             {                                  let ticketdataarray:nsmutablearray = nsmutablearray()                                 while result.next()                                 {                                      self.dic1.setobject(result.stringforcolumn("imagepath"), forkey: "imagepath")                                     self.dic1.setobject(result.stringforcolumn("imagename"), forkey: "imagename")                                     self.dic1.setobject(result.stringforcolumn("ticket_category"), forkey: "ticket_category")                                     self.dic1.setobject(result.stringforcolumn("ticket_type"), forkey: "ticket_type")                                     self.dic1.setobject(result.stringforcolumn("date"), forkey: "date")                                     self.dic1.setobject(result.stringforcolumn("time"), forkey: "time")                                      ticketdataarray.addobject(self.dic1)                                  }                                 print("ticketdataarray :  \(self.dic1)")                             }else                             {                                 print("error: \(ticketdb.lasterrormessage())")                              }                            ticketdb.close()                          }                       self.navigationcontroller?.poptorootviewcontrolleranimated(true)                         if wcsession.defaultsession().reachable                         {                             let dict = ["watchdat":self.dic1]                              self.session.sendmessage(dict, replyhandler: { (replaydic: [string:anyobject]) in                                 print("success!")                                 }, errorhandler: { (error:nserror) in                                     print("error")                             })                         }                 }))                  self.presentviewcontroller(alt, animated: true, completion: nil)                 print(databasepath)             }         } 

source code watch

func session(session: wcsession, didreceivemessage message: [string : anyobject], replyhandler: ([string : anyobject]) -> void) {     //print(message.values)      messagedata = nsmutabledictionary(object: message, forkey: "data")     print("\(messagedata)")     watch_ticket_category.settext("\(messagedata["data"]!["watchdat"]!!["ticket_category"] as! string)")     watch_displayimage.setimage(uiimage(named: messagedata["data"]!["watchdat"]!!["imagepath"] as! string))     watch_tickettype.settext("\(messagedata["data"]!["watchdat"]!!["ticket_type"] as! string)")     watch_tickettime.settext("\(messagedata["data"]!["watchdat"]!!["time"] as! string)")     watch_ticketdate.settext("\(messagedata["data"]!["watchdat"]!!["date"] as! string)")     } 


Comments