iOS App與WatchKit Extension的數(shù)據(jù)通信
NSUserDefaults
NSUserDefaults是快速共享信息的途徑。它適合存儲(chǔ)各種快速訪問(wèn)和計(jì)算的小型數(shù)據(jù),比如用戶(hù)名與檔案信息。如果希望使用UserDefaults,請(qǐng)用于靜態(tài)數(shù)據(jù)這樣用戶(hù)不必考慮數(shù)值的變化。
你需要設(shè)定App Group來(lái)讓設(shè)備通過(guò)共享容器來(lái)實(shí)現(xiàn)數(shù)據(jù)共享,確保手表擴(kuò)展和ios target都已如此設(shè)置?;旧暇褪轻槍?duì)兩個(gè)設(shè)備創(chuàng)建一個(gè)統(tǒng)一的App Group標(biāo)識(shí)符。如果需要?jiǎng)h除它,可以以類(lèi)似的方法進(jìn)行。
你可以通過(guò)之前創(chuàng)建的App Group名來(lái)使用defaults,基本上就是為特定的key鍵值設(shè)置對(duì)象。在iPhone上,用戶(hù)輸入了文本,保存,文本就存到了應(yīng)用共享的UserDefaults里。在Watch上,你可以從AppGroup得到defaults然后進(jìn)行手表顯示內(nèi)容的更新。
- // on the iPhone app
- letdefaults=NSUserDefaults(suiteName:"group.com.natashatherobot.userdefaults")
- letkey="userInput"
- overridefuncviewDidLoad(){
- super.viewDidLoad()
- textLabel.text=defaults?.stringForKey(key)??"Type Something..."
- }
- @IBActionfunconSaveTap(sender:AnyObject){
- letsharedText=textField.text
- textLabel.text=sharedText
- defaults?.setObject(sharedText,forKey:key)
- defaults?.synchronize()
- }
- // WatchKit
- classInterfaceController:WKInterfaceController{
- @IBOutletweakvartextLabel:WKInterfaceLabel!
- letdefaults=NSUserDefaults(suiteName:
- "group.com.natashatherobot.userdefaults")
- varuserInput:String?{
- defaults?.synchronize()
- returndefaults?.stringForKey("userInput")
- }
NSFileCoordinator
對(duì)更大型的數(shù)據(jù)來(lái)說(shuō),NSFileCoordinator是管理應(yīng)用和watch擴(kuò)展的共享空間里文件的方式之一。對(duì)于有限列表的內(nèi)容它很合適,同時(shí)也適用于圖像文件。
下面的例子是個(gè)簡(jiǎn)單的代辦事項(xiàng)列表app,在手機(jī)上增加任務(wù)然后暑假傳輸?shù)絎atchKit擴(kuò)展并在手表上顯示。你的視圖控制器需要遵循NSFilePresenter協(xié)議,除了實(shí)現(xiàn)兩個(gè)必需方法,其它不是很關(guān)鍵。FilePresenter協(xié)議有一個(gè)item URL,就是填你的AppGroup標(biāo)識(shí)符的地方。通過(guò)URL,你在對(duì)應(yīng)目錄建立一個(gè)文件。有必要的話你也可以通過(guò)操作隊(duì)列來(lái)控制多線程訪問(wèn)。
另外,presentedItemDidChange這個(gè)代理方法,在FilePresenter里通知你是否一個(gè)對(duì)象發(fā)生了改變,來(lái)讓你更新app數(shù)據(jù)而無(wú)需用戶(hù)手動(dòng)刷新。
然而這里還是有個(gè)關(guān)于NSFileCoordinator與NSFilePresenter 的bug而不方便在擴(kuò)展里使用。具體可參見(jiàn)Natasha的網(wǎng)站。
在代辦事項(xiàng)數(shù)組里利用FileCoordinator寫(xiě)入一個(gè)文件,可以通過(guò)讀寫(xiě)文件以實(shí)現(xiàn)打包和解包事項(xiàng)的數(shù)據(jù)到事項(xiàng)數(shù)組,接下來(lái)可以依據(jù)文件里的事項(xiàng)數(shù)據(jù)計(jì)算生成表格。需要注意的是如果你設(shè)計(jì)了刪除功能,而watch擴(kuò)展和iPhone應(yīng)用都能修改文件,會(huì)遇到線程同步的麻煩。
- // iPhone app
- privatefuncsaveTodoItem(todoItem:String){
- // write item into the todo items array
- ifletpresentedItemURL=presentedItemURL{
- fileCoordinator.coordinateWritingItemAtURL(presentedItemURL,options:nil,error:nil)
- {[unownedself](newURL)->Voidin
- self.todoItems.insert(todoItem,atIndex:0)
- letdataToSave=NSKeyedArchiver.archivedDataWithRootObject(self.todoItems)
- letsuccess=dataToSave.writeToURL(newURL,atomically:true)
- }
- }
- }
- // in the Watch
- // MARK: Populate Table From File Coordinator
- privatefuncfetchTodoItems(){
- letfileCoordinator=NSFileCoordinator()
- ifletpresentedItemURL=presentedItemURL{
- fileCoordinator.coordinateReadingItemAtURL(presentedItemURL,options:nil,error:nil)
- {[unownedself](newURL)->Voidin
- ifletsavedData=NSData(contentsOfURL:newURL){
- self.todoItems=NSKeyedUnarchiver.unarchiveObjectWithData(savedData)as[String]
- self.populateTableWithTodoItems(self.todoItems)
- }
- }
- }
- }
Frameworks
“If the code appears more than once, it probably belongs in a framework.(如果代碼出現(xiàn)超過(guò)一次,應(yīng)該考慮能否放到框架里)”
-WWDC 2014, Building Modern Frameworks
框架對(duì)于業(yè)務(wù)邏輯、CoreData、可重用UI組件來(lái)說(shuō)很棒。就像WWDC里說(shuō)的那樣,你可以將重復(fù)代碼放到框架里。在FileCoordinator的例子里,我們獲取和讀寫(xiě)文件的代碼出現(xiàn)了兩次,可以把它們提取到一個(gè)framework框架里。建立框架很簡(jiǎn)單:建立新target,選擇Cocoa Touch framework,然后命名。它會(huì)在你的iOS應(yīng)用里自動(dòng)鏈接,因此也不要忘了在WatchKit擴(kuò)展里進(jìn)行鏈接。
關(guān)鍵的一點(diǎn),特別是對(duì)于Swift語(yǔ)言來(lái)說(shuō),應(yīng)該把框架認(rèn)作一個(gè)API。它需要聲明為公共的(public),因?yàn)檫@是iOS應(yīng)用和watchkit擴(kuò)展共用的接口。因此如果你在建立對(duì)象類(lèi),確保public關(guān)鍵字也加上了。這樣在手機(jī)和手表應(yīng)用里你導(dǎo)入了框架就可以訪問(wèn)任何公共內(nèi)容。
- importWatchKit
- importMySharedDataLayer
- classInterfaceController:WKInterfaceController{
- @IBOutletweakvarfavoriteThingsLabel:WKInterfaceLabel!
- overridefuncawakeWithContext(context:AnyObject?){
- super.awakeWithContext(context)
- letmyFavoriteThings=MySharedData().myFavoriteThings
- letfavoriteThingsString=", ".join(myFavoriteThings)
- favoriteThingsLabel.setText("My favorite things are \(favoriteThingsString)")
- }
- }
Keychain Sharing
鑰匙鏈共享是針對(duì)更高安全性要求的數(shù)據(jù)的。UserDefaults提供的安全性不滿(mǎn)足時(shí),你可以用鑰匙鏈共享來(lái)保障信息安全及跨擴(kuò)展的共享能力。
WatchKit目前的一個(gè)大問(wèn)題是沒(méi)有認(rèn)證機(jī)制。蘋(píng)果提供了KeychainIteamWrapper的示例,但API太老不支持ARC。我推薦使用這個(gè)版本https://gist.github.com/dhoerl/1170641,它基于ARC并有清晰的接口。
根據(jù)問(wèn)題是如何通過(guò)access group初始化KeychainItemWrapper。與AppGroup的概念類(lèi)似,設(shè)備之間有共享空間。你在iOS和WatchKit擴(kuò)展中都需要鑰匙鏈來(lái)訪問(wèn)用戶(hù)數(shù)據(jù)。通過(guò)鍵值存儲(chǔ)體系,你設(shè)定用戶(hù)名和密碼并用同一個(gè)標(biāo)識(shí)符建立同一類(lèi)型的keychain項(xiàng)。這個(gè)例子里僅展示了當(dāng)用戶(hù)填好用戶(hù)名密碼時(shí)WatchKit擴(kuò)展展示數(shù)據(jù)的這一工作過(guò)程。
- // iPhone app
- @IBActionfunconSaveTap(sender:AnyObject){
- letusername=usernameTextField.text
- letpassword=passwordTextField.text
- letkeychainItem=KeychainItemWrapper(identifier:"SharingViaKeychain",accessGroup:"W6GNU64U6Q.com.natashatherobot.SharingViaKeychain")
- // WatchKit extension
- letkeychainItem=KeychainItemWrapper(identifier:"SharingViaKeychain",accessGroup:"W6GNU64U6Q.com.natashatherobot.SharingViaKeychain")
- letpasswordData=keychainItem.objectForKey(kSecValueData)asNSData
- letpassword=NSString(data:passwordData,encoding:NSUTF8StringEncoding)
- letusername=keychainItem.objectForKey(kSecAttrAccount)as?String