我有一個數組存儲在我的iPhone上,無論何時在手機上更新後,我都需要將其發送到手錶中。我在RayWenderlich.com上關注了一個教程,但我似乎無法完成它的工作。爲什麼iPhone沒有將數組傳送給Apple Watch?
電話代碼:
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 where session.reachable {
session.transferUserInfo(["Array1": array1])
}
if let session = session where session.reachable {
session.transferUserInfo(["Array2": array2])
}
}
sendToWatch()
}
,觀看代碼:
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
})
}
}
}
如果我輸入虛擬數據到陣列上的手錶的表工作正常,所以我知道一切都正確設置。我真的只有轉移/接收方面的問題。
第二次嘗試:
我現在試圖用updateApplicationContext()
而不是transferUserInfo()
。我刪除了session.reachable
檢查並更改了一些代碼。
電話代碼:
private func sendToWatch() {
do {
let applicationDict = ["Array1": array1]
let applicationDict2 = ["Array2": array2]
try WCSession.defaultSession().updateApplicationContext(applicationDict)
try WCSession.defaultSession().updateApplicationContext(applicationDict2)
}
catch {
print(error)
}
}
關注代碼:
第三次嘗試:
我現在能夠有時得到要麼數組1或數組2至出現在手錶上。當然,它會崩潰應用程序,因爲兩個數組都沒有被成功接收。任何人都可以幫助我嗎?
關注代碼:當手表應用程序是在前臺
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 and unreceived array is out of index
index++
}
}
}
你可以發佈你的會議爲iOS應用設置代碼嗎? – ccjensen
謝謝,我剛剛做到了! – bkwebhero
updateApplicationContext僅傳輸最近的字典,這是它的工作方式。你想使用transferUserInfo或者把array1和array2放到同一個字典中並調用updateApplicationContext一次 – ccjensen