archive-com.com » COM » T » TECHOTOPIA.COM

Total: 1018

Choose link from "Titles, links and description words view":

Or switch to "Titles and links view".
  • Building an Example watchOS 2 WatchKit App - Techotopia
    the Image object needs to be configured to display the image when the app runs Select the Image object in the storyboard scene and display the Attributes Inspector in the utilities panel View Utilities Show Attributes Inspector Within the inspector panel use the drop down menu for the Image attribute to select the watch image option Figure 3 7 Finally select the Label object in the scene and use the Attribute Inspector panel to change the Alignment attribute so that the text is centered within the label Having set this attribute a review of the scene will show that the text is still positioned on the left of the layout The reason for this is that the text has been centered within the label but the Label object itself is still positioned on the left side of the display To correct this locate the Alignment section in the Attributes Inspector panel and change the Horizontal attribute from Left to Center Figure 3 8 shows the Attributes Inspector panel with these attributes set Figure 3 8 Running the WatchKit App All that remains is to run the WatchKit app and make sure that it appears as expected For the purposes of this example this will be performed using the simulator environment In order to test the WatchKit app the run target may need to be changed in the Xcode toolbar Select the current scheme in the toolbar and use the drop down menu Figure 3 9 to select the WatchKitSample WatchKit App iPhone 6 Apple Watch 38mm option Figure 3 9 With the WatchKit app selected click on the run button Once the simulator has loaded two windows should appear one representing the iPhone 6 device and the other the Apple Watch device After a short delay the WatchKit app should appear on the watch simulator display as illustrated in Figure 3 10 Figure 3 10 Running the App on a Physical Apple Watch Device In order to test the app on a physical Apple Watch device connect an iPhone with which an Apple Watch is paired to the development system and select it as the target device within the Xcode toolbar panel Figure 3 11 Figure 3 11 With WatchKitSample WatchKit App still selected as the run target click on the run button and wait for the app icon to appear on the Apple Watch home screen and for the app to launch eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters Setting the Scene Title and Key Color The area at the top of the Apple Watch display containing the current time is the status bar and the area to the left of the time is available to display a title string To set this property click on the scene within the storyboard so that it highlights in blue display

    Original URL path: http://www.techotopia.com/index.php/Building_an_Example_watchOS_2_WatchKit_App (2016-02-13)
    Open archived version from archive


  • An Overview of the watchOS 2 App Architecture - Techotopia
    been defined using the IBOutlet keyword In programming terms an IBOutlet is simply an instance variable that references the user interface object to which access is required The following line demonstrates an outlet declaration for a label IBOutlet weak var myLabel WKInterfaceLabel Once outlets and actions have been implemented and connected all of the communication required to make these connections work is handled transparently by WatchKit Figure 4 1 provides a visual representation of actions and outlets Figure 4 1 Outlets and actions can be created visually with just a few mouse clicks from within Xcode using the Interface Builder tool in conjunction with the Assistant Editor panel a topic which will be covered in detail in the chapter entitled An Example Interactive WatchKit App WatchKit App State Transitions At any given time a WatchKit app will be in one of the following three possible states Not running Either the WatchKit app has yet to be launched or it has been terminated by watchOS Inactive The WatchKit app is running but is not receiving any events from WatchKit An app usually enters this state briefly during the launch process or when the app is transitioning out of active state because the user is no longer interacting with the app Active The WatchKit app is running is most likely the currently visible app on the Apple Watch and is receiving events As a WatchKit app transitions from one state to another the app is notified of the state change via calls to the methods in the extension delegate object eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters The WatchKit Extension Delegate In addition to an interface controller instance for each scene in the WatchKit app the WatchKit extension also includes a single extension delegate object This class is added to the WatchKit extension automatically by Xcode when a WatchKit app target is created and conforms to the WKExtensionDelegate protocol which defines which methods this class must implement These methods are then called by the WatchKit framework to notify the WatchKit app about lifecycle state transition events and notifications By adding code to these methods the WatchKit app can respond to the notifications where necessary At a minimum the extension delegate class will implement the following lifecycle methods applicationDidFinishLaunching Called by WatchKit when the WatchKit app launch process is close to completion and the WatchKit extension is about to run This is a useful location in which to perform initialization tasks that relate to the entire WatchKit app as opposed to an individual interface controller applicationDidBecomeActive Called to notify the WatchKit app that it is now active and visible to the user This method may be used to resume any tasks that were paused when the app previously transitioned to the inactive state applicationDidBecomeInactive Called when the WatchKit app transitions from active

    Original URL path: http://www.techotopia.com/index.php/An_Overview_of_the_watchOS_2_App_Architecture (2016-02-13)
    Open archived version from archive

  • An Example Interactive watchOS 2 App - Techotopia
    name of the containing iOS app Figure 5 4 for example highlights the interface controller source file for the main scene of the TipCalcApp extension Figure 5 4 Locate and select this file so that it loads into the editing panel Once loaded the code should read as outlined in the following listing import WatchKit import Foundation class InterfaceController WKInterfaceController override func awakeWithContext context AnyObject super awakeWithContext context Configure interface objects here override func willActivate This method is called when watch view controller is about to be visible to user super willActivate override func didDeactivate This method is called when watch view controller is no longer visible super didDeactivate Xcode has created an interface controller class implementation that overrides a subset of the lifecycle methods outlined in the chapter entitled An Overview of WatchKit App Architecture Later in this chapter some initialization code will be added to the willActivate method At this point some outlets need to be configured so that changes can be made to the Label objects in the main WatchKit app scene Establishing Outlet Connections Outlets provide the interface controller class with access to the interface objects within the corresponding storyboard scene Outlets can be created visually within Xcode by using Interface Builder and the Assistant Editor panel To establish outlets begin by loading the Interface storyboard file into the Interface Builder tool Within Interface Builder click on the scene so that it is highlighted before displaying the Assistant Editor by selecting the View Assistant Editor Show Assistant Editor menu option Alternatively it may also be displayed by selecting the center button the one containing an image of interlocking circles of the row of Editor toolbar buttons in the top right hand corner of the main Xcode window as illustrated in the following figure Figure 5 5 In the event that multiple Assistant Editor panels are required additional tiles may be added using the View Assistant Editor Add Assistant Editor menu option By default the editor panel will appear to the right of the main editing panel in the Xcode window For example in Figure 5 6 the panel to the immediate right of the Interface Builder panel is the Assistant Editor marked A Figure 5 6 By default the Assistant Editor will be in Automatic mode whereby it automatically attempts to display the correct source file based on the currently selected item in Interface Builder If the correct file is not displayed use the toolbar along the top of the editor panel to select the correct file The small instance of the Assistant Editor icon in this toolbar can be used to switch to Manual mode allowing the file to be selected from a pull right menu containing all the source files in the project Figure 5 7 Make sure that the InterfaceController swift file is displayed in the Assistant Editor and establish an outlet for the top most label by Ctrl clicking on the Label object in the scene and dragging the resulting line to the area immediately beneath the class InterfaceController declaration line in the Assistant Editor panel as shown in Figure 5 8 Figure 5 8 Upon releasing the line the configuration panel illustrated in Figure 5 9 will appear requesting details about the outlet to be defined Figure 5 9 Since this is an outlet the Connection menu should be set to Outlet The type and storage values are also correct for this type of outlet The only task that remains is to enter a name for the outlet so in the Name field enter amountLabel before clicking on the Connect button eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters Repeat the above steps to establish an outlet for the second Label object named tipLabel Once the connections have been established review the InterfaceController swift file and note that the outlet properties have been declared for us by the Assistant Editor import WatchKit import Foundation class InterfaceController WKInterfaceController IBOutlet weak var amountLabel WKInterfaceLabel IBOutlet weak var tipLabel WKInterfaceLabel When we reference these outlet variables within the interface controller code we are essentially accessing the objects in the user interface Establishing Action Connections Now that the outlets have been created the next step is to connect the Slider and Button objects to action methods within the interface controller class When the user increments or decrements the slider value the interface controller will need to change the value displayed on the amountLabel object to reflect the new value This means that an action method will need to be implemented within the interface controller class and connected via an action to the Slider object in the storyboard scene With the scene displayed in Interface Builder and the InterfaceController swift file loaded into the Assistant Editor panel Ctrl click on the Slider object in the storyboard scene and drag the resulting line to a position immediately beneath the tipLabel outlet as illustrated in Figure 5 10 Figure 5 10 Once the line has been released the connection configuration dialog will appear Figure 5 11 Within the dialog select Action from the Connection menu and enter sliderChange as the name of the action method to be called when the value of the slider is changed by the user Figure 5 11 Click on the Connect button to establish the action and note that Xcode has added a stub action method at the designated location within the InterfaceController swift file import WatchKit import Foundation class InterfaceController WKInterfaceController IBOutlet weak var amountLabel WKInterfaceLabel IBOutlet weak var tipLabel WKInterfaceLabel IBAction func sliderChange value Float An action method will also need to be called when the user taps the Button object in the user interface Ctrl click on the Button object in the scene and drag the resulting line to a position beneath the sliderChange method On releasing the line the

    Original URL path: http://www.techotopia.com/index.php/An_Example_Interactive_watchOS_2_App (2016-02-13)
    Open archived version from archive

  • An Overview of WatchKit Tables in watchOS 2 - Techotopia
    6 3 The row controller within the table is a template for the row controllers that will be created within the interface controller To design the row simply drag and drop user interface objects from the Object Library onto the row and resize the items to achieve the desired layout In Figure 6 4 for example the template row controller contains an image and label Figure 6 4 It is important to be aware that the appearance of the visual elements in a row is generally defined at runtime by the interface controller using the outlets declared in the row controller class Any attributes set within the storyboard will serve as the default attributes for the visual elements and will appear in the app at runtime unless overridden in the interface controller The row controller template must also be assigned an identifier which will be referenced in the interface controller code when instances of that row type are created during initialization The identifier is configured by selecting the row controller item in the Document Outline panel and entering a suitable identifier name into the Identifier field of the Attributes Inspector panel Figure 6 5 Adding the Row Controller Class to the Extension As previously outlined each row controller type must have a corresponding row controller class file within the app extension This must be a subclass of NSObject and can be created using the following steps 1 Locate the WatchKit Extension folder within the Project Navigator panel and Ctrl click on it 2 From the resulting menu select the New File menu option 3 In the new file template panel select Source listed under watchOS in the left hand panel and WatchKit Class from the main panel before clicking Next 4 Enter a name for the class into the Class field and select NSObject from the Subclass of menu before clicking Next 5 Click Finish to add the new class to the extension Associating a Row Controller with a Row Controller Class Before the table can be displayed the row controller in the scene needs to be associated with the corresponding row controller class residing within the extension This is achieved by selecting the row controller in the Document Outline panel displaying the Identity Inspector View Utilities Show Identity Inspector and selecting the row controller class name from the Class menu eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters Creating Table Rows at Runtime The interface controller class for the scene containing the table is responsible for configuring the table and creating the rows during the initialization phase of the WatchKit app launch process The interface controller will need an outlet connected to the table instance in the scene on which it will call either the setNumberOfRows withRowType or setRowTypes methods setNumberOfRows Used when all of the rows to be

    Original URL path: http://www.techotopia.com/index.php/An_Overview_of_WatchKit_Tables_in_watchOS_2 (2016-02-13)
    Open archived version from archive

  • A watchOS 2 WatchKit Table Tutorial - Techotopia
    the image object in the storyboard scene and drag the resulting line to a location beneath the class declaration line in the Assistant Editor panel Release the line and establish an outlet connection named myImage Repeat the above step to establish an outlet from the Label object named myLabel On completion of these steps the MyRowController swift file should read as follows import WatchKit class MyRowController NSObject IBOutlet weak var myImage WKInterfaceImage IBOutlet weak var myLabel WKInterfaceLabel With these steps completed the next step is to create some data to be displayed and to add initialization code to the interface controller to display the rows at runtime Connecting the Table Outlet In the course of initializing the table it will be necessary to access the table instance from within the interface controller class which will in turn require the establishment of an outlet With the Interface storyboard file selected and the Document Outline panel displayed open the Assistant Editor panel and make sure that it is displaying the content of the InterfaceController swift file Ctrl click on the Table entry in the Document Outline panel and drag the resulting line to a position immediately beneath the class declaration line in the Assistant Editor panel Figure 7 8 Release the line and establish an outlet named myTable in the resulting connection dialog eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters Creating the Data Clearly the rows within the table consist of an image and a label The next step is to add some data to the project to serve as content for these two objects This will take the form of two arrays declared within the interface controller class consisting of the names of the image files to be displayed on the Image object and strings to display on the Label object Select the InterfaceController swift file and edit it to add these two arrays as follows import WatchKit import Foundation class InterfaceController WKInterfaceController IBOutlet weak var myTable WKInterfaceTable let stringData Warm up Cardio Weightlifting Core Bike Cooldown let imageData walking treadmill weights core bikeriding cooldown Next a method needs to be added to the class to initialize the table with the content of the data arrays Remaining within the InterfaceController swift file implement this method as follows func loadTable myTable setNumberOfRows stringData count withRowType MyRowController for index labelText in stringData enumerate let row myTable rowControllerAtIndex index as MyRowController row myLabel setText labelText row myImage setImage UIImage named imageData index The method identifies the number of elements in the string array and uses that value to set the number of rows within the table while specifying the row type as MyRowController The code then iterates through each row in the table setting image and label properties using the two arrays as the data sources for the content to be displayed The

    Original URL path: http://www.techotopia.com/index.php/A_watchOS_2_WatchKit_Table_Tutorial (2016-02-13)
    Open archived version from archive

  • Implementing watchOS 2 WatchKit Table Navigation - Techotopia
    the storyboard should resemble Figure 8 2 Figure 8 2 With the new scene added to the storyboard and selected so it is highlighted in blue display the Attributes Inspector and enter DetailInterfaceController into the Identifier field By default labels are configured to display a single line of text which will be clipped if the text exceeds the width of the watch display To avoid this problem configure the label to wrap the text over multiple lines by selecting the label in the second scene displaying the Attributes Inspector and increasing the Lines attribute to 5 Figure 8 3 The design of the second scene is now complete The next step is to add an interface controller to go with it Adding the Detail Interface Controller Add the interface controller for the detail scene by locating the TableDemoApp WatchKit Extension entry in the Xcode Project Navigator panel Ctrl clicking on it and selecting New File from the menu In the template panel select the options to create a watchOS WatchKit Class source file and click Next Name the new class DetailInterfaceController and configure it to be a subclass of WKInterfaceController Click Next make sure that the file is to be generated into the extension folder then click Create Load the Interface storyboard file into Interface Builder Select the second scene so that it highlights in blue display the Identity Inspector panel and change the Class menu setting to the new DetailInterfaceController class as illustrated in Figure 8 4 Figure 8 4 Select the Label object in the detail scene in the storyboard and display the Assistant Editor panel Make sure that the Assistant Editor is displaying the DetailInterfaceController swift file and then control click and drag from the Label object to a position just beneath the class declaration line in the Assistant Editor panel Release the line and configure an outlet for the label named detailLabel The outlet should now be declared within the class file as follows import WatchKit import Foundation class DetailInterfaceController WKInterfaceController IBOutlet weak var detailLabel WKInterfaceLabel override func awakeWithContext context AnyObject super awakeWithContext context Configure interface objects here eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters Adding the Detail Data Array When the user selects a row in the table the detail scene will appear on the display The label within this scene is intended to provide a more detailed explanation of the exercise to be performed The text displayed will depend on which row was selected by the user In other words when the user selects the Warm up row the detail scene will provide details on how the user should perform the warm up routine For the purposes of this example the array containing the detailed descriptions will be placed in the interface controller of the first scene The appropriate text will then be

    Original URL path: http://www.techotopia.com/index.php/Implementing_watchOS_2_WatchKit_Table_Navigation (2016-02-13)
    Open archived version from archive

  • WatchOS 2 Page-based User Interfaces and Modal Interface Controllers - Techotopia
    eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters Managing Pages at Runtime The interface controllers and ordering sequence for a paging interface may also be specified from within the application code via a call to the reloadRootControllersWithNames WKInterfaceController class method passing through an array containing the identifiers of the interface controllers to be included in the page navigation together with a second array containing context data to be passed to each controller during scene transitions for example WKInterfaceController reloadRootControllersWithNames controllerOne controllerTwo contexts contextObj1 contextObj2 It is also possible to make an interface controller the currently displayed controller within the page sequence via a call to the becomeCurrentPage method within an initialization method of the interface controller to be displayed Modal Presentation of Interface Controllers Interface controller scenes can be displayed modally using either storyboard segues or programmatically from within a WatchKit app extension Modal interface controllers are typically used to display information to the user and result in the scene associated with the controller appearing to the user together with an option located in the top left hand corner to dismiss the scene and return to the previous controller Modal Presentation in Code Modal interface controllers can be displayed individually or as a paging group A single interface controller can be presented in code via a call to the presentControllerWithName method passing through as parameters the identifier name of the interface controller to be displayed and an optional object containing context data which will be passed to the modal interface controller via the awakeWithContext lifecycle method For example the following code fragment modally presents the interface controller with the identifier matching controllerTwo presentControllerWithName controllerTwo context contextObj A set of interface controllers organized using page based navigation may be presented modally from within code using the presentControllerWithNames method passing through arrays containing the controller identifiers and corresponding context objects presentControllerWithNames controllerOne controllerTwo contexts contextObj1 contextObj2 By default the dismissal option displayed in the modal scene will be labelled Cancel This can be changed by setting the title property of the interface controller to the desired text Modal Presentation using Storyboard Segues An alternative to writing code to present an interface controller modally involves the use of storyboard segues All that is required to implement a modal segue is to Ctrl click on the user interface object within the scene that is to trigger the modal transition and then drag the line to the scene that is to be presented If a modal segue can be established the destination scene will highlight as shown in Figure 9 5 If the scene does not highlight a segue cannot be established It is not possible for example to establish a segue from a Label object because labels unlike Button objects do not trigger an event when tapped by the user Figure 9 5 Upon

    Original URL path: http://www.techotopia.com/index.php/WatchOS_2_Page-based_User_Interfaces_and_Modal_Interface_Controllers (2016-02-13)
    Open archived version from archive

  • A watchOS 2 WatchKit Page-based Interface Tutorial - Techotopia
    and dropping an Interface Controller object from the Object Library panel so that it is positioned beneath the three existing scenes in the storyboard Figure 10 9 Drag and drop a Timer object from the Object Library onto the newly added scene With the Timer object selected display the Attributes Inspector panel and set the Horizontal and Vertical alignment attributes to Center Click on the T button within the Font text field select System from the popup panel Bold from the Style menu and set the Size setting to 28 as shown in Figure 10 10 Figure 10 10 On completion of these steps the scene should match that illustrated in Figure 10 11 Figure 10 11 eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters By default the Timer object displays hours minutes and seconds as numbers referred to as positional format A range of other configuration alternatives is available within the Attributes Inspector for changing both what is displayed on the timer and how it is displayed It is for example possible to also display month week day and year values It is also possible to change the way in which this information is displayed including fully spelling out the time and date in words instead of numbers Although this example will use the default setting it is worth reviewing the options in the Attributes Inspector panel for future reference Locate and select the PageDemoApp WatchKit Extension entry in the Xcode Project Navigator panel Ctrl click on it and select the New File menu option Create a new watchOS WatchKit Class file named TimerInterfaceController subclassed from WKInterfaceController and proceed with the steps to generate the class source file into the PageDemoApp WatchKit Extension project folder Return to the Interface storyboard file select the timer scene so that it is highlighted in blue and using the Identity Inspector panel change the Class menu to TimerInterfaceController Switch to the Attributes Inspector panel and enter TimerInterfaceController into the Identifier field Select the Timer object in the timer scene display the Assistant Editor panel and verify that it is displaying the source code for the TimerInterfaceController swift file Ctrl click on the Timer object and drag the resulting line to a position immediately after the class declaration line in the Assistant Editor panel Release the line and establish an outlet named workoutTimer On completion of these steps the start of the TimerInterfaceController swift file should read as follows import WatchKit import Foundation class TimerInterfaceController WKInterfaceController IBOutlet weak var workoutTimer WKInterfaceTimer override func awakeWithContext context AnyObject super awakeWithContext context Configure interface objects here With the timer interface controller added and configured the next step is to implement the modal segues so that the interface controller is presented when the Start buttons are tapped Adding the Modal Segues When the buttons on the three paged

    Original URL path: http://www.techotopia.com/index.php/A_watchOS_2_WatchKit_Page-based_Interface_Tutorial (2016-02-13)
    Open archived version from archive



  •