Apple Watch Kit(1)- 开发一瞥

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"]];
          }
    

相关资源下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值