发送标签数据和图像以在swift中观看App

时间:2016-08-25 06:04:59

标签: ios swift sqlite fmdb watch-os

我有一个IOS应用程序,其中我有4个标签和1个imageView,其中数据显示在上一个视图中。当我点击" sendtowatch"我希望在观看应用中显示相同的数据。按钮。即,数据将从FMDB数据库中获取,然后它将显示在watch app中。在手表应用程序中,我还拍摄了4个标签和1个图像视图。任何人都可以帮助我吗?

screenshot for my app

let ticketInfo = NSMutableDictionary()

类TicketDetailViewController:UIViewController,WCSessionDelegate {

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

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

    display_image.image = 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 as NSString).stringByAppendingPathComponent("ticket_Pass.sqlite")


    if !filemgr.fileExistsAtPath(databasePath as String)
    {

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

            let sql_stmt = "CREATE TABLE IF NOT EXISTS TICKET (ID INTEGER PRIMARY KEY AUTOINCREMENT, IMAGE 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())")
        }
    }
}

@IBAction func addTickets(sender:UIButton)   {

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

            let insertSQL = "INSERT INTO TICKET (image, ticket_category, ticket_type, date, time) VALUES ('\(display_image.image!)', '\(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

                         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 you 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 as String)
    if ticketDB.open()
    {

        let insertSQL = "INSERT INTO TICKET (image, ticket_category, ticket_type, date, time) VALUES ('\(display_image.image!)', '\(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 result: FMResultSet? = ticketDB.executeQuery(querySQL, withArgumentsInArray: nil)

                        if let result = result
                        {
                            self.ticketDataArray = NSMutableArray()
                            while result.next()
                            {

                                ticketInfo.setObject(result.stringForColumn("image"), forKey: "image")
                                ticketInfo.setObject(result.stringForColumn("ticket_category"), forKey: "ticket_category")
                                ticketInfo.setObject(result.stringForColumn("ticket_type"), forKey: "ticket_type")
                                ticketInfo.setObject(result.stringForColumn("date"), forKey: "date")
                                ticketInfo.setObject(result.stringForColumn("time"), forKey: "time")
                                self.ticketDataArray.addObject(ticketInfo)
                               print("ticketDataArray :  \(ticketInfo)")

                            }


                        }else
                        {
                            print("Error: \(ticketDB.lastErrorMessage())")

                        }

                    ticketDB.close()

                    }

                        let dict = ["Watchdat":ticketInfo]

                    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)

        }
    }

观看代码

class InterfaceController: WKInterfaceController, WCSessionDelegate {

var session: WCSession!



@IBOutlet var watch_displayImage: WKInterfaceImage!
@IBOutlet var watch_ticket_category: WKInterfaceLabel!
@IBOutlet var watch_ticketType: WKInterfaceLabel!
@IBOutlet var watch_ticketTime: WKInterfaceLabel!
@IBOutlet var watch_ticketDate: WKInterfaceLabel!

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)

    if WCSession.isSupported()
    {

        session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()

    }


}

override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()


}


func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
    print(message.values)
    watch_ticket_category.setText("worked")
}

0 个答案:

没有答案