API for 1.IWatch
Features of the 2.API
3. Communication mechanisms
4.UI
1.watch API and Features
WatchKit Frame
Wkinterfacecontroller
Life cycle
Initializes the watch UI, generates the corresponding controller, and responds to the Init method
-(Instancetype) Initwithcontext: (ID) context;//load data or update interface added by current controller objects
When the currently loaded UI is displayed
-(void) willactivate;//update interface objects or handle other events
When the user switches the page or stops using it
-(void) diddeactivate;//should clean up the task or data here, the update will be ignored by the system
Page jump (two groups)
The first group (similar to Uiviewcontroller)
-(void) Pushcontrollerwithname: (NSString *) name context: (ID) context
-(void) Popcontroller;
-(void) Poptorootcontroller;
-(void) Presentcontrollerwithname: (NSString *) name context: (ID) context;
-(void) Presentcontrollerwithnames: (Nsarray *) names Contexts: (Nsarray *) contexts;
-(void) Dismisscontroller;
-(void) becomecurrentpage;
Note
(a) The identifier string set in storyboard in the first parameter of the push and present methods, watch Kit extension uses these APIs to deliver messages to the Watch OS, and the real UI load rendering is performed on the watch side
(b) Poptoroot is the controller that jumps to the main Entry point in storyboard
(c) Presentcontrollerwithnames we can present a group of controllers that will be displayed as page control
(d) Becomecurrentpage The current page is displayed as page control, this method can be called to change the page
Second group
-(ID) Contextforseguewithidentifier: (NSString *) Segueidentifier;
-(Nsarray *) Contextsforseguewithidentifier: (NSString *) Segueidentifier;
-(ID) Contextforseguewithidentifier: (NSString *) segueidentifier intable: (wkinterfacetable *) Table RowIndex: ( Nsinteger) RowIndex;
-(Nsarray *) Contextsforseguewithidentifier: (NSString *) segueidentifier intable: (wkinterfacetable *) Table RowIndex: ( Nsinteger) RowIndex;
"Note" can be set directly in the storyboard triggeredsegues, when using segues, also support push and model two jump mode
Responding to interactive events
Controls such as Button,slider,switch in Wkinterfaceobject can interact with users
"It's important to note that"
Wkinterfacecontroller contains wkinterfacetable instances, you can implement the default
-(void) Table: (wkinterfacetable *) Table Didselectrowatindex: (Nsinteger) RowIndex
Responding to click events on each row of the table is easier than UITableView
Glance (short-term reminders for users)
You can create a glance interface controller from storyboard to WatchKit extension, agree to inherit from Wkinterfacecontroller, enjoy the same life cycle, You can also interact with the user
When the user taps the Glance page, it jumps to our Watch app
Passing data using a custom Glanceinterfacecontroller
-(void) Updateuseractivity: (NSString *) type userInfo: (nsdictionary *) userinfo//For example we need to click on glance to go to a specific page, You can wrap the ID of the target page and other messages to be passed into the dictionary, and then implement the following method in the initial Interface controller
-(NSString *) actionforuseractivity: (nsdictionary *) useractivity context: (ID *) context//jump to the target page, The useractivity here is the userinfo of the above, the return nsstring is the target page of the Identifier,context is the context of the target page
Notification&wkusernotificationinterfacecontroller
When the main app supports notification, watch can display notifications when the user clicks the notification to enter the app
-(void) Handleactionwithidentifier: (NSString *) identifier forremotenotification: (Nsdictionary *) remotenotification
Or
-(void) Handleactionwithidentifier: (NSString *) identifier forlocalnotification: (Uilocalnotification *) The localnotification//method will be responded to, can implement both methods to get notification of the message, jump to the target page
A notifier interinterface Controller can be created via storyboard, enabling a custom notification interface
-(void) Didreceiveremotenotification: (nsdictionary *) remotenotification withcompletion: (void (^) ( Wkusernotificationinterfacetype interface)) Completionhandler
Or
-(void) Didreceivelocalnotification: (uilocalnotification *) localnotification withcompletion: (void (^) ( Wkusernotificationinterfacetype interface)) completionhandler//get the notification content and set a callback block for processing completion
Menu
Add the appropriate item in the context
-(void) Addmenuitemwithimage: (UIImage *) Image title: (NSString *) title action: (SEL) Action;
-(void) addmenuitemwithimagenamed: (NSString *) imageName title: (NSString *) title action: (SEL) Action;
-(void) Addmenuitemwithitemicon: (Wkmenuitemicon) Itemicon title: (NSString *) title action: (SEL) Action;
-(void) clearallmenuitems;
Wkinterfaceobject
Responsible for the elements of the interface, only responsible for transmitting the response events in the WatchKit Extension and watch app, the specific UI rendering is done in the Watch app
The Watch app and IOS app are completely different in layout, unable to specify the specific coordinates of a view, cannot be AutoLayout, can only be laid out in the basic unit of behavior, and if multiple elements are displayed in a row, the Wkinterfacegroup layout
Wkinterfacetable
You can add a table directly to the Controller by storyboard, each table contains a table row Controller (inherited NSObject) by default, which is equivalent to the previous cell, You can use the style of each row to set the identifier to differentiate
You can set the style of each row of table by the following two groups, rowtype the corresponding identifier of the row controller
-(void) Setrowtypes: (Nsarray *) rowtypes;
-(void) Setnumberofrows: (Nsinteger) numberofrows withrowtype: (NSString *) RowType;
You can get the row Controller for a row by using-(ID) Rowcontrolleratindex: (nsinteger) index
Cases
-(void) Loadtablerows {
[self.interfacetable setNumberOfRows:self.elementsList.count withrowtype:@ "Default"];
Create all of the table rows.
[Self.elementslist enumerateobjectsusingblock:^ (nsdictionary *rowdata, Nsuinteger idx, BOOL *stop) {
Aaplelementrowcontroller *elementrow = [self.interfacetable rowcontrolleratindex:idx];
[Elementrow.elementlabel settext:rowdata[@ "label"]];
}];
}
Delete Row of table
-(void) Insertrowsatindexes: (Nsindexset *) rows Withrowtype: (NSString *) RowType;
-(void) Removerowsatindexes: (Nsindexset *) rows;
Wkinterfacedevice
This is a singleton class that can get some information about the current watch
@property (nonatomic,readonly) CGRect screenbounds;
@property (nonatomic,readonly) cgfloat Screenscale;
@property (Nonatomic,readonly,strong) Nslocale *currentlocale;
@property (nonatomic,readonly,copy) NSString *preferredcontentsizecategory;
In addition, we can use the following set of methods in this class to cache pictures for future use:
-(void) Addcachedimage: (UIImage *) image name: (NSString *) name;
-(void) Addcachedimagewithdata: (NSData *) imageData name: (NSString *) name;
-(void) Removecachedimagewithname: (NSString *) name;
-(void) removeallcachedimages;
Wkinterfaceimage
Images that have been cached can be read directly using the following API:
-(void) Setimagedata: (NSData *) ImageData;
-(void) setimagenamed: (NSString *) imageName;
WatchKit allows each app to cache up to 20MB of images, and if more than that, WatchKit will start deleting the oldest data and make room for new data.
Features of the Apple Watch API