Apple Watch Countアプリ Watch -> 親アプリ 情報更新

Apple Watch -> iPhoneとのデータ情報更新の情報を入手しました。

ソースを眺めているだけでは習得出来ないので「WatchKitをさわってみた」このページに記載されている通りに制作して見ました。

写生ですが、動作確認でき勉強になりました。

 

自分のメモとして公開しておきます。

詳細はリンク先を参照願います。

 

AppDelegate.swift

import UIKit


@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {


    var window: UIWindow?

 

    //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

    // Watch Kit App からiPhone側の親アプリが起動された時に呼ばれます

    func application(

                                    application: UIApplication!,

        handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!,

                                          reply: (([NSObject : AnyObject]!) -> Void)!) {

                                            

        var count: String = userInfo["content"] as String!

        

        var appDelegate = UIApplication.sharedApplication().delegate as AppDelegate

        var vc:ViewController? = appDelegate.window?.rootViewController as? ViewController

        vc?.countLabel.text = count

        

        reply(["from":"watchkitsample"])

    }

 

    //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

}

ViewController.swift

import UIKit


class ViewController: UIViewController {


    @IBOutlet weak var countLabel: UILabel!

    

    override func viewDidLoad() {

        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.

    }

    

}

InterfaceController.swift

import WatchKit

import Foundation



class InterfaceController: WKInterfaceController {

    

    @IBOutlet weak var countLabel: WKInterfaceLabel!

    var count = 0

    

    // 最初に呼び出されるメソッド

    override func awakeWithContext(context: AnyObject?) {

        super.awakeWithContext(context)

        

        // Configure interface objects here.

    }


    // ユーザーにUIが表示されたタイミングで呼び出されるメソッド

    override func willActivate() {

    // This method is called when watch view controller is about to be visible to user

        super.willActivate()

    }


    // UIが非表示になったタイミングで呼び出されるメソッド

    override func didDeactivate() {

        // This method is called when watch view controller is no longer visible

        super.didDeactivate()

    }

    

    // カウントラベル

    func updateCountLabel() {

        countLabel.setText(String(count))

        

        // iPhone側の親アプリを起動することができます。

        //iPhone側の親アプリに値を連携

              // userinfo key:content 値:count

        WKInterfaceController.openParentApplication(["content": String(count)],

            reply: {obj, error in

                NSLog("return from %@", obj["from"] as String!)

        })

        

    }


    // カウントダウンアクション

    @IBAction func countDown() {

        

        if(count > 0) {

            count--

            updateCountLabel()

        }

        

    }

    

    // カウントダウンアップ

    @IBAction func countUp() {

        count++

        updateCountLabel()

    }

    

 

}


これでApple Watch側でそれぞれのカウントボタンをタップするとiPhone(親アプリ)が起動し、
Apple Watch側のカウントと親アプリ側のカウントの表示が同期します。

 

GitHub CountWatchKit

  

目 次