Apple watch 开发入门
这篇文章其实早在8月份就写出来了,一直没发表出来,这期间也有好几位朋友来问我关于 Apple watch 相关的问题。我也将这篇文章的 md 发给他们了,现在将这篇文章发表出来,让更多想了解 Apple watch 开发的小伙伴能有一个简单的入门吧。
相关文章文章推荐
喵神 WWDC15 Session笔记 - 30 分钟开发一个简单的 watchOS 2 app
Apple Watch和iOS App之间的通信&Apple Watch自定义Cell
Apple Watch音频录制,.wav转换.mp3,获取音频文件时长
Apple Watch开发中遇到的那些问题(WatchOS 2)
配置
Glance以及Notification需要自己手动的配置,编辑它们的Scheme就可以了。
生命周期
presentControllerWithNames(忘了这图的原出处了,要是原作者看见请告知,侵删)控制器之间传值
正向传值
- (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端方便多了。
Segue传值
//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/MP4播放
//.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);
}];
mp3播放.png
mp4播放.png
音频录制
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界面(没有交互响应,点击任何位置都会跳转到应用内部)
这是一个类似简介的东西
开始没有选择,如何新增一个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
Notifications
Apple watch的通知有两种,分别是short looks
和long looks
short looks:短版本
从上图看,内容很简单,一个App的icon,应用的名字,一条消息。 和
Glance
的UI界面一样不能添加带有操作性的控件。至于哪些能添加哪些不能添加,最直接的办法就是拖一个控件到Static Notification interface controller
或者 Dynamic Notification interface controller
不报错就说明这个控件是可以添加的。</br>
long looks
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审核,如果你选择了某个功能,但没有实现,那么一定会被拒绝的,大家注意一下这点,坑就来那里~
坑:
- 使用代码设置图片</br>
Apple watch和iPhone开发不一样,所以图片尺寸要求也不一样
找到Apple watch的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自定义Cell
Error
Apple watch的Bundle versions string,short
和Bundle version
都要和iOS 里面的一致,不然会build fail
小技巧
- 修改controller title的颜色</br>
选择要修改的控制器,右侧的选择器,选择第一个选择器下面的Global Tint
网络请求
不要使用NSURLConnection send...
方法,应该使用NSURLSession
方法