簡體   English   中英

iPhone為什么不將陣列傳輸到Apple Watch?

[英]Why isn't iPhone transferring an array to Apple Watch?

我在iPhone上存儲了一個數組,無論何時在手機上對其進行更新,都需要在后台將其發送到Watch。 我已經關注了RayWenderlich.com上的教程,但似乎無法正常工作。

電話代碼:

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

觀看代碼:

func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {

    //I can't figure out how to retrieve the data!

    print(applicationContext) // returns nil 
    array1 = applicationContext["Array1"] as! [String] //returns nil and crashes app
    array2 = applicationContext["Array2"] as! [String] //returns nil and crashes app

}

第三嘗試:

我現在有時可以 array1或array2出現在Watch上。 當然,由於未成功接收兩個陣列,因此它會使應用程序崩潰。 有人可以幫我嗎?

觀看代碼:

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++

        }

    }

}

關於您的第三次嘗試:

您不想使用updateApplicationContext-您想要使用transferUserInfo,如其他建議的那樣。

每個updateApplicationContext調用將覆蓋已發送到手表的所有先前數據。 當您在dict1之后立即發送dict2時,第一個詞典將被丟棄並替換為dict2。 您不能通過updateApplicationContext以這種方式獲得兩個字典。

例如,您將使用updateApplicationContext來傳遞應用程序中僅最新設置很重要的設置或選項。 例如,如果您有一個可以在iOS應用程序中更改的標志,則每次在iOS應用程序中更改該標志時,都將使用updateApplicationContext。 最終加載watch應用程序時,它將僅看到didReceiveApplicationContext中標志的最后設置。

現在設置您的第三次嘗試的方式,將發送第一個字典,然后立即發送第二個字典。 您看到基於這兩個詞典的交付中的可變滯后時間的間歇性故障。

如果在完全傳輸第二個字典之前調用didReceiveApplicationContext,則您將成功檢索字典1,因為該數據尚未被覆蓋。 當它嘗試訪問索引“ Array 2”時會失敗,因為它還不存在。 如果第二個上下文在手表到達檢索“ Array 1”調用之前被傳輸,則您的應用將在那里失敗,因為最后收到的數據不是鍵入“ Array1”,而是鍵入“ Array2”。

使用updateUserInfo進行的調用不會覆蓋之前的調用。 加載監視應用程序后,它將為每個調用觸發一次didReceiveUserInfo代碼。 通話的順序得以保持,因此,如果您從iOS應用發送了dict1,dict2和dict3,則手表應用將按照該順序進行發送。

session.reachable僅在Watch應用程序位於前台時才為true(有一些罕見的例外)。

如果要在后台將數據發送到watch應用程序,則應使用updateApplicationContexttransferUserInfotransferFile (選擇哪種取決於您要發送的內容及其大小)。

所以像這樣:

更新了代碼以響應創建者的第三次編輯/問題

電話代碼:

private func sendToWatch() {
do {
        let applicationDict = ["Array1": array1, "Array2": array2]
        try WCSession.defaultSession().updateApplicationContext(applicationDict)
    }

catch {
        print(error)
    }
}

觀看代碼:

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])

            row.dateLabel.setText(array2[index])

            index++

        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM