Directory:

  • preface
  • I. Introduction to Apple Watch
  • Ii. Watch App structure in the project
  • 3. Features and principles of apps on Apple Watch
  • Iv. Watch App UI introduction
  • V. Communication between App Watch and iPhone
  • 6. Data sharing between Watch App and watchKit Extention
  • 7. Watch App notification
  • 8. The App Watch sends network requests separately

Preface:

With the development of the current Apple ecosystem, more and more apps will migrate their simplified versions from iOS to WatchOS (Alipay, wechat, Q, Toutiao, QQ Music, netease Cloud Music, etc., all have Watch apps).

I. Introduction to Apple Watch

Apple Watch is the company’s “wellness” smartwatch.

The original Apple Watch 1 was released in 2014, and by 2020, the Apple Watch 5 had been released.

On March 10, 2015, Apple held its 2015 spring product release conference at Moscone Center in San Francisco to officially announce the Apple WATCH, which is divided into sports, standard and customized versions. It will be pre-ordered on April 10, 2015 and released on April 24.

Apple Watch supports phone calls, voice and text messages, car, weather, flight information, map navigation, music playing, heartbeat measurement, step counting and dozens of other functions. It is a comprehensive health and exercise tracking device.

There are two kinds of Apple Watch apps:

  • Watch App for iOS App: The Watch App is migrated from iOS and can communicate with the iOS App.
  • Watch App: A standalone Watch App that can be installed independently on the Apple Watch.

Mostly the first one, Watch App for iOS App.

Ii. Watch App structure in the project

Create a new target for watchOS.

  • At this point, two targets appear: WatchKit App and WatchKit Extension.

Note: In WatchOS, you can’t rely on UIKit to write complex interfaces like iOS can. At the moment, you can only rely on storyboards to build simple UI interfaces and interface jump logic.

3. Features and principles of apps on Apple Watch

Let’s talk about the features and principles of apps on Apple Watch from the perspective of development.

Apple Watch operating mode

First of all, the app on the Watch and the APP on the iPhone are linked, that is, the app developed on the Apple Watch is inseparable from the iPhone. Why? This is related to its operation strategy. The CPU, hardware and power of the watch are limited, so it is very important to save space and power, so the interface design of the watch should be streamlined and the operation is less.

The Watch app and iPhone App are seamlessly related, because iPhone runs all the apps on The Watch. The close proximity between the phone and The Watch and the infinite transmission saves a lot of work for The Watch to some extent. The Watch is responsible for displaying the results of iPhone processing. This is also a binding sales strategy. The app on the Watch is actually a part of the app on the iPhone, an extension.

Apple Watch App interface type

Watch App has three ways to interact with users, or three interfaces:

  • Regular Watch app is necessary. The display and simple operation of some simple contents can be regarded as a simplified version on mobile phones.
  • Glance interface is a purely suggestive interface, which cannot interact with users. This part is optional.
  • Notification type interface, which is used for message notification, mobile phone can also receive push through this interface display, which can interact with the user.

The relationship between Watch App and iPhone app

Watch App and iPhone App are linked, but also bound, as a whole. The Watch App is not a separate project, but a Target extended in iOS project. There are no codes on the Watch app, only interface resources. The code runs on the iPhone.

Before WatchOS 2, the WatchKit App was responsible for the presentation, installed on the Watch, and the WatchKit Extension was responsible for the business and control logic, installed on the iPhone. All the computing, logic and control is done on the iPhone. Watch App and iPhone App are bribed through Watchkit. All the code execution logic is run on iPhone, storyboards and picture resources are displayed on Watch, and the installation and operation of Watch App is controlled by iPhone.

  • In addition, one iPhone can be associated with one Apple Watch at a time, which is a one-to-one relationship. The Apple Watch can only be paired with aN iPhone, not an iPad.

After WatchOS 2, the biggest change was porting WatchKit Extension directly from the iPhone to the AppleWatch.

Watch App startup process

The figure above shows the loading process of Watch App. It can be seen that the loading of Watch App depends on iPhone. Start the app, load storyboard, pictures and other resources of Watch terminal, and then initialize UI. After initialization, UI will be displayed. The actions triggered by UI are to call the code in iPhone extension and display the execution result on Watch.

The init method we just mentioned is exactly what its name suggests, awakeWithContext: And the way to do that is to initialize the UI with the information that’s passed in from the iPhone, just like we did before viewDidLoad, willActivate is just like viewWillAppear, so it should make a lot of sense.

Watch App life cycle

Apps on iPhone have their own life cycle. As for the life cycle of Watch App, we can see intuitively from the above figure that every step from UI display and user interaction to the end of app is in close communication with the extension of iPhone corresponding app.

  • WKInterfaceController inherits from NSObject and WKInterfaceController is UIViewController. The lifecycle methods are:

    1. -initWithContext: called when it is initialized, which is where the view elements are configured, equivalent to -viewDidLoad
    2. -willActivate is called when the view element is about to be rendered. You can also set the view element in this method, equivalent to -viewwillAppear
    3. -viewDidAppear is called when it’s already presented, which is the same thing as -viewDidAppear
    4. -willDisappear is called when it is about to disappear equivalent to -viewwillDisappear
    5. -didDeactivate is called after rendering. In this method, objects holding self, such as NSTimer, are deactivated. The equivalent of – viewDidDisappear
  • WKInterfaceObject and its subclasses

    1. WKInterfaceObject acts as a proxy for UIView. The actual View rendered by WatchKit is invisible to the developer. UI object properties can only be set through WKInterfaceObject, and very few properties can be set.
    2. WKInterfaceButton acts as a proxy for UIButton
    3. WKInterfaceImage acts as a proxy for UIImageView
  • WatchKit is independent of UIKit, all classes inherit from NSObject, there is no complete Response chain

Iv. Watch App UI introduction

This section will be covered in a future article

We mainly use WatchKit framework for UI development. Compared with UIKit, it has fewer functions and is more concise.

Watch App layout

  • The view development of Watch App cannot use code, but must be StoryBoard. The layout is as follows.

  • The layout of the Watch App is completely different from that of iOS apps. Cannot use autoLayout or coordinates. Use only relative layouts.

    1. Level: Left center right
    2. Vertical: top center bottom
  • View Size can be set in three ways:

    1. Size To Fit Content: View Size To Fit Content
    2. Relative To Container: Sets the size of the Container
    3. Fixed: Sets a Fixed width and height

Common UI controls

  • WKInterfaceObject – (equivalent to UIView)

  • WKInterfaceGroup

    • None of the other WKInterfaceObject subclasses can overlap. Only this class can overlap layout with other WKInterfaceObject objects.
      1. Cannot overlap as shown in the figure:

2. OverlapCopy the code

  • WKInterfaceButton – (equivalent to UIButton)

  • WKInterfaceLabel – (equivalent to UILabel)

  • WKInterfaceImage – (equivalent to UIImageView)

  • WKInterfaceTable – (equivalent to UITableView)

  • WKInterfacePicker – (equivalent to UIPickerView)

  • WKAlertControllerStyle – (3 popovers)

    1. alert

  1. actionSheet

  1. sideBySideButtonsAlert

  • WKInterfaceMenu – (trigger display when pressed)
    1. The Apple Watch’s Retina display is Force Touch enabled, providing a new way to interact.
    2. This is a WatchOS specific class that activates Menu on the page when it is configured on the page by pressing the screen. It also displays the operations under Menu, which are of another type, WKInterfaceMenuItem.
    3. Menu displays up to four MenuItems, which can be associated with their respective click events.

  • The other…

Navigation development

  • All navigation methods can be implemented in code or SB

  • Stack navigation, similar to UINavigationController. Touch the upper left corner to return

    1. PushControllerWIthName: context: the first parameter is the Controller corresponding to the Identifier string, set up in SB. You can pass data through context.

2. popController
3. popToRootController
Copy the code
  • Modal way
    1. presentControllerWithName:context:

2. dismissController

  • Paging navigation
    1. Similar UIPageController. Swiping left and right
    2. PresentControllerWithNames: contexts: incoming names and contexts array, in this way be exhaled Controller will appear in the page navigation.

Apple Watch vs. iOS

  • You can only use the storyboard to drag and drop the controls to build the basic UI.
  • Simple layout, vertical layout by default. Vertical layout requirements can be accomplished by nesting groups.
  • So you’re going to have to rely on the contextForSegue method to transfer values between interfaces.
    • Set segueIdentifier in the storyboard.
    • Awake (withContext Context: Any?) Method receives the parse context.

V. Communication between App Watch and iPhone

WatchOS has a WatchConnectivity framework that is responsible for communication between WatchOS and iOS. Use the Connectivity framework to communicate between WatchKit Extension and iOS apps. The framework provides two-way communication between two processes and allows data and file transfer in the foreground or background.

  1. In data transmission, you can use NSDictionary objects that contain the data to be sent for data transmission.

  2. Dictionary keys and values must both be property list types. Examples are NSNumbe, NSString, NSArray, NSDictionary, Bool, and other basic types

  3. If you need to include types that are not property list types, you can package them into AN NSdata object or write them to a file before sending them.

WCSession

  1. The WatchConnectivity framework mainly uses the WCSession class for data transmission.

  2. There are also some properties in the WCSession class that are only available in iOS apps. IsPaired isWatchAppInstalled, isComplicationEnabled remainingComplicationUserInfoTransfers, watchDirectoryURL. These are only available in the iOS App.

  3. Before enabling Session, you need to check whether the current iOS device supports the Connectivity framework by calling the isSupported() method.

  1. There is no need to check whether the Connectivity framework is supported before activating the WatchKit Extension Session, as WatchOS must support the Connectivity framework.

  1. The iPhone sends messages to the Apple Watch

  1. The Apple Watch sends messages to the iPhone

Communication mode

Foreground real-time transmission

  • Foreground transmission is real-time transmission, message dictionary transmission and message data transmission.

Message dictionary transfer

Background unscheduled transmission

Background transmission is divided into coverage transmission, queue transmission.

Overlay transmission
  • Overwriting in background transmission means that when you transfer data, if the first data has not been sent, the second data transfer at this point will overwrite the first data. In this case, the data received by the data receiver will only have the second data, and the first data will be lost.
  • Queue transfers in background transfers mean that a subsequent transfer will not overwrite the data transferred in the previous one. The system will send all the data in order.
Queue transmission
  • Dictionary transfer,
  • File transfer,
  • Dial data transmission.

6. Data sharing between Watch App and watchKit Extention

Media files can be shared between the Watch App and WatchKit Extension at runtime using the Shared App Group.

  1. Enable the app Group function:

  2. Set data to APP group:

  3. Get content from app Group:

7. Watch App notification

  • Starting with WatchOS 3.0(corresponding to iOS 10.0), notifications start using the UserNotifications framework.
  • Apple Watch notifications come in two types: short-look and long-look

Short-look:

When the Apple Watch is first notified, it displays a myopic screen. Short-look is a simple notification preview, and the notification interface for short-look is not customizable. If the user continues to view the notification, the system quickly changes the short-look interface to the long-lock interface.

Long-look(Long view plane):

The long view plane is a scrollable screen that displays the contents of the notification and any associated action buttons. If a custom notification interface is not provided, the Apple Watch displays a default screen that includes the app icon, the title of the notification, and the content of the notification. The Apple Watch will display a custom notification screen if one is available.

The long-look notification interface is divided into three areas: sash area, Content area, and Bottom area.

  • Sash area: An overlay area that contains the application icon and application name. Its color is customizable.
  • Content area: Contains details about incoming notifications, which is the main custom area.
  • The Bottom area: contains the close button and actionable buttons registered in iOS.

Custom long-look

The custom long-look notification interface consists of two independent interfaces: one is Static and the other is Dynamic.

  • Static interfaces are required and are an easy way to display notification messages and configure any Static images, text.
  • The Dynamic Interface is optional and provides a way to customize the display of notification content at run time.
  • WatchOS displays Dynamic interfaces in all but the following cases:
    • When dynamic interfaces are unavailable
    • There is not enough power to display dynamic interface
    • When WatchOS is explicitly told not to display the dynamic interface:

  • The purpose of a static interface is to provide a stable interface in cases where WatchKit Extension cannot configure a dynamic interface in a timely manner. The notification page is also displayed in the notification center. Rules for creating static interfaces:
    • All images must be in the Watch App’s package.
    • Interfaces must not contain Control, table, Map, or other interactive controls.
    • The notificationAlertLabel on the interface must be externally connected to the Lable. The contents of the Label will be set to the notification message

How to determine active devices

  • Notifications will be pushed to the iPhone when the screen is unlocked.
  • When the Apple Watch is not on the user’s wrist, notifications will be pushed to the iPhone.
  • When the Apple Watch is on the user’s wrist and the iPhone is locked, the notification will be pushed to the Watch.
  • In the remote notification push, if the notification is pushed to the Watch, the iPhone terminal can receive the notification, but there will be no reminder, and the screen will not even light up.
  • If you want to push an Apple Watch when it’s not wearing it, you can also disable the wrist detection option in the general Settings. Just make sure the Apple Watch is not on the charger.

8. The App Watch sends network requests separately

  • The APP Watch also uses NSUrlSession to send network requests.
  • Also supported by AFN, Kingfisher.. Such as ios third-party universal library.

reference

  • If there is infringement, contact must delete!
  • If there are incorrect places, welcome to guide!
  • If you have any questions, feel free to discuss them in the comments section!