如何将应用程序放在后台并返回前台(watchOS和iOS)?

时间:2016-03-15 12:13:03

标签: ios notifications watchkit apple-watch watchconnectivity

我正在为watchOS制作一个旨在接收一些通知的应用程序,发送这些通知的设备是iPhone。

我已经设法在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()



}



}

1 个答案:

答案 0 :(得分:0)

无法以编程方式打开Apple Watch应用程序,也无法自动打开Apple Watch应用程序。打开一个。 Apple Watch应用程序启动的唯一方法是通过直接用户操作(可能包括点击通知或浏览)。

当您构建应用程序的WatchKit扩展时,听起来您可能正在观察模拟器中发生的情况。这类似于用户启动应用程序。如果您希望能够测试WatchKit App的常用用法,请让WatchOS模拟器继续运行,并构建iPhone目标。然后,您可以与iPhone应用程序进行交互,并在iPhone应用程序生命周期内随时手动启动WatchKit应用程序。