這篇文章其實早在8月份就寫出來了,一直沒發表出來,這期間也有好幾位朋友來問我關於 Apple watch 相關的問題。我也將這篇文章的 md 發給他們了,如今將這篇文章發表出來,讓更多想了解 Apple watch 開發的小夥伴能有一個簡單的入門吧。html
喵神 WWDC15 Session筆記 - 30 分鐘開發一個簡單的 watchOS 2 appgit
Apple Watch和iOS App之間的通訊&Apple Watch自定義Cellgithub
Apple Watch音頻錄製,.wav轉換.mp3,獲取音頻文件時長app
Apple Watch開發中遇到的那些問題(WatchOS 2)ide
Glance以及Notification須要本身手動的配置,編輯它們的Scheme就能夠了。
push | modal |
---|---|
OneController init | OneController init |
OneController awakeWithContext: | OneController awakeWithContext: |
OneController willActivate | OneController willActivate |
OneController didAppear | OneController didAppear |
push | modal |
---|---|
OneController willDisappear | TwoController init |
TwoController init | TwoController awakeWithContext: |
TwoController awakeWithContext: | OneController willDisappear |
TwoController willActivate | TwoController willActivate |
OneController didDeactivate | TwoController didAppear |
TwoController didAppear | OneController didDeactivate |
pop | dismiss |
---|---|
TwoController willDisappear | TwoController willDisappear |
OneController willActivate | OneController willActivate: |
TwoController didDeactivate | OneController didAppear |
OneController didAppear | TwoController didDeactivate |
每一個 WKInterfaceController 對象必然會被調用的生命週期方法有三個,分別是該對象被初始化時的-initWithContext:
,將要呈現時的 -willActivate
以及呈現結束後的 -didDeactivate
,一樣類比 UIViewController 的話,能夠將它們理解爲分別對應-viewDidLoad
,viewWillAppear:
以及 -viewDidDisappear:
咱們通常在 -initWithContext: 和 -willActivate 中配置「視圖元素」的屬性,在 -didDeactivate 中停用像是 NSTimer 之類的會 hold 住 self 的對象。須要特別注意的是,在 -didDeactivate 中對「視圖元素」屬性進行設置是無效的,由於當前的 WKInterfaceController 已經非活躍。
帶你玩- AppleWatch開發二:界面佈局 帶你玩-AppleWatch開發四:Table視圖
一個控件只能對應一個action tableView的點擊事件,注意不要去給cell拉線
#pragma mark - Table Row Select
-(void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex
{
NSLog(@"did select rowIndex = %i",rowIndex);
/** 設置傳指數據(正向傳指) */
NSDictionary *contextDic = @{@"PicName":@"picture",@"index":[NSNumber numberWithInteger:rowIndex]};
// [self presentControllerWithName:@"detail" context:contextDic];
[self pushControllerWithName:@"detail" context:contextDic];
}
複製代碼
Apple watch獨有的,相似iPhone的3DTouch,也是根據重力感應來彈出菜單
通過測試,最多隻能添加四個,無論添加多少個,只會顯示前4個。
push或者Modal以後返回只有一個返回箭頭
針對 Watch 的開發不能使用代碼的方式。首先,全部的 WKInterfaceObject 對象都必需要設計的時候經由 StoryBoard 進行添加,運行時咱們沒法再向界面上添加或者移除元素 (若是有移除須要的,可使用隱藏);其次 WKInterfaceObject 與佈局相關的某些屬性,好比行高行數等,不可以在運行時進行變動和設定。基原本說在運行時咱們只可以改變視圖的內容,以及經過隱藏某些視圖元素來達到有限地改變佈局 (其餘視圖元素會試圖填充被隱藏的元素)。
- (void)pushControllerWithName:(NSString *)name context:(nullable id)context;
- (void)presentControllerWithName:(NSString *)name context:(nullable id)context;
/** present多個控制器,相似next Page */
- (void)presentControllerWithNames:(NSArray<NSString*> *)names contexts:(nullableNSArray*)contexts;
複製代碼
- (void)pushControllerWithName:(NSString *)name context:(nullable id)context;
- (void)presentControllerWithName:(NSString *)name context:(nullable id)context;
- (void)presentControllerWithNames:(NSArray<NSString*> *)names contexts:(nullableNSArray*)contexts;
複製代碼
在上面的三個方法中的末尾都有一個context:
參數,這個參數就是用於在咱們跳轉控制器的時候傳值,這點比iOS端方便多了。
//tableView
- (NSArray *)contextsForSegueWithIdentifier:(NSString *)segueIdentifier inTable:(WKInterfaceTable *)table rowIndex:(NSInteger)rowIndex {
return nil;//傳值內容
}
- (id)contextForSegueWithIdentifier:(NSString *)segueIdentifier {
return nil;//傳值內容
}
- (NSArray *)contextsForSegueWithIdentifier:(NSString *)segueIdentifier {
return nil;//傳值內容
}
//tableView
- (id)contextForSegueWithIdentifier:(NSString *)segueIdentifier inTable:(WKInterfaceTable *)table rowIndex:(NSInteger)rowIndex {
return nil;//傳值內容
}
複製代碼
在控制器的awakeWithContext:
方法接收數據
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
NSLog(@"receive = %@",context);
// Configure interface objects here.
}
複製代碼
//.mp3 or .mp4
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Jennifer Lopez - Feel the Light" withExtension:@".mp3"];
NSDictionary *options = @{WKMediaPlayerControllerOptionsAutoplayKey:@YES};
[self presentMediaPlayerControllerWithURL:url options:options completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * _Nullable error) {
if (error) {
NSLog(@"error = %@",error);
return ;
}
NSLog(@"endTime = %f",endTime);
}];
複製代碼
Apple watch有自帶的錄音控制器,咱們只須要配置好就能夠。支持.wav, .mp4, and .m4a
格式
NSDictionary *recorderOptions = @{
/** 錄製好以後的標題 */
WKAudioRecorderControllerOptionsActionTitleKey:@"發送",
/** 是否自動錄製 */
WKAudioRecorderControllerOptionsAutorecordKey:@YES,
/** 時間 NSTimeInterval */
WKAudioRecorderControllerOptionsMaximumDurationKey:@30
};
[self presentAudioRecorderControllerWithOutputURL:_recorderUrl preset:WKAudioRecorderPresetHighQualityAudio options:recorderOptions completion:^(BOOL didSave, NSError * _Nullable error) {
NSLog(@"didSave = %@",didSave?@"YES":@"NO");
if (error) {
NSLog(@"error = %@",error);
}
}];
複製代碼
OutputURL 這個URL不是沙盒URL,而是App Groups
的URL。 在模擬器上使用沙盒路徑,錄製播放都沒有問題。 可是,使用手錶的話就會出現一錄音這個控制器就直接dismiss掉了。
App Groups 的路徑
/** Identifier 要跟App Groups 一致 */
NSURL *url = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.LaiYoung.NextPage1111"];
NSURL *fileUrl = [url URLByAppendingPathComponent:@"record.wav"];
複製代碼
相關文章 Apple Watch音頻錄製,.wav轉換.mp3,獲取音頻文件時長
這是一個相似簡介的東西 開始沒有選擇,如何新增一個Glance
,在storyboard拖一個Glance Interface Controller
,而後新增一個Scheme
,命名爲ClanceXXX
,選擇Edit Scheme...
,Executable
選擇XXX Watch App.app
,Watch Interface則選擇對應的Glance,close便可
UI:Glance都是基於特定模版的,蘋果提供了一系列的模版,包括了屏幕頂部和底部的設計。在Glance Interface Controller Scene
的第四個選擇器選擇。 UI界面不能添加帶有事件操做性的控件,例如Button,Switch
等等,可是能夠添加Label,Image
這樣的控件。避免使用table
和map
Apple watch的通知有兩種,分別是short looks
和long looks
Glance
的UI界面同樣不能添加帶有操做性的控件。至於哪些能添加哪些不能添加,最直接的辦法就是拖一個控件到
Static Notification interface controller
或者
Dynamic Notification interface controller
不報錯就說明這個控件是能夠添加的。
long looks:長版本,長版本相對於短版原本說多了很多東西 從上圖看首先它的UI是能夠滾動的 將它分爲3個部分,分別是sash
,content
,actions
sash:包括了應用的名稱和icon,這部分默認的顏色是透明的,能夠本身自定義顏色(修改顏色,選擇Static Notification interface controller
的入口,第四個選擇器) content:這部分就是推送的詳細內容 action:操做按鈕(最多能夠添加4個),而後Dismiss按鈕是系統一直會有的,不須要咱們添加
模擬long looks
咱們建立好一個Apple watch應用或者爲已有項目添加一個target的時候默認會選擇Notification Scene
,而後咱們的Notification InterfaceController
就會是上面的樣子,可能和storyboard中的有點不同,上面的圖我是在官方文檔中找到的,可能官方還沒來的及更改吧。只要明白意思就好... Static interface
是必須的,而dynamic interface
是可選的。 在推送到消息的時候通常默認都是選擇的dynamic interface,只有當dynamic interface
不可用、沒有足夠的電力保證顯示動態界面、明確指出不能用動態界面的時候纔會顯示Static interface
配置自定義界面的類目(Category)
官方文檔 Notifications
相關文章 Notifications概述
關於Watch App審覈,若是你選擇了某個功能,但沒有實現,那麼必定會被拒絕的,你們注意一下這點,坑就來那裏~
Assets.xcassets
選擇圖片以後點擊第三個選擇器 Devices
勾選watchOS,會發現對於的2x/38 mm 2x/42 mm 2x
都 沒有圖片,因此致使代碼設置不會出來。watchOS 2.0
再也不支持App Groups
,watchOS 2.0可使用WCSession
進行通信,WCSession
具體怎麼使用能夠看我以前的這片文章 Apple Watch和iOS App之間的通訊&Apple Watch自定義CellApple watch的Bundle versions string,short
和Bundle version
都要和iOS 裏面的一致,否則會build fail
Global Tint
不要使用NSURLConnection send...
方法,應該使用NSURLSession
方法