5

ウォッチ コネクティビティについて詳しく説明したいくつかの優れたブログ投稿 ( http://www.kristinathai.com/watchos-2-tutorial-using-application-context-to-transfer-data-watch-connectivity-2/およびhttp://natashatherobot .com/watchconnectivity-application-context/ ) では、iPhone の UI をタップしたときにウォッチにデータを送信する簡単なアプリの例を使用しています。

私のアプリは単に iPhone アプリからのデータを一覧表示するだけなので、すぐにデータを送信する必要はありません。アプリが読み込まれたり、バックグラウンドになったときにデータを送信したかっただけです...この目的のために、updateApplicationContextindidFinishLaunchingdidEnterBackground.. .しかし、私のウォッチインターフェイスコントローラーの私のdataSourceデリゲートは、トリガーされることに非常に気づいています...特に、一目はシミュレーターにのみロードされ、デバイスにはロードされません。情報をプッシュするのに適した時間と場所はありますか?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    WatchSessionManager.sharedManager.startSession()      
    do {
         try WatchSessionManager.sharedManager.updateApplicationContext(["peopleDict" : peopleDict])                                        
    } catch {
        print(error)
    }
     return true
}

func applicationDidEnterBackground(application: UIApplication) {     
     do {
         try WatchSessionManager.sharedManager.updateApplicationContext(["peopleDict" : peopleDict])                                      
     } catch {
         print(error)
     }
}

以下はWatchSessionManager私のactiviateSessionextensionDelegateappliciationDidFinishLaunching

import WatchConnectivity

protocol DataSourceChangedDelegate {
    func dataSourceDidUpdate(dataSource: DataSource)
}


class WatchSessionManager: NSObject, WCSessionDelegate {

    static let sharedManager = WatchSessionManager()
    private override init() {
        super.init()
    }

    private var dataSourceChangedDelegates = [DataSourceChangedDelegate]()

    private let session: WCSession = WCSession.defaultSession()

    func startSession() {
        session.delegate = self
        session.activateSession()
    }

    func addDataSourceChangedDelegate<T where T: DataSourceChangedDelegate, T: Equatable>(delegate: T) {
        dataSourceChangedDelegates.append(delegate)
    }

    func removeDataSourceChangedDelegate<T where T: DataSourceChangedDelegate, T: Equatable>(delegate: T) {
        for (index, indexDelegate) in dataSourceChangedDelegates.enumerate() {
            if let indexDelegate = indexDelegate as? T where indexDelegate == delegate {
                dataSourceChangedDelegates.removeAtIndex(index)
                break
            }
        }
    }
}

// MARK: Application Context
// use when your app needs only the latest information
// if the data was not sent, it will be replaced
extension WatchSessionManager {

    // Receiver
    func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {

        dispatch_async(dispatch_get_main_queue()) { [weak self] in
            self?.dataSourceChangedDelegates.forEach { $0.dataSourceDidUpdate(DataSource(data: applicationContext))}
        }

    }
}
4

1 に答える 1