The communication mechanism of Apple watch

Source: Internet
Author: User
Tags notification center

Communication mechanism

(1) Users need to turn on Bluetooth connection watch in the app settings, adjust the interaction between the watch and iphone, Bluetooth distance of 9 meters, over distance, disconnected

(2) The iphone is on the same Wi-Fi network as the AppleWatch (or connected to a Wi-Fi hotspot built by the iphone), so long as the signal is present, the distance between the two sides can be

(3) Watch app is added to the project and includes watch app and WatchKit Extension. The watch App is located on iwatch and is currently only allowed to contain storyboard and resource files, and watch Extension is located on the user's iphone-installed APP, which includes the code logic and other resources we need to implement. These two sections are connected via watch kit, and when the user clicks the Watch app, the iphone that matches watch will start WatchKit extension and then connect with watch to create a communication

APIs for iphone and Apple Watch communications

(1) using the openparentapplication:rely in Wkinterfacecontroller: The method wakes the iphone main application in the background, the main application goes through the processing of the network data, processing the data needed to return the WatchKit extension.

WatchKit Extension Send request wake-up main app

+ (BOOL) openparentapplication: (nsdictionary *) UserInfo reply: (void (^) (nsdictionary * replyinfo,nserror * error)) reply ;

(2) The main application process WatchKit request method, Uiapplicationdelegate

-(void) application: (uiapplication*) Application handlewatchkitextensionrequest: (nsdictionary*) UserInfo reply: ( void (^) (nsdictionary *)) reply;

It is important to note that the main application executes the Uiapplicationdelegate method every time, WatchKit the request after the completion of the reply (replyinfo), otherwise this method will respond to failure.

The IPhone app communicates with its Watch app

The IPhone app can use the Darwin Notification Center (Core Foundation Framework's API) to notify WatchKit Extension a specific event

"Note" An app has only one Darwin Notification Center, all Drawin notifications are system-level, to be notified, the main thread needs to run in a common mode, Watch and iphone Must be run in the foreground to process sending and receiving Darwin notifications, not transmitting objects via Darwin notification, only with a name and UserInfo dictionary, Darwin notification is non-persistent, instant delivery, if placed backstage, the notification will be lost

Share data with app Groups

It's easy to share small amounts of data between iOS apps and WatchKit extensions using app Groups and Nsuserdefaults, and for other resources like core data, you can use shared containers to simplify data access between them and keep up-to-date data

Using handoff

Handoff allows users to easily switch between iphone and Apple Watch, the Handoff API in Wkinterfacecontroller can be used in glance and custom notification interfaces and can be passed to the application's main interface controller

iphone and watch two-way communication iOS9 (either watch sends data to the iphone or lets the iphone actively send data to Watch)

Watch Connectivity

1, the Official document code

The first line of the session is always supported watchOS, this is to check whether iOS settings support session

The second row gets a default session instance

The third line sets the delegate for the session

Row four sets the session to an active state

Status of 2.WCSession

Whether the paired is paired with the device

Watchappinstalled Watchapp is installed

Complication is available

3. Classification of communications The-watchconnectivity framework has two modes of communication, one is background transmission and the other is interactive message

Background transmission is the most common mode of communication, content-oriented and user interaction, mainly for the transmission of non-instant content, reflected in the content can be intelligent transmission by the operating system (the operating system allows the sender to exit, select a transport mechanism, support recipients to send the next boot, and send the content in a queue)

Type: Application context content transfer, user data transfer, file transfer

Implementation steps:

Get the default wcsession and turn on

Implement Wcsessiondelegate Receive callback method

Send a message and process a response message

Receive-side processing receive and response (optional)

The difference between watchconnectivity in IOS8 and iOS9

The communication between watch and iphone in IOS8 can only be done via Watch's unsolicited request, and the iphone responds to this request in a way that does not allow the iphone to directly connect to watch and send data to watch

Bidirectional operation in IOS9

The contents of the data sent in iOS8 can only be normal dictionary data types

IOS9 not only can send simple dictionary data, but also can support sending some data (nsdata) and file type

The communication mechanism of Apple watch

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.