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 WatchKit App - Techotopia
    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 if result 0 is String Result is a String object Handle as a text string if result 0 is NSData Result is an NSData object Handle as an image eBookFrenzy com Purchase the fully updated watchOS 2 Swift 2 edition of this 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 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 iOS in the left hand panel and select Single View Application 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 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 Adding the WatchKit App Target The next step is to add the WatchKit app target to the project Within Xcode select the File New Target menu option In the target template dialog select the Apple Watch option listed beneath the iOS heading In the main panel select the WatchKit App icon and click on Next On the subsequent screen turn off the Include Glance Scene and Include Notification Scene options before clicking on the Finish button As soon as the extension target has

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


  • Sharing Data Between a WatchKit App and the Containing iOS App - Techotopia
    and must match in both the parent app and extension entitlement files While it is possible to manually create the necessary entitlement files by far the easiest way to configure app group membership is to do so through the Xcode Capabilities panel To configure app group support for the iOS app select the target located at the top of the Project Navigator panel and click on the Capabilities tab in the main panel Within the capabilities panel locate the App Groups section and switch it to the On position Figure 14 2 When app groups have been enabled in the Capabilities screen any existing app groups associated with your Apple developer account will be listed To make the current app a member of any of those groups simply enable the checkbox next to the group name Figure 14 3 To add a new app group to your account simply click on the button and enter the new app group name Add the current app to the newly added app group by enabling the checkbox next to the group name The app group will subsequently appear as an option in all other project targets within the Xcode Capabilities panel With the iOS app added to the app group the WatchKit app extension must also be added as a member of the same group in order to gain access to the shared container By default the Capabilities panel displays the settings for the iOS app target To access the capability settings for the WatchKit Extension use the menu located in the top left hand corner of the Capabilities panel as indicated in Figure 14 4 Figure 14 4 When clicked this menu will present a list of targets contained within the current project as shown in Figure 14 5 one of which will be the WatchKit extension Select this option and repeat the steps followed for the iOS app to enable and configure app group support making sure to select the same group name as that chosen for the iOS app Figure 14 5 A review of the files in the Project Navigator panel will reveal that entitlement files have been added for each of the two targets the contents of which will read as follows allowing for differences in the app group name xml version 1 0 encoding UTF 8 DOCTYPE plist PUBLIC Apple DTD PLIST 1 0 EN http www apple com DTDs PropertyList 1 0 dtd plist version 1 0 dict key com apple security application groups key array string group com payloadmedia SharingData string array dict plist With both the parent iOS app and the WatchKit extension added to the same app group code can now be written to access the shared data container eBookFrenzy com Purchase the fully updated watchOS 2 Swift 2 edition of this 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 App Group File Sharing The first

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

  • WatchKit Extension and iOS App File and Data Sharing - A Tutorial - Techotopia
    shared container and if so read the content of the file into a data buffer before displaying it to the user via the Text View override func viewDidLoad super viewDidLoad let sharedContainer fileManager containerURLForSecurityApplicationGroupIdentifier YOUR APP GROUP IDENTIFIER HERE let dirPath sharedContainer path sharedFilePath dirPath stringByAppendingPathComponent sharedtext doc if fileManager fileExistsAtPath sharedFilePath let databuffer fileManager contentsAtPath sharedFilePath textView text NSString data databuffer encoding NSUTF8StringEncoding as String Finally the method needs to check for the presence of a user default value for the key switch and in the event that a default exists set the state of the Switch view accordingly override func viewDidLoad super viewDidLoad let sharedContainer fileManager containerURLForSecurityApplicationGroupIdentifier YOUR APP GROUP IDENTIFIER HERE let dirPath sharedContainer path sharedFilePath dirPath stringByAppendingPathComponent sharedtext doc if fileManager fileExistsAtPath sharedFilePath let databuffer fileManager contentsAtPath sharedFilePath textView text NSString data databuffer encoding NSUTF8StringEncoding sharedDefaults NSUserDefaults suiteName YOUR APP GROUP IDENTIFIER HERE let switchSetting sharedDefaults boolForKey switch if let setting switchSetting mySwitch on setting eBookFrenzy com Purchase the fully updated watchOS 2 Swift 2 edition of this 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 Saving the Data The next step in implementing the iOS app is to write the code for the saveData method Locate this method in the ViewController swift file and modify it to read as follows IBAction func saveData sender AnyObject let databuffer textView text as NSString dataUsingEncoding NSUTF8StringEncoding if fileManager fileExistsAtPath sharedFilePath databuffer writeToFile sharedFilePath atomically true else fileManager createFileAtPath sharedFilePath contents databuffer attributes nil sharedDefaults setBool mySwitch on forKey switch The method begins by encoding the text entered into the Text View into an NSData object and writing that data to the shared file Next the switch setting is saved to the shared defaults storage as a Boolean value Compile and run the application Once running enter some text into the Text View and change the setting of the Switch view Stop the app from the Xcode toolbar and then re launch it On restarting both the text and switch mode should have been preserved With the iOS app functioning as expected the next step is to add and implement the WatchKit app Adding the WatchKit App Target Within Xcode select the File New Target menu option In the target template dialog select the Apple Watch option listed beneath the iOS heading In the main panel select the WatchKit App icon and click on Next On the subsequent screen turn off the Include Glance Scene and Include Notification Scene options before clicking on the Finish button As soon as the extension target has been created a new panel will appear requesting permission to activate the new scheme for the extension target Activate this scheme now by clicking on the Activate button in the request panel Adding the WatchKit App to the App Group With the iOS app added to the app group the WatchKit extension must also be added as a member

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

  • Configuring Preferences with the WatchKit Settings Bundle - Techotopia
    heading Child Pane Allows the user to navigate to additional pages of preference settings The controls are added to the Root plist file and configured using the Xcode Property List Editor When a control is added to the property list file it needs to be assigned an identifier key This is the key that will be used to extract and set the current preference value from within the code of the iOS app and WatchKit extension Figure 16 6 shows an example Root plist file loaded into the Xcode Property List Editor In this case the list consists of a single preference control item in the form of a toggle switch The toggle is configured to display a title which reads Use Large Font and to be enabled by default The item has also been assigned font preference as the identifier Figure 16 6 Figure 16 7 shows the above preference item as it appears within an Apple Watch app settings panel Figure 16 7 Accessing WatchKit Bundle Settings from Code Obviously preference settings are of little use if the WatchKit app does not identify the current settings and act upon them The WatchKit bundle settings reside within the containing iOS app but will need to be accessed from the WatchKit app extension This sharing can be achieved by creating an app group and configuring both the containing iOS app and WatchKit extension to be group members as outlined in the chapter entitled Sharing Data Between a WatchKit App and the Containing iOS App Once an app group has been created as outlined it needs to be added to the Root plist file of the settings bundle using the ApplicationGroupContainerIdentifier key Figure 16 8 for example shows an app group identifier assigned to this key Figure 16 8 eBookFrenzy com Purchase the fully updated watchOS 2 Swift 2 edition of this 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 Assuming a Boolean preference with an identifier of font preference and an app group identifier of group com ebookfrenzy MySampleApp the code to access the preference setting from within either the iOS app or the WatchKit extension would read as follows let defaults NSUserDefaults suiteName group com ebookfrenzy MySampleApp if let preference defaults boolForKey font preference if preference The preference setting is enabled else The preference setting is disabled Registering Default Preference Values Each item added to the Root plist file through the Property List Editor has associated with it an optional default value In Figure 16 8 for example the toggle switch was configured to have a default Boolean value of YES or true A common mistake made when working with user defaults is to assume that the defaults set in the properties list file are the values that will be stored in the settings bundle in the absence of the selection being made by the user In reality these settings only define

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

  • A WatchKit Settings Bundle Tutorial - Techotopia
    the menu and configure the title value to read Font and Color Figure 17 9 Click on the down arrow on the Item 1 line to fold the sub items and then Ctrl click on the item once again selecting the Add Row menu option but this time choosing Slider from the list of preference items types Unfold the new item and set the Identifier field to font preference the Minimum value to 10 the Maximum value to 30 and the Default Value to 15 Adding a Multi Value Control to the Settings Bundle The multi value control will provide the user with a choice of red blue or green for the color of the text on the label in the WatchKit app Add this item by folding the sub items of Item 2 Ctrl clicking on the item line and selecting the Add Row menu option From the list of item types choose the Multi Value option Edit the values for the new item so that the title reads Foreground Color and the identifier is set to color preference The multi value control requires a list of titles to display to the user together with a set of corresponding values Add the Titles entry by selecting the Default Value line and clicking on the button From the resulting menu select the Titles entry Figure 17 10 Unfold the new Titles entry so that the arrow is pointing down and click on the button to add the first title to the list which will appear with a key of Item 0 Enter White into the value field for this item before clicking on the button on the Item 0 line to add the next title Enter Green as the value for this key Repeat this step once more this time setting the title text to Blue On completion of these steps the multi value section of the properties file should match Figure 17 11 Figure 17 11 Each of the titles added to the multi value control now needs to have a corresponding value Select and fold the Titles section of the properties list so that the title sub items are hidden With the Titles line still selected click on the button and add a new item of type Values Select the Values line unfold it and click on the button to add a new value item to the array The type of a value is set to String by default This can be changed by clicking on the type field and selecting a different type from the resulting menu as shown in Figure 17 12 Figure 17 12 Using this technique change the value type for the new value from String to Number and set the value to 0 Repeat the above steps to add two more values configured as numbers set to 1 and 2 respectively Review the Titles and Values settings which should now resemble those configured in Figure 17 13 Figure 17 13 The last task in this phase of the tutorial is to configure the default value setting for the multi value control Within the multi value section of the list listed as Item 3 locate the Default Value key change the value type to Number and set the value to 0 Compile and run the iOS app target and then use the Apple Watch app to test that the preference items are configured correctly If the Apple Watch app launches directly into the SettingsBundle preferences page it may be necessary to use the Back button to return to the list of WatchKit apps and then reselect the SettingsBundle app in order to see the new configuration The first screen should match Figure 17 14 Figure 17 14 Selecting the Foreground Color control should display a second screen Figure 17 15 listing the full range of color choices available for selection by the user Figure 17 15 Setting Up the App Group The WatchKit settings bundle is packaged with the containing iOS app This means that an app group is required if the WatchKit extension is to be able to access the preferences contained within the settings bundle Begin by selecting the SettingsBundle target located at the top of the Project Navigator panel and clicking on the Capabilities tab in the main panel Within the capabilities panel locate the App Groups section and switch the setting to the On position To add a new app group to your account simply click on the button and enter the new app group name for example group com ebookfrenzy SettingsBundle Add the current app to the newly added app group by enabling the checkbox next to the group name With the iOS app added to the app group the WatchKit extension must also be added as a member of the same group in order to gain access to the shared container To access the capability settings for the WatchKit extension use the menu located in the top left hand corner of the capabilities panel as indicated in Figure 17 16 Figure 17 16 When clicked this menu will present a list of targets contained within the current project as shown in Figure 17 16 one of which will be the SettingsBundle WatchKit Extension Select this option and repeat the steps followed for the iOS app to enable membership in the same app group as that configured for the iOS app eBookFrenzy com Purchase the fully updated watchOS 2 Swift 2 edition of this 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 App Group to the Settings Bundle The final step in configuring the app group is to add the group container identifier to the settings bundle This is achieved by selecting the Root plist file from the settings bundle folder in the Project Navigator and entering the app group identifier into the value field of the ApplicationGroupContainerIdentifier key line

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

  • An Overview of WatchKit Glances - Techotopia
    scene This build scheme appears as an option within the Xcode run target menu as shown in Figure 18 3 and is named Glance app name WatchKit App Figure 18 3 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 18 4 Figure 18 4 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 Cocoa Touch 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 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 18 5 Figure 18 5 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 Image Duplicating an Xcode scheme Figure 18 6 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 18 7 Figure 18 7 eBookFrenzy com Purchase the fully updated watchOS 2 Swift 2 edition of this 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 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 The most flexible is the default template shown in Figure 18 8 This consists of a Group

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

  • A WatchKit Glance Tutorial - Techotopia
    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 Creating an App Group 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 app group user defaults as described in the chapter entitled Sharing Data Between a WatchKit App and the Containing iOS App Select the TableDemoApp target located at the top of the Project Navigator panel and click on the Capabilities tab in the main panel By default the Capabilities panel displays the settings for the iOS app target To access the capability settings for the WatchKit Extension use the menu located in the top left hand corner of the capabilities panel as indicated in Figure 19 7 Figure 19 7 eBookFrenzy com Purchase the fully updated watchOS 2 Swift 2 edition of this 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 When clicked this menu will present a list of targets contained within the current project one of which will be the TableDemoApp WatchKit Extension Select this option and scroll down the capabilities list until the App Groups section comes into view Switch App Group support On and select a suitable Apple developer account with which to associate the group when prompted to do so Click on the button within the App Group section of the capabilities panel and add a new app group based on the application name and your organization identifier for example group com ebookfrenzy TableDemoApp Once the new

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

  • A WatchKit Context Menu Tutorial - Techotopia
    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 20 5 to configure the appearance of the menu item Figure 20 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 20 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 fully updated watchOS 2 Swift 2 edition of this 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 Context Menu Example Project Start Xcode and create a new iOS project On the template screen choose the Application option located under iOS in the left hand

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



  •