快速将数据从iOS发送到Apple Watch

时间:2016-08-31 06:50:21

标签: ios swift apple-watch

在我的应用程序中,当我在模拟器中运行它时,数据正被传输到监视应用程序。数据包括四个UILabels和一个UIImageView。但是当我在真正的iPhone上运行时,除了UIImageView之外,数据正在传输。有人能帮助我吗?

screenshot for my watch apple simulator

我的iOS应用的源代码是:

func saveImageToFolder()
{

    let imageData:NSData = UIImagePNGRepresentation(holding_Image!)!
    let imageName = "image_\(NSDate.timeIntervalSinceReferenceDate()).png"
    let imagePath = (self.getDocumentsDirectory() as NSString).stringByAppendingPathComponent(imageName)
    print("imagePath:- \(imagePath)")

    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 sendToWatch(sender: AnyObject)
{

    let ticketDB = FMDatabase(path: databasePath as String)
    if ticketDB.open()
    {
        self.saveImageToFolder()
        let insertSQL = "INSERT INTO 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 is saved to Database!", image: UIImage(named: ""), style: .Alert)

            alt.addAction(PMAlertAction(title: "OK!", style: .Default, action:
            { (ACTION) -> Void in


                    let ticketDB = FMDatabase(path: self.databasePath as String)
                    if ticketDB.open()
                    {

//                        let querySQL = "SELECT * FROM TICKET ORDER BY ID DESC LIMIT 1"
                         let querySQL = "SELECT * FROM TICKET "

                        let result: FMResultSet? = ticketDB.executeQuery(querySQL, withArgumentsInArray: nil)

                        if let result = result
                        {

                           // self.ticketDataArray = NSMutableArray()
                            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)

        }
    }

我的观看代码是

func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void)
{

    print(message.values)

    MessageData = NSMutableDictionary(object: message, forKey: "data")
    print("Message data---",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)")

}

0 个答案:

没有答案