我试图在Swift 4中的iPhone和Apple Watch Apps之间传递一个数组,到目前为止,我一直没有成功。这是我尝试遵循的一些示例,但均未成功。在这些示例中,它们没有包含很多功能,但是当我尝试编写它时,它会在does not conform to protocols
声明监视会话时另外产生错误。它未正确声明,因此didRecieveApplicationContext
在Swift 4中与如其他问题/示例所示链接 Link2
这是我的iPhone ViewController
import UIKit
import WatchConnectivity
class watchtimetablemaker: UIViewController, WCSessionDelegate {
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
}
func sessionDidBecomeInactive(_ session: WCSession) {
}
func sessionDidDeactivate(_ session: WCSession) {
}
var watchSession: WCSession?
var initalArray = [String]()
var combination = ""
var vdlArray = [String]()
@IBAction func Save(_ sender: Any) {
vdlArray.removeAll()
//here I edit the arrays, add instances etc. cut out for fluency
self.view.endEditing(true)
sendToWatch()
UserDefaults.standard.set(vdlArray, forKey: "vdlarray")
UserDefaults.standard.synchronize()
print(initalArray)
print(vdlArray)
}
func sendToWatch(){
do{
let appDictionary = ["Array": initalArray]
try WCSession.default.updateApplicationContext(appDictionary)
}
catch {
print(error)
}
}
override func viewDidLoad() {
super.viewDidLoad()
if (WCSession.isSupported()){
watchSession = WCSession.default
watchSession?.delegate = self
watchSession?.activate()
}
vdlArray = UserDefaults.standard.array(forKey: "vdlarray") as! [String]
print(vdlArray)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
这是Apple Watch Controller
import WatchKit
import Foundation
import WatchConnectivity
class TimetableWatch: WKInterfaceController, WCSessionDelegate {
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
load()
}
@IBOutlet var tabletitle: WKInterfaceTable!
var watchSession: WCSession?
var tableData = [String]()
override func awake(withContext context: Any?) {
super.awake(withContext: context)
if (WCSession.isSupported()){
watchSession = WCSession.default
watchSession?.delegate = self
watchSession?.activate()
}
load()
tabletitle.setNumberOfRows(tableData.count, withRowType: "Cell")
var i = 0
for item in tableData {
let row = tabletitle.rowController(at: i) as! TableCellRow
row.tbalecelllabel.setText(item)
i = i + 1
}
}
func load(){
func session(session: WCSession, didRecieveApplicationContext applicationContext: [String : Any]) {
DispatchQueue.main.async() { () -> Void in
if let retreivedArray = applicationContext["Array"] as? [String] {
self.tableData = retreivedArray
print(self.tableData)
}
}
}
}
override func willActivate() {
super.willActivate()
}
override func didDeactivate() {
super.didDeactivate()
}
}
我找不到与swift4相关的任何文档,因此,我们将不胜感激。
在WKInterfaceController
:
我同意詹斯·彼得(Jens Peter)的意见,即您不应该didReceiveApplicationContext
使用load()函数。
这样处理错误activationDidCompleteWith
:
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if let error = error {
print("Activation failed with error: \(error.localizedDescription)")
return
}
print("Watch activated with state: \(activationState.rawValue)")
}
这也将有助于确定其是否WatchConnectivity
已经确立。
删除var watchSession: WCSession?
完全并宣布WCSession
在awake(withContext)
为:
if WCSession.isSupported() {
let watchSession = WCSession.default
watchSession = self
watchSession()
}
同时删除对load()
in的呼叫awake(withContext)
。
在InterfaceController
它有助于把一些print
语句在WCSession存根,即使你没有完全使用它们。您可以查看WatchConnectivity是否正在激活。
func sessionDidBecomeInactive(_ session: WCSession) {
// To support multiple watches.
print("WC Session did become inactive.")
}
func sessionDidDeactivate(_ session: WCSession) {
// To support multiple watches.
WCSession.default.activate()
print("WC Session did deactivate.")
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if let error = error {
print("WC Session activation failed with error: \(error.localizedDescription)")
return
}
print("Phone activated with state: \(activationState.rawValue)")
}
WCSession.default.activate()
如您所见,这条线sessionDidDeactivate
将在用户碰巧拥有多个手表的情况下提供多种手表支持。
var watchSession: WCSession?
像在中一样删除WKInterfaceController
。再次viewDidLoad()
检查是否有以下方面的WCSession
支持:
if WCSession.isSupported() {
let watchSession = WCSession.default
watchSession = self
watchSession()
}
更新sendToWatch()
至:
func sendToWatch() {
let session = WCSession.default()
if session.activationState == .activated {
let appDictionary = ["Array": initalArray]
do {
try session.updateApplicationContext(appDictionary)
} catch {
print(error)
}
}
}
我认为这可以清理一下,并有望奏效或至少使您前进。但是我没有在项目中构建它。希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句