Introduction to Apple Watch Development (i) WatchKit App architecture

Source: Internet
Author: User

The WatchKit app and the WatchKit extension implement the app's interface. When the user is working in Apple Watch, the WatchKit app chooses the right scene from the storyboards. For example, if a user views glance, it chooses the glance scene. When selecting a scene, WatchKit notifies the corresponding iphone to launch WatchKit extension to create an object that manages the scene. When the common configuration is complete, it appears on Apple Watch. This kind of data transfer in the WatchKit app and the WatchKit extension is done entirely behind the scenes.

Shows the communication between the WatchKit app and the WatchKit extension.

Management Scenario: Interface Controller

Each scene is managed by an interface controller object, which is an instance of the Wkinterfacecontroller class. Since only one interface is displayed on the screen at a time, the app will switch the interface controller according to user actions when it presents the new interface. The app can have a modal interface. The type of switchover also determines how the interface is displayed.

Note: The glance and custom notification interfaces use special interface controllers, which differ from other interfaces.

WatchKit app life cycle

The user's action starts the app and also determines its life cycle. The user launches the app from the main screen, viewing the glance or viewing the notification interface. Each interaction will start the WatchKit app and the corresponding WatchKit extension. Watch app and WatchKit will always send messages to instruct users to stop manipulating the app, and this time iOS hangs estension know the next user action.

At startup, WatchKit loads the appropriate scene for the current operation. If the user views the app's Glance,watchkit load the glance scene from the storyboard. If the user launches the App,watchkit Load app initialization scenario directly. After loading the scene, WatchKit notifies WatchKit extension to create the corresponding interface controller to prepare the scene for presentation. Shows the process.

Using the interface controller's init and Awakewithcontext: method to load the required data, set the value of the object, and prepare for the display of the interface. Do not use willactivate to initialize the interface. The Willactivate method is only called in a very short time to be displayed, so you can only make a smile change with this method. For example, use this method to start an animation, or to start an action that is only performed when the interface is displayed.

When the interface is on the screen, the user interface's operation will be handled by the custom method of the interface controller. When a user operates a control such as a table, a button, a switch, WatchKit will call your method to respond. Use these methods to update the interface and to perform related characters. If you want to perform a task at another time, you can use Nstimer to implement it.

Note: The glance interface does not support event methods. Clicking on the Glance screen will launch the app.

WatchKit extension will run when the user operates Apple Watch. Apple Watch is generally a short operation, and all interface controllers should be lightweight and do not perform long-time tasks. When the user exits the app or stops the Apple Watch operation, iOS stops the interface controller and hangs the WatchKit extension. Such as

Different tasks performed by the app in different states

In different states of the app declaration cycle, iOS calls the Wkinterfacecontroller object's method to let it handle it. The following table lists the methods that are basically implemented, and what they do.

method tasks performed
init
awakewithcontexxt: Configure the interface controller with any available data. Use it to load data, update tags, images, tables, and other objects in storyboard. The context data is specified when the new controller is configured. For example, when you create a new interface in a multi-tiered interface, you specify the context data to be displayed for the next interface. It is recommended to provide a context object but not necessary.
willactive The interface will be displayed to the user immediately. This method makes a small change in the interface. For example, use this method to change the label content based on the new data. The initialization of the interface is still done in the Init and Awakewithcontext: Methods
diddeactivate

Code to debug the state on the emulator

In the test, you can lock and unlock the emulator to confirm that the activation drink deactivation code works as expected. When using the Hardware>lock command to lock the emulator, WatchKit invokes the Diddeactivate method of the current interface. When you unlock the emulator, WatchKit calls Willactivate.

Share data with an existing iOS app

If the iOS app and WatchKit extension rely on the same data, use the Shared app group to store the data. The APP group is thread-safe. Because WatchKit extension and iOS apps are in different sandbox environments, they can't share files and communications directly. APP Group allows two of processes to share files and data.

Set up a shared app group in the iOS app and the Capabilities tab of the WatchKit extension project. Add a entitilements file for each target and add a com.apple.security.application-groups element to the file. All target needs to select the same app group.

At run time, share data by reading and writing to shared folders. Use Nsfilemanager's Containerurlforsecurityapplicationgroupidentifier: method to get the folder root address. Use this URL to enumerate the contents of a folder or create a new file.

You can also create a Nsuserdefaults object with the group ID to share the data. The Nsuserdefaults object created by using the Initwithsuitename: Method can access the shared data. Both IOS app and WatchKit extension can read and write data.

Communicate directly with the iOS app

The app can use the Openparentallication:reply: method to send requests and receive responses to the iOS app. WatchKit extension does not support background run mode. They only run when the user operates Apple Watch. IOS apps don't have that many restrictions, they can be configured to run in the background to get data for watchkit extension. Some activities may take some time to complete, such as getting the user's location, which should be done by the iOS app and then transmitted to WatchKit extension.

When calling the Openparentapplication:reply: Method, iOS starts or wakes the app in the background and then calls the app Delegate's Application:handlewatchkitectensionrequest: Reply: Method. The APP delegate through the information provided and then returns the results to WatchKit extension.

Introduction to Apple Watch Development (i) WatchKit App architecture

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.