咱们能够在iPhone和Apple Watch间经过app groups来共享数据。方法以下:app
首先要在dev center添加一个新的 app group:ide
接下来建立一个新的single view application,名字就叫SharingDataDemo:ui
添加三个控件到主界面:TextField、Button和Label。spa
在Button的点击事件中使用NSUserDefaults将用户输入的内容进行保存:orm
注意suiteName必须和dev center中定义的app group的identifier ID一致。事件
接下来添加Watch:选择File - New - Target - WatchKit App:get
添加两个控件到watch的main界面:Label和Button。it
在Button的点击事件中使用NSUserDefaults将上一步保存的数据读取出来,显示到Label上:io
最后一步,也是关键的一步,设置iPhone App和Watch App到同一个group中,缺失这一步,数据是没法共享的。
注意iPhone App和Watch App分别要如此设置一次,不要漏了Watch App的设置。方法
效果: