Apple Watch Kit(1)- 开发一瞥

来源:互联网 发布:人工智能技术路线图 编辑:程序博客网 时间:2024/05/18 04:25

Watch Kit 开发一瞥

创建项目

  • 一下是在已经创建的iphone app的基础上进行的操作:

    • 1、添加target

    • 2、选择WatchKit App

    • 3、填写Target信息,此处可以选择是否支持 Notification 和 Glance。

Watch App 的运行机制

创建完项目后我们可以看到多出了来两个group 这个两个group 分别是WatchKit Extension和WatchKit App.   WatchKit Extension 即 运行在iphone上的WatchKit App的辅助程序,主要用来处理事件数据等,  WatchKit App     运行在apple watch 上程序,用于展示。
  • watch app 的加载机制,如下图:

  • watchkit 的 interface controller 的生命周期: 如下图:

  • 三种接口类型的实现

    • The WatchKit App

            ///数据初始化      - (instancetype)init      {          self = [super init];          if (self) {              //Fetch the data you want to display.          }          return self;      }      ///页面布局参数设置      - (void)awakeWithContext:(id)context {          [super awakeWithContext:context];          //Set the initial values and configuration of labels, images, and other controls. Configure interface objects here.          NSLog(@"%@ awakeWithContext", self);      }      ///当controller将显示在屏幕上的时候调用      - (void)willActivate {          // This method is called when watch view controller is about to be visible to user          NSLog(@"%@ will activate", self);      }      ///当controller即将不显示在屏幕上的时候调用      - (void)didDeactivate {          // This method is called when watch view controller is no longer visible          NSLog(@"%@ did deactivate", self);      }

  • Glances

    Glances页面不可以滑动,只能提供一个屏幕;只能用于读取数据,不能用于编辑修改数据;不能包含按钮、开关和其他交互的control;点击进入对应的watch app。 一个应用只允许有一个glance接口控制器,因此在使用glance显示你的数据的时候必须考虑这个问题。(只允许显示一个手表的屏幕大小)

  • Actionable notifications

    • 收到推送时候的回调

      如下WKUserNotificationInterfaceController类 收到推送时候的回调方法:此处可 以设置以什么类型的Notification interface展示,static or dynamic。

                /**           *@description Remote Notification 接收回调           *@param  remoteNotification 推送内容           *@param  回调           *@returns void           */      - (void)didReceiveLocalNotification:(UILocalNotification *)localNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler {      //获取apns内容      // Use the following constant to display the static or the dynamic notification.      //dynamic      completionHandler(WKUserNotificationInterfaceTypeCustom);      //static      //    completionHandler(WKUserNotificationInterfaceTypeDefault);      }      /**       *@description Remote Notification 接收回调、设置使用的static还是dynamic类型notification       *@param  remoteNotification 推送内容       *@param  回调       *@returns void       */      ///接收到推送,展示前调用      - (void)didReceiveRemoteNotification:(NSDictionary *)remoteNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler {          //获取apns内容          //设置使用的static还是dynamic类型notification          // Use the following constant to display the static or the dynamic notification.          //dynamic          completionHandler(WKUserNotificationInterfaceTypeCustom);          //static          //    completionHandler(WKUserNotificationInterfaceTypeDefault);      }
      • 提供的推送测试文件:在对于target中可以设置 NotificationPayload.apns, 内容如下:

              {  "aps": {      "alert": {          "body": "Test message apns",          "title": "Optional title"      },      "category": "myCategory"  },  "WatchKit Simulator Actions": [                                 {                                 "title": "First Button",                                 "identifier": "firstButtonAction"                                 }                                 ,{                                 "title": "Second Button",                                 "identifier": "secondButtonAction"                                 }                                 ],  "customKey": "Use this file to define a testing payload for your notifications. The aps dictionary specifies the category, alert text and title. The WatchKit Simulator Actions array can provide info for one or more action buttons in addition to the standard Dismiss button. Any other top level keys are custom payload. If you have multiple such JSON files in your project, you'll be able to select them when choosing to debug the notification interface of your Watch App."  }
  • 响应notification 自定义按钮事件的回调方法

    如下watch app Extension 中 main InterfaceController类 :

      #pragma mark - Notification Action  //remote (identifier:事件id;remoteNotification:推送内容)  - (void)handleActionWithIdentifier:(NSString *)identifier               forRemoteNotification:(NSDictionary *)remoteNotification{      NSArray* identifiersArr = @[@"firstButtonAction",@"secondButtonAction"];      NSInteger tag = NSNotFound;      if ([identifiersArr containsObject:identifier]) {          tag = [identifiersArr indexOfObject:identifier];      }      switch (tag) {          case 0:{              NSLog(@"firstButtonAction is taped");              break;          }          case 1:{              NSLog(@"secondButtonAction is taped");              break;          }          default:{              NSLog(@"未找到action 对应的 identifier:%@",identifier);              break;          }      }  }  //local (identifier:事件id;remoteNotification:推送内容)  - (void)handleActionWithIdentifier:(NSString *)identifier                forLocalNotification:(NSDictionary *)remoteNotification{      NSArray* identifiersArr = @[@"<#identifier1#>",@"<#identifier2#>"];      NSInteger tag = NSNotFound;      if ([identifiersArr containsObject:identifier]) {          tag = [identifiersArr indexOfObject:identifier];      }      switch (tag) {          case 0:{              break;          }          case 1:{              break;          }          default:{              NSLog(@"未找到action 对应的 identifier:%@",identifier);              break;          }      }  }
  • 问题:

        如何设置是动态推送控制器上的button?    在NotificationPayload.apns文件中first button 和 second button 对应的josn块分别对应的就是first、second按钮。添加对应的josn就可以添加多个按钮。    *按钮事件可以自定义吗?可以    默认点击按钮就会进入对应的watch app应用中。此处可以类比iphone app中的notication。

小技巧

  • 创建项目后有一个watch App的Target,如果跑notification 和 glance 需要手动修改设置比较麻烦,我们可以通过添加target解决这个问题。

      在xcode左上角点击manage Scheme 后Duplicate Watch App的Target设置对应的Watch Interface。

  • 创建 notification 的 interface

    如下图系统提供了三种interface

    • interface controller (普通的)

    • notification interface controller (对应的就是static notification)

    • galnce interface controller (glance的)

    dynamic notitcation呢?

  • 创建dynamic notification interface controller:

    需要创建一个普通的 interface controller、使static notification interface controller 与之相关联即可。 我们可以在dynamic notification interface controller 上创建一些提供了Outlet的控件如Alert label,和一些允许创建的控件。如不允许创建button。

  • Glance 与 watch app 之间的通信,即更新、响应跳转相关的用户活动。

    • 在Glance interface controller 中,修改用户活动信息,代码如下

      在willactive中

        - (void)willActivate {  NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];  [self updateUserActivity:bundleIdentifier userInfo:@{                                                       @"controllerName": @"imageDetailController",                                                        @"detailInfo": @"This is some more detailed information to pass zd."                                                       }];

      }

    • 在 main interface controller 中 响应Glance点击启动watch app的回调方法。

    一定要在glance interface controller 中使用方法updateUserActivity: userInfo:更新用户活动信息才会调用如下方法。

          #pragma mark - UserActivity(Glance)      /**       *@description watch app启动的时候,响应glance点击,接收更新的切换相关的用户活动,即 UserActivity (called on root controller(s) with user info)       *@param       userInfo :用户信息       *@returns     void       */      - (void)handleUserActivity:(NSDictionary *)userInfo {          //do some           //  [self pushControllerWithName:userInfo[@"controllerName"] context:userInfo[@"detailInfo"]];      }

相关资源下载

  • 官方

    watchkit 相关资源:

    https://developer.apple.com/watchkit/

    demo :

    https://developer.apple.com/library/prerelease/ios/samplecode/WKInterfaceCatalog/WatchKitCatalogUsingWatchKitInterfaceElements.zip

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 荷兰猪一直叫该怎么办 人吃了缓冲泡沫怎么办 不小心吃了泡沫怎么办 不想养荷兰猪了怎么办 刚买的乌龟死了怎么办 剪猫指甲出血了怎么办 猫吃了酸性植物怎么办 金鱼在缸底不动怎么办 野兔子不吃不喝怎么办 让荷兰猪咬了怎么办 仓鼠的脚被棉花怎么办 仓鼠的脚变黑了怎么办 夏天小仓鼠生了怎么办 把仓鼠摔出血了怎么办 仓鼠摔成骨折了怎么办 孩子被仓鼠咬了怎么办 仓鼠不咬磨牙石怎么办 仓鼠妈妈跑了宝宝怎么办 买的仓鼠繁殖了怎么办 仓鼠没有鼠粮了怎么办 仓鼠被踩吐血了怎么办? 买的蓝莓太酸怎么办 荷兰猪夏天掉毛怎么办 被猫抓伤肿了怎么办 荷兰猪鼻子破了怎么办 荷兰猪吃了包菜怎么办 龙猫不爱吃主粮怎么办 荷兰猪躲起来了怎么办 荷兰猪一直叫是怎么办 龙猫不吃粗的主粮怎么办 转龙猫中暑症状+龙猫中暑怎么办 火车上空调太冷怎么办 格力空调太冷怎么办 未满月龙猫宝宝怎么办 房间小空调太冷怎么办 奶猫半夜不睡觉怎么办 龙猫不喜欢吃草粒怎么办 龙猫一直在发抖怎么办 龙猫牙齿掉了怎么办 龙猫晚上很吵怎么办 小狗把手咬破了怎么办