2016-03-15 83 views
0

我正在製作一個旨在接收一些通知的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() 



} 



} 

回答

0

沒有辦法以編程方式打開Apple關注的應用程序,也不是 '自動' 打開一個門。 Apple Watch應用程序啓動的唯一方法是通過直接用戶操作(可能包括點擊通知或瀏覽)。

聽起來好像您可能正在觀察模擬器中發生的情況,當您構建應用程序的WatchKit擴展時。這類似於用戶啓動應用程序。如果您希望能夠測試WatchKit應用的常用用途,請讓WatchOS模擬器運行並構建iPhone目標。然後,您可以與iPhone應用程序進行交互,並且可以在iPhone應用程序生命週期期間在您選擇的任何位置手動啓動WatchKit應用程序。