我使用手表连接框架将数据从iphone发送到Apple watch,反之亦然。有没有办法计算数据传输时间?
代码: // // ViewController.swift // WatchCommunication_counter // //由Kaushik Shanmugam于03/06/16创建。 //版权所有©2016 Kaushik Shanmugam。版权所有。 //
import UIKit
import WatchConnectivity
class ViewController: UIViewController, WCSessionDelegate{
//Communication session
let session = WCSession.defaultSession()
var phoneCounter = 0
@IBOutlet weak var labelcounter: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
initWCSession()
// Do any additional setup after loading the view, typically from a nib.
}
func initWCSession(){
session.delegate = self
session.activateSession()
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
let msg = message["CounterValueFromWatch"]as! Int
labelcounter.text = "Counter: \(msg)"
}
@IBAction func sendData(sender: AnyObject) {
//Send data to apple watch
let msg = ["CounterValueFromiphone" : phoneCounter++]
session.sendMessage(msg, replyHandler: {(replay) -> Void in
}) {(error) -> Void in
}
}
}
答案 0 :(得分:0)
您可以测量往返次数:
let msg = ["CounterValueFromiphone" : phoneCounter++]
let start = NSDate()
session.sendMessage(msg, replyHandler: {(replay) -> Void in
let end = NSDate()
let timeInterval: Double = end.timeIntervalSinceDate(start)
// display timeInterval in main queue
}) {(error) -> Void in
let end = NSDate()
let timeInterval: Double = end.timeIntervalSinceDate(start)
// display timeInterval in main queue
}
我不相信你可以比较在iPhone上测量的NSDate()和手表上的NSDate(),并依靠差异。