我有一个用于在 iPhone 和 Watch 之间共享数据(共享文本)的功能应用程序,我希望即使手表设置在后台(当 Watch 在后台时将数据从 iPhone 发送到 Watch)也能正常工作。我读了很多关于如何做到这一点的内容,但似乎没有什么适合我的应用程序。请添加代码以使应用程序正常工作,就像我之前所说的那样。或者给我一些适合这个应用程序的来源。谢谢你!
iPhone 代码:
import UIKit
import WatchConnectivity
class ViewController: UIViewController, WCSessionDelegate {
@IBOutlet weak var iPhoneLabel: UILabel!
var session : WCSession!;
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
}
func sessionDidBecomeInactive(_ session: WCSession) {
}
func sessionDidDeactivate(_ session: WCSession) {
}
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
let msg = message["b"] as? String;
self.iPhoneLabel.text = msg;
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if(WCSession.isSupported()){
self.session = WCSession.default;
self.session.delegate = self;
self.session.activate();
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func sendMessage(_ sender: Any) {
session.sendMessage(["a" : "Hello"], replyHandler: nil, errorHandler: nil);
}
}
手表代码:
import WatchKit
import Foundation
import WatchConnectivity
import UIKit
class InterfaceController: WKInterfaceController, WCSessionDelegate {
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
}
@IBOutlet var WatchLabel: WKInterfaceLabel!
var session: WCSession!;
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
//self.label.setText(message["a"]! as? String)
let msg = message["a"] as? String;
WatchLabel.setText(msg);
sendMessage();
}
func sendMessage(){
session.sendMessage(["b":"goodbye"], replyHandler: nil, errorHandler: nil);
}
override func awake(withContext context: Any?) {
super.awake(withContext: 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()){
self.session = WCSession.default;
self.session.delegate = self;
self.session.activate();
}
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
}
在我用 session.updateApplicationContext() 更改方法 session.sendMessage() 后,它只能工作一次。有什么建议吗?
iPhone 代码:
import UIKit
import WatchConnectivity
class ViewController: UIViewController, WCSessionDelegate {
@IBOutlet weak var iPhoneLabel: UILabel!
var session : WCSession!;
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
}
func sessionDidBecomeInactive(_ session: WCSession) {
}
func sessionDidDeactivate(_ session: WCSession) {
}
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
let msg = applicationContext["b"] as? String
//Use this to update the UI instantaneously (otherwise, takes a little while)
DispatchQueue.main.async() {
self.iPhoneLabel.text = msg;
}
}
override func viewDidLoad() {
super.viewDidLoad()
if(WCSession.isSupported()){
self.session = WCSession.default;
self.session.delegate = self;
self.session.activate();
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func sendMessage(_ sender: Any) {
let applicationDict = ["a":"Hello"];
do {
try session.updateApplicationContext(applicationDict)
} catch {
print("error")
}
}
}
手表代码:
import WatchKit
import Foundation
import WatchConnectivity
import UIKit
class InterfaceController: WKInterfaceController, WCSessionDelegate {
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
}
@IBOutlet var WatchLabel: WKInterfaceLabel!
var session: WCSession!;
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
print("Watch message received")
let msg = applicationContext["a"] as? String
DispatchQueue.main.async() {
self.WatchLabel.setText(msg);
}
sendMessage();
}
func sendMessage(){
print("Watch send message");
let applicationDict = ["b":"goodbye"];
do {
try session.updateApplicationContext(applicationDict)
} catch {
print("error")
}
}
override func awake(withContext context: Any?) {
super.awake(withContext: 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()){
self.session = WCSession.default;
self.session.delegate = self;
self.session.activate();
}
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
}