Some details and summaries of Apple watch development

Source: Internet
Author: User

This article is intended to summarize the problems and details that have been encountered in the recent watch development.

1, about watch real-machine debugging problems

In general, you created a extention for the iOS master app, such as today Extension. Xcode will automatically help you generate the extention AppID, and then generate the corresponding provisioning profile. Then in the Targets-->build settings-->code Signing Select the corresponding provisioning profile, tick the corresponding keychain can run.

If you create a applewatch for your project, you'll add the WatchKit extension and WatchKit app two target to the original project. Then you need to create the correct bundle identifier for the WatchKit extension and WatchKit app in the Apple Developer Center respectively. The new bundle identifier for WatchKit both extensions are extended from the bundle identifier of the main application, and such naming rules must be followed. If your main application bundle identifier is Com.jaybin.myapp. Then WatchKit extension bundle identifier must be com.jaybin.myapp.watchkitextension. The bundle identifier of Watchkitapp must be com.jaybin.myapp.watchkitapp. Because on IOS systems, the app ontology is the core. All the running entities are based on the ontology.

Here you can create the corresponding provisioning profile for the AppID of the WatchKit two target. In addition to the main application, 6 provisioningprofiles are required. 3 x Development profiles,3 Archiving/store Profiles.

The final step is to deploy the certificate to the Xcode project to run or package. First, follow the steps below for Xcode >preferences > Accounts > Your_account > View Details.


You can see all the certificates under the account, select one of the profiles, choose to open in the Finder, and then delete all the certificates under the folder, so that the original invalid profiles can be emptied. Then click the Refresh button in the bottom left corner of the view to refresh the certificate, and Xcode will download the latest certificate, including the new profiles you just created. Finally, make sure that target, primary target, and WatchKit target in the project are selected to associate with the correct team. Then choose the correct provisioning Profiles (typically provisioning Profiles select Automaic to run). But oddly enough, I only created the corresponding AppID and profile for WatchKit extention to run. The estimate is that Xcode automatically helps us to set the provisioning profile for the WatchKit app.


Don't forget to clean the project before you run it, because Xcode sometimes caches the old profile and settings.

At the end of the note, AppleWatch's dev/real-machine commissioning is the same as the iphone, and it also needs to be added to the Dev Center account under the Development test facility. Looking at AppleWatch's udid and looking at iphone devices, if watch and iphone have been paired successfully, check out the udid of watch under the Udid page of the iphone. Window > Deivces can be viewed under Xcode.


2. Special navigation type of WatchKit app

There are two types of navigation in the form and style

1) Layered style (push/pop, present and the same navigation style on iphone)

2) Paging style (Presentcontrollerwithnames, becomecurrentpage)

Specific view switch, page Jump API

-(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;

It is important to note that when using this set of APIs for view switching and jumping, the first parameter of the push and present methods is the corresponding identifier string set for Wkinterfacecontroller in storyboard.

Of course, for Watchkitapp, it is possible to build multiple Interfacecontroller in the storyboard and visually draw the view transformation connection as in the iOS app, which can be switched and jumped by the view controller code. Set the triggered segues directly in the storyboard. When using segues, selection also supports push and model two jump modes. With regard to paging, we can also control from view a by dragging to view B in storyboard to select Next page to establish this relationship.

One of the most notable concerns about Watchkitapp navigation is the mix of paging navigation (page-based) and hierarchical Navigation (Stack) mode. Because in the switch and jump of the view, in order to facilitate the interaction with the user we often need a variety of navigation ways to mix. However, paging and hierarchical navigation are mutually exclusive and must be toggled using modal mode (Present).

For example, when the main controller is Paged view (page navigation with page-based), to correctly pop up a hierarchical view stack can be used Presentcontrollerwithname:conext: method, and the master controller is a hierarchical view, to correctly pop up a single page view, Also with Presentcontrollerwithname:conext:.

        If you want to pop up a paging view of multiple pages, we can present a group of controllers that will be in page Control in the form of display, then need to change with presentcontrollerwithnames:contexts:. However, there is a problem if I present a set of controllers in a hierarchical view, that is, the currently displayed view is displayed as a page control. For this Component page view, the upper-left corner of all views defaults to a return "Cancel" button, and when clicked, the current set of views will be dismiss off, returning to the previous layer of view. But if I present a modal view in the current Component page view and then return, I find that the return "Cancel" button in the upper left corner of the previous Component page view disappears and cannot be returned to the previous layer. It seems that calling Dismisscontroller in these views does not work. So, for presentcontrollerwithnames:contexts: if present out a set of controllers, in this set of controller views do not present the modal view, Otherwise, this component page view will not be able to return to the previous level page.

However, there is a good way to do this with pagination view, Reloadrootcontrollerswithnames:contexts: When the app launches, if you want to present the view controller as a paging interface, the user can swipe left and right to toggle the view. Then we can call the Reloadrootcontrollerswithnames:contexts: Method in the Init method of the initial interface controller. In this case, we typically configure a set of initial sets of pages in the storyboard file. When the app launches, WatchKit instantiates and initializes the first displayed page, the initial interface controller (Maininterfacecontroller), and then the other interface controllers in the paging interface. This means that when the system loads the WatchKit app interface, it will instantiate and initialize all the interface controllers that comprise the interface at once. When the user switches from one interface controller to the next, it invokes the Diddeactivate method of the current interface controller and the Willactivate method of the interface controller that is about to be displayed. The Willactivate method ensures that the information in the interface is up-to-date. Of course we can also call this method in the Willactivate method while the app is running.


3. Data communication on Watch and iphone main application

If a request for data is made in WatchKit extension, such as a network call.    This will result in the inability to reuse the existing code in the project, will write a lot of duplicate code, the performance of the optimization of the space is not small. So if the Apple Watch app needs to perform long-running tasks in the background, such as a network call, you should let the iphone's main app do the job.


1) Use the openparentapplication:reply in Wkinterfacecontroller: method in the background to wake up the iphone main application, the main application to do network data processing, The data required for the WatchKit extension is returned after processing is complete.

WatchKit extension Send request wakes the main app:

+ (BOOL) openparentapplication: (nsdictionary *) UserInfo reply:

(void (^) (Nsdictionary*replyinfo, Nserror *error)) reply;


WatchKit extension, the specific demo is as follows:

        WatchKit extension sends the request to the main application, wakes the main application    nsmutabledictionary *userinfo = [nsmutabledictionary dictionary];    [UserInfo setobject:@ "Say Something" forkey:@ "OpenType"];    [Wkinterfacecontroller openparentapplication:userinfo reply:^ (nsdictionary *replyinfo, NSError *error) {                //The callback after the main application finishes processing, returns the data required for extension        nsstring *words = [Replyinfo objectforkey:@ "words"];        NSLog (@ "say:%@", words);    }];        


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

-(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.


In the main application, the demo is as follows:

The main app handles requests from WatchKit extension-(void) Application: (UIApplication *) application handlewatchkitextensionrequest: ( Nsdictionary *) UserInfo reply: (void (^) (nsdictionary *replyinfo)) reply{        if (userInfo) {        NSString *openvalue = [ UserInfo objectforkey:@ "OpenType"];        if (Openvalue && [@ "Say something" isequaltostring:openvalue]) {            nsmutabledictionary *replyinfo = [ Nsmutabledictionary dictionary];            [Replyinfo setobject:@ "Hello world!" forkey:@ "words"];                        After the main application processing is complete, the callback is from the reply (replyinfo) of the watchkit extension, otherwise the method response fails            reply (Replyinfo);}}        


This completes the data communication between the WatchKit extension and the main application. Output printing in WatchKit extension is:

Say:hello world!

That is, WatchKit extension the data transmitted by the main application by asking for it.

Another way to communicate is in our well-known iOS app, where the main app and extension communicate data sharing through app group.





Some details and summaries of Apple watch development

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.