我正在製作一個旨在接收一些通知的watchOS應用程序,並且發送這些通知的設備是iPhone。如何把應用程序放在後臺並返回放在前臺(watchOS和iOS)?
我已經設法讓Apple Watch和iPhone之間進行通信,iPhone已經向Apple Watch發送消息,Apple Watch也已經向iPhone發送消息。我沒有使用WatchNotification
工具。
發生了以下情況,當我運行該應用程序時,它會自動在Apple Watch中打開,而我想要的是該應用程序仍在後臺運行。當我按下iPhone中的一個按鈕時,文本會發送到Apple Watch,然後(Apple Watch收到文本時)應用程序正常打開。
我會很感激任何幫助。
iPhone代碼
import UIKit
import WatchConnectivity
class ViewController: UIViewController,WCSessionDelegate {
var iphoneNotification = "Deseja renovar o seu seguro automovel?"
let session = WCSession.defaultSession()
@IBOutlet weak var lblNotification: UILabel!
override func viewDidLoad() {
initSession()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func ActionSentNotification(sender: AnyObject) {
let msg = ["NotificationSentforIphone" : iphoneNotification]
session.sendMessage(msg, replyHandler: {(replay) -> Void in }) { (error) -> Void in
}
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject])
{
let msg = message["NotificationSentforWatch"] as! String
lblNotification.text = "\(msg)"
}
func initSession()
{
session.delegate = self
session.activateSession()
}
}
觀看代碼
import WatchKit
import Foundation
import WatchConnectivity
class InterfaceController: WKInterfaceController,WCSessionDelegate {
@IBOutlet var Notification: WKInterfaceLabel!
var watchNotification = "ok"
let session = WCSession.defaultSession()
//@IBOutlet var lblNotification: WKInterfaceLabel!
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
initSession()
super.willActivate()
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject])
{
let msg = message["NotificationSentforIphone"] as! String
Notification.setText("\(msg)")
//lblNotification.setText("Notification:\(msg)")
}
@IBAction func ActionSentNotificationforIphone()
{
let msg = ["NotificationSentforWatch" : watchNotification]
session.sendMessage(msg, replyHandler: {(replay) -> Void in }) { (error) -> Void in
}
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
func initSession()
{
session.delegate=self
session.activateSession()
}
}