我创建了一个应用程序,其中数据显示在上一个视图的当前视图中。数据包括图像和三个标签。现在在当前视图中我有一个"发送到观看"按钮,它还将数据存储到数据库,同时它将传输显示的数据进行观察。我正在使用FMDB数据库来存储数据。如何将数据从IOS应用程序传输到观看应用程序。任何人都可以一步一步解释我,因为我是IOS开发的新手。我目前对故事板的看法如下: my storyboard view
当我运行我的应用时,其当前页面位于下方 application running page
我的上一个视图的源代码如下:
class TicketDetailViewController: UIViewController, WCSessionDelegate {
var databasePath = NSString()
var holding_Image: UIImage?
var hold_ticketName: String = ""
var hold_ticketDate: String = ""
var hold_ticketTime: String = ""
var session: WCSession!
@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!
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
let filemgr = NSFileManager.defaultManager()
let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docsDir = dirPaths[0]
var ticketDB: FMDatabase
databasePath = (docsDir as NSString).stringByAppendingPathComponent("tickets.sqlite")
if !filemgr.fileExistsAtPath(databasePath as String)
{
ticketDB = FMDatabase(path: databasePath as String)
if ticketDB.open()
{
let sql_stmt = "CREATE TABLE IF NOT EXISTS TICKETS (ID INTEGER PRIMARY KEY AUTOINCREMENT, IMAGE 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 sendToWatch(sender: UIButton) {
let ticketDB = FMDatabase(path: databasePath as String)
if ticketDB.open()
{
let insertSQL = "INSERT INTO TICKETS (image, ticket_type, date, time) VALUES ('\(display_image.image!)','\(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.presentViewController(alt, animated: true, completion: nil)
print(databasePath)
}
}
} }
答案 0 :(得分:0)
您可以使用应用程序组共享数据或提交该数据。 转到目标,点击功能,您可以找到“应用程序组”,只需启用它并添加“group.com.development.xyzapp”之类的条目,您可以通过该条目从iOS应用程序共享数据,反之亦然。您可以通过以下代码保存iOS应用中的数据。
- (void)sendDataToWatch
{
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.development.xyzapp"];
[userDefaults setObject:object forKey:@"itemone"];
[userDefaults setObject:object forKey:@"itemtwo"];
[userDefaults synchronize];
}
现在你可以看到这些数据。
希望这会有所帮助。