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".
  • Handling User Input in a watchOS 2 WatchKit App - Techotopia
    user to generate input from the phrase selection list and dictation only The emoji button is absent from the text input controller screen in this mode WKTextInputMode AllowEmoji Allows the user to generate input using the phrase list dictation and non animated emoji images WKTextInputMode AllowAnimatedEmoji Allows the user to generate input using the phrase list dictation and both animated and non animated emoji images The text input controller screen will automatically dismiss when the user has made an input selection or tapped the Cancel button The controller may also be dismissed from within the code of the interface controller via a call to the dismissTextInputController method When the text input controller is dismissed in this way the code within the completion handler block is not executed When testing text input it is important to note that emoji and dictation modes are not available from within the simulator environment Detecting if Input is a String or NSData Object If the input is in the form of text or a non animated emoji the input will be returned in the form of a Unicode string contained within a String object If the user selects an animated emoji however the image will be returned in the form of an NSData object The following code can be used to identify whether the returned result is a String or NSData 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 if let choice result if choice 0 is String Result is a String object Handle as a text string if choice 0 is NSData Result is an NSData object Handle as an image Direct Dictation Input In many situations input will be needed only through the use of dictation To move directly to dictation based input call the presentTextInputControllerWithSuggestions method without any suggested phrases and with the input mode set to Plain For example presentTextInputControllerWithSuggestions nil allowedInputMode Plain completion result Void in Creating the User Input Example Project Start Xcode and create a new iOS project On the template screen choose the Application option located under watchOS in the left hand panel and select iOS App with WatchKit App Click Next set the product name to TextInputApp enter your organization identifier and make sure that the Devices menu is set to Universal Before clicking Next change the Language menu to Swift and verify that all of the Include options are switched off On the final screen choose a location in which to store the project files and click on Create to proceed to the main Xcode project window Designing the WatchKit App Main Scene Locate and select the Interface storyboard file so that it loads into Interface Builder Drag and drop a Button object from the Object Library panel double click on it and change the text so that it reads Get Input Add a

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


  • An Introduction to Watch Connectivity in watchOS 2 - Techotopia
    face complications it is also possible to verify whether the complication associated with the WatchKit app is installed in the user s clock face by checking the complicationEnabled property if session complicationEnabled true Complication is enabled The topic of complications will be covered in greater detail beginning with the chapter entitled An Overview of watchOS 2 ClockKit and Apple Watch Complications 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 watchDirectoryURL Property For as long as the WatchKit app is installed on the Apple Watch and the watchAppInstalled session property returns a true value the watchDirectoryURL session property will contain the path to the watch directory This is a container on the watch into which can be stored any data needed by the current installation instance of the WatchKit app such as preferences or files waiting to be transferred The directory will remain in place until the user removes the WatchKit app from the Apple Watch device at which point the directory and all its content will be deleted from the device Sending and Receiving Application Context Data Once the Watch Connectivity session has been established by both the iOS and WatchKit apps application context data may be packaged into a dictionary object and transferred via a call to the updateApplicationContext method of the default session as demonstrated in the following code fragment do let session WCSession defaultSession let context FlightTime Delayed try session updateApplicationContext context catch let error as NSError print Error error description The receiving app will be notified of the arrival of a context update via a call to the session didReceiveApplicationContext method of the object designated as the delegate during the session initialization process If the destination app is already running the method will be called as soon as the transfer is complete In the event that the app is not running when the context data is transferred the method will be called next time the app is launched by the user Passed through as a parameter to this method is the dictionary object containing the context data func session session WCSession didReceiveApplicationContext applicationContext String AnyObject print Received context print applicationContext FlightTime Sending and Receiving User Information Data User information data is transferred using the transferUserInfo method of the default session instance as follows let session WCSession defaultSession let userInfo NewMessage msgTitle session transferUserInfo userInfo The receiving app will be notified of the arrival of user info data via a call to the session didReceiveUserInfo method of the object designated as the delegate during the session initialization process If the destination app is already running the method will be called as soon as the transfer is complete In the event that the app is not running when the context data is transferred the method will be called next time the app is launched by the user

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

  • A watchOS 2 WatchConnectivity Messaging Tutorial - Techotopia
    play func adjustVolume level Float audioPlayer volume level Each of these methods will need to be called by the corresponding action methods IBAction func playAudio sender AnyObject startPlay IBAction func stopAudio sender AnyObject stopPlay IBAction func sliderMoved sender AnyObject adjustVolume volumeControl value Compile and run the iOS app and test that the user interface controls allow playback to be started and stopped via the two buttons and that the slider provides control over the volume level With the iOS app now functioning it is time to initialize the WatchConnectivity session for the iOS app 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 Initializing the iOS App Watch Connectivity Session The Watch Connectivity session needs to be initialized as early as possible in the app lifecycle This process involves checking that the Apple Watch is paired and that the corresponding WatchKit app is installed on the watch device For the purposes of this example the code to perform these tasks will be added to the didFinishLaunchingWithOptions method located in the AppDelegate swift file as follows making sure to import the WatchConnectivity framework and to declare the class as implementing the WCSessionDelegate protocol import UIKit import WatchConnectivity UIApplicationMain class AppDelegate UIResponder UIApplicationDelegate WCSessionDelegate var window UIWindow func application application UIApplication didFinishLaunchingWithOptions launchOptions NSObject AnyObject Bool if WCSession isSupported let session WCSession defaultSession session delegate self session activateSession if session paired true print Apple Watch is not paired if session watchAppInstalled true print WatchKit app is not installed else print WatchConnectivity is not supported on this device return true Designing the WatchKit App Scene Select the Interface storyboard file located under WatchConnectApp WatchKit App so that the storyboard loads into Interface Builder Drag and drop a Label two Buttons and a Slider from the Object Library onto the scene canvas so that the layout matches that shown in Figure 13 5 Figure 13 5 Select the Label object display the Attributes Inspector panel and set the Alignment property in the Label section of the panel to center the displayed text Within the Alignment section of the panel change the Horizontal menu to Center Double click on the uppermost of the two buttons and change the text to Play Repeat this step for the second button this time changing the text so that it reads Stop Select the Slider object and in the Attributes Inspector panel change both the Maximum and Steps properties to 10 On completion of the above steps the scene layout should resemble Figure 13 6 Figure 13 6 Display the Assistant Editor and verify that it is showing the contents of the InterfaceController swift file Using the Assistant Editor establish an outlet connection from the Label object in the user interface named statusLabel Next create action connections from the two buttons named startPlay and stopPlay respectively and an action connection

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

  • An Overview of watchOS 2 WatchKit Glances - Techotopia
    glance scene This build scheme appears as an option within the Xcode run target menu as shown in Figure 14 4 Figure 14 4 Adding a Glance to an Existing WatchKit App The inclusion of a glance scene to an existing WatchKit app is a multi step process that begins with the addition of a Glance Interface Controller scene to the WatchKit app storyboard file To add the scene select the Interface storyboard file so that it loads into the Interface Builder tool and drag and drop a Glance Interface Controller object from the Object Library panel onto the storyboard canvas Figure 14 5 Figure 14 5 Next add an interface controller to accompany the glance scene by Ctrl clicking on the WatchKit Extension entry in the Project Navigator panel selecting the New File menu option and creating a new watchOS WatchKit Class source file subclassed from the WKInterfaceController class Return to the Interface storyboard file and select the Glance Interface Controller scene so that it highlights in blue Display the Identity Inspector panel and select the newly added interface controller class from the Class drop down 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 The final step is to add a build scheme for the glance scene The easiest way to achieve this is to duplicate the existing WatchKit App scheme and modify it for the glance scene Begin this process by selecting the WatchKit App scheme in the run target menu as shown in Figure 14 6 Figure 14 6 Display the run target menu again this time selecting the Edit Scheme menu option Within the scheme editing panel click on the Duplicate Scheme button located in the lower left hand corner and in the name field located in the upper left hand corner replace the Copy of text with a Glance prefix Figure 14 7 With the new scheme created and appropriately named select the Run option on the left hand panel and change the Watch Interface option menu in the main panel from Main to Glance as shown in Figure 14 8 Figure 14 8 With these steps complete the glance is now added and fully integrated into the WatchKit app WatchKit Glance Scene Layout Templates The layout for a WatchKit glance must be based on one of a number of templates provided within the Xcode environment Glance scenes are divided into upper and lower sections Xcode currently provides 12 template options for the upper section of the scene and 24 template options for the lower section The templates vary in the amount of customization that can be performed in terms of adding other visual elements To change the template for either the upper or lower section of the scene select the scene in the storyboard and display the Attributes Inspector in the utilities panel

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

  • A watchOS 2 WatchKit Glance Tutorial - Techotopia
    layout of the glance scene should match that outlined in Figure 15 8 Figure 15 8 Establishing Outlet Connections The interface controller for the glance scene will need access to the two labels to change the text that is displayed to the user Keeping the Interface storyboard file loaded in the Interface Builder tool display the Assistant Editor panel and make sure that it is displaying the content of the GlanceController swift file Ctrl click on the upper label in the glance scene and drag the resulting line to a position immediately beneath the class declaration line in the Assistant Editor panel Release the line and create an outlet connection named titleLabel Repeat this step to connect the lower label to an outlet named mainLabel At this point the beginning of the GlanceController swift file should read as follows import WatchKit import Foundation class GlanceController WKInterfaceController IBOutlet weak var titleLabel WKInterfaceLabel IBOutlet weak var mainLabel WKInterfaceLabel override func awakeWithContext context AnyObject super awakeWithContext context Configure interface objects here Adding Data to the Glance Interface Controller Now that the user interface of the glance scene is designed and the Label objects are connected to outlets in the interface controller some data needs to be added to represent the information to be presented when the glance is displayed by the user The information contained within the glance will consist of the currently selected exercise and a reminder of the duration or number of repetitions to be performed Select the GlanceController swift file and add two data arrays to the class so that it reads as follows import WatchKit import Foundation class GlanceController WKInterfaceController IBOutlet weak var titleLabel WKInterfaceLabel IBOutlet weak var mainLabel WKInterfaceLabel let titleData Warm up Cardio Weightlifting Core Bike Cooldown let durationData 20 mins 30 mins 3 x 10 2 x 20 20 mins 20 mins override func awakeWithContext context AnyObject super awakeWithContext context Configure interface objects here Storing and Retrieving the Currently Selected Table Row When the glance is invoked by the user it will need to know which row within the corresponding WatchKit app was last selected by the user The WatchKit app interface controller therefore needs to store this information in a location where it can also be accessed by the glance interface controller Since this is a small amount of data the ideal location is within user defaults storage using the NSUserDefaults class The selection of a table row results in a call to the didSelectRowAtIndex method of the main WatchKit app interface controller which now needs to be updated to save the current index value Within the Project Navigator panel select the InterfaceController swift file locate the didSelectRowAtIndex method and modify it as follows override func table table WKInterfaceTable didSelectRowAtIndex rowIndex Int pushControllerWithName DetailInterfaceController context detailData rowIndex 1 let userDefaults NSUserDefaults standardUserDefaults userDefaults setObject rowIndex forKey index userDefaults synchronize Within the glance interface controller this stored index value needs to be retrieved and used to display the correct information within the glance scene Edit the

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

  • A watchOS 2 WatchKit Context Menu Tutorial - Techotopia
    Menu Item objects from the Object Library onto the Menu entry in the Document Outline panel Action method connections are established by displaying the Assistant Editor panel Ctrl clicking and dragging from a menu item to an appropriate position in the interface controller source file and following the usual steps to establish an action connection Select a menu item from the outline panel and display the Attributes Inspector panel Figure 16 5 to configure the appearance of the menu item Figure 16 5 The title attribute specifies the text that appears beneath the menu item image circle If the image menu is set to Custom an image meeting the Apple style guidelines will need to be added to the image assets catalog of the WatchKit App folder in the Project Navigator panel and then selected from the drop down menu located beneath the Image menu To select a pre defined image simply click on the Image menu and make a selection Figure 16 6 Adding and Removing Menu Items in Code If a context menu has been added to a WatchKit storyboard scene additional menu items may be added dynamically within the code of the interface controller using the following interface controller methods addMenuItemWithImage Takes as parameters a UIImage object containing the image to be displayed the title string to display beneath the image and a reference to the method to be called when the item is selected addMenuItemWithImageNamed This method is used when the image to be displayed is already included in the WatchKit app resources The method takes as parameters the name of the image file stored on the Apple Watch device the title string to be displayed beneath the image and a reference to the method to be called when the item is selected addMenuItemWithIcon Allows one of the pre designed template icons to be specified for the menu item image In addition to the icon reference this method also requires the title string to be displayed beneath the image and a reference to the method to be called when the item is selected When calling the addMenuItemWithIcon method the icon options are defined within the WKMenuItemIcon enumeration and may be referenced as follows WKMenuItemIcon Accept WKMenuItemIcon Add WKMenuItemIcon Block WKMenuItemIcon Decline WKMenuItemIcon Info WKMenuItemIcon Maybe WKMenuItemIcon More WKMenuItemIcon Mute WKMenuItemIcon Pause WKMenuItemIcon Play WKMenuItemIcon Repeat WKMenuItemIcon Resume WKMenuItemIcon Share WKMenuItemIcon Shuffle WKMenuItemIcon Speaker WKMenuItemIcon Trash The following code for example adds a menu item to a context menu using the Pause template icon configured to call an action method named pauseSelected addMenuItemWithItemIcon WKMenuItemIcon Pause title Pause action pauseSelected All of the menu items added from within the interface controller code may be removed via a call to the clearAllMenuItems method of the interface controller object Any menu items added to the storyboard scene using Interface Builder are unaffected by this method call eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or Print 27 99 format watchOS

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

  • Working with Images in WatchKit and watchOS 2 - Techotopia
    will be listed containing the 2x image If this were an image set for an iOS app it would also be recommended to add 1x for non retina screens and 3x for iPhone 6 Plus and iPad devices images to the image set Since the Apple Watch has retina only displays of similar sizes only the 2x image is required Figure 17 5 When referencing an image stored in an asset catalog the 2x is dropped from the filename With the image file named email protected contained in an image asset catalog for a WatchKit app target the code to display this image on a WKInterfaceImage object would read as follows myImage setImageNamed spaceship Compressing Large Images Depending on the image size it can take a considerable amount of time to transfer an image from the iPhone to the Apple Watch Consider a scenario in which the extension for a WatchKit app needs to display a photo that the user has taken using the iPhone camera Transferring such an image could take over two minutes to transfer wirelessly from the iPhone to the watch This is clearly an unacceptable amount of time to keep the user waiting 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 In reality the image that is taken by the iPhone camera is orders of magnitude larger than is necessary to fit on the display of an Apple Watch In fact an image from the iPhone camera roll is measured in thousands of pixels while the display of even the largest Apple Watch model has a resolution of only 312 x 390 pixels Clearly there is significant opportunity for reducing the size of any large images before they are transferred to the Apple Watch for display There are a number of options for reducing the size of any images both in terms of dimensions and storage space before the transfer to the watch takes place The following code for example is useful for reducing the size of an image by a specified scale factor if let largeImage UIImage named myLargeImage let imageSize largeImage size let scale CGFloat 0 15 Scale factor let reducedSize CGSizeMake imageSize width scale imageSize height scale UIGraphicsBeginImageContext reducedSize largeImage drawInRect CGRectMake 0 0 reducedSize width reducedSize height let compressedImage UIGraphicsGetImageFromCurrentImageContext UIGraphicsEndImageContext myImage setImage compressedImage Display the image In tests on an original PNG image file of 2448 x 3264 pixels with a size of 23MB the transfer time was reduced from approximately 3 minutes down to just 6 seconds after the image was compressed using the above code Specifying the WKInterfaceImage Object Dimensions in Code When a WKInterfaceImage interface object is added to a storyboard scene using Interface Builder the dimensions of the object can be specified from within the Attributes Inspector panel Figure 17 6 Options are available to allow the

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

  • A watchOS 2 WatchKit Animated Image Tutorial - Techotopia
    layout matches Figure 18 1 Figure 18 1 Display the Assistant Editor panel and establish an outlet connection from the Image object in the scene named imageObject Adding the Animation Sequence Images The animation sequence in this example consists of 40 PNG image files which combine to make up an animation of the planet Earth rotating These files can be found in the animation images folder of the sample code archive available for download from the following link http www ebookfrenzy com code watchOS2BookSamples zip Locate and select the Assets xcassets entry listed under the AnimationApp WatchKit App folder within the Xcode Project Navigator panel Ctrl click in the left hand panel of the asset catalog and select the Import option from the resulting menu Within the file selection dialog navigate to and select the animation images folder and click on Open Once the images have been imported into the asset catalog a new image set will be created named animation images containing all of the animation sequence images Figure 18 2 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 With the images added to the asset catalog all that remains is to add some code in the interface controller class to create and initiate the animation Creating and Starting the Animated Image Locate and edit the InterfaceController swift file and modify the awakeWithContext method to create and display the animation image override func awakeWithContext context AnyObject super awakeWithContext context imageObject setImageNamed earth Compile and run the WatchKit app and note that image 0 appears but that the image object does not animate through the remaining frames Modify the code

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



  •