gpt4 book ai didi

ios - WatchConnectivity 通过在 iPhone 上启动计时器来发送 watch 消息

转载 作者:行者123 更新时间:2023-11-30 13:58:37 26 4
gpt4 key购买 nike

我创建了一个在 iPhone 上运行的计时器应用程序。我希望我们可以控制它 iPhone 和 Watch

Projet Screenshot

iPhone 上的控件(播放、停止、重新启动)工作正常,我的仪表显示在 watch 上。

watch 在“停止”上运行良好,但“开始”不起作用,仪表无法打开 iPhone 或 watch 。

也重新启动工作。

如果信息来自 watch ,iPhone 上的“我的标签”更改速度非常慢,但在另一个方向(即 iPhone Watch)上效果很好

你注意到这个问题了吗,这是一个与WatchConnectivity相关的问题

感谢您的帮助

下面是我的代码:

ViewController.swift

import UIKit
import WatchConnectivity

class ViewController: UIViewController, WCSessionDelegate {

@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var watchLabel: UILabel!

var session: WCSession!
var timerCount = 0
var timerRunning = false
var timer = NSTimer()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if (WCSession.isSupported()) {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()

if session.paired != true {
print("Apple Watch is not paired")
}

if session.watchAppInstalled != true {
print("WatchKit app is not installed")
}
} else {
print("WatchConnectivity is not supported on this device")
}

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


@IBAction func startButton(sender: UIButton) {
startPlay()
}

@IBAction func stopButton(sender: UIButton) {
stopPlay()
}


@IBAction func restartButton(sender: UIButton) {
restartPlay()
}

//Receive messages from watch
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
var replyValues = Dictionary<String, AnyObject>()

//let viewController = self.window!.rootViewController as! ViewController
switch message["command"] as! String {
case "start" :
startPlay()
replyValues["status"] = "Playing"
case "stop" :
stopPlay()
replyValues["status"] = "Stopped"
case "restart" :
restartPlay()
replyValues["status"] = "Stopped"
default:
break
}
replyHandler(replyValues)
}

//Counter Timer
func counting(timer:NSTimer) {
self.timerCount++
self.timerLabel.text = String(timerCount)

let requestValues = ["timer" : String(timerCount)]
let session = WCSession.defaultSession()
session.sendMessage(requestValues, replyHandler: nil, errorHandler: { error in print("error: \(error)")})
}

//Fonction Play
func startPlay() {
if timerRunning == false {
self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("counting:"), userInfo: nil, repeats: true)
self.timerRunning = true
self.watchLabel.text = "START"
}
}

//Fonction Stop
func stopPlay() {
if timerRunning == true {
self.timer.invalidate()
self.timerRunning = false
self.watchLabel.text = "STOP"
}
}

//Fonction Restart
func restartPlay() {
self.timerCount = 0
self.timerLabel.text = "0";

let requestValues = ["timer" : "0"]
let session = WCSession.defaultSession()
session.sendMessage(requestValues, replyHandler: nil, errorHandler: { error in print("error: \(error)")})
}
}

InterfaceController.swift

import WatchKit
import Foundation
import WatchConnectivity


class InterfaceController: WKInterfaceController, WCSessionDelegate {

@IBOutlet var watchLabel: WKInterfaceLabel!
@IBOutlet var statusLabel: WKInterfaceLabel!

//Receiving message from iphone
func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
self.watchLabel.setText(message["timer"]! as? String)
// self.statusLabel.setText(message["command"]! as? String)
}


override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}

override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
if (WCSession.isSupported()) {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}

override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}


@IBAction func startButtonWatch() {
if WCSession.defaultSession().reachable == true {
let requestValues = ["command" : "start"]
let session = WCSession.defaultSession()
session.sendMessage(requestValues, replyHandler: { reply in
self.statusLabel.setText(reply["status"] as? String)
}, errorHandler: { error in
print("error: \(error)")
})

}

}


@IBAction func stopButtonWatch() {
if WCSession.defaultSession().reachable == true {
let requestValues = ["command" : "stop"]
let session = WCSession.defaultSession()
session.sendMessage(requestValues, replyHandler: { reply in
self.statusLabel.setText(reply["status"] as? String)
}, errorHandler: { error in
print("error: \(error)")
})

}
}

@IBAction func restartButtonWatch() {
if WCSession.defaultSession().reachable == true {
let requestValues = ["command" : "restart"]
let session = WCSession.defaultSession()
session.sendMessage(requestValues, replyHandler: { reply in
self.statusLabel.setText(reply["status"] as? String)
}, errorHandler: { error in
print("error: \(error)")
})

}

}

}

最佳答案


你应该使用这个:

   func startPlay() {
if timerRunning == false {
//self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("counting:"), userInfo: nil, repeats: true)
self.timer = NSTimer(timeInterval: 1, target: self, selector: "counting:", userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(self.timer, forMode: NSRunLoopCommonModes)
self.timerRunning = true
self.watchLabel.text = "Start"
}
}


我无法向你解释为什么我们需要显式使用 NSRunLoop。在开发具有数据传输功能的应用程序时,我遇到了同样的计时器问题。您可以通过查询“nstimer runloop”或here在谷歌中找到一些答案。
我更喜欢用它来重新启动:

    func restartPlay() {
self.timerCount = 0
self.timerLabel.text = "0";
stopPlay()
startPlay()
self.watchLabel.text = "Restarted"
}


干杯。

关于ios - WatchConnectivity 通过在 iPhone 上启动计时器来发送 watch 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33278536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com