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".
  • A watchOS 2 WatchKit Audio Recording and Playback Example - Techotopia
    button is displayed within the controller WKAudioRecorderControllerOptionsAutorecordKey A Boolean value controlling whether or not the recording starts automatically when the controller is presented to the user Using App Groups to Share Media File Access As discussed in the Sharing Data Between a WatchKit App and the WatchKit Extension chapter it is important when working with media files to be aware that those files need to be accessible to both the WatchKit app and the corresponding WatchKit extension When recording audio the WatchKit app is responsible for storing the recorded audio to file In terms of playing back the audio however the extension will need to be able to reference the saved audio file When recording or playing back audio therefore it is important that an app group shared container be used for the storage of the recorded audio The Audio Recording and Playback Tutorial The remainder of this chapter will work through the creation of a simple WatchKit app designed to record save and playback audio content using the Apple Watch as both the playback and recording device 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 RecordApp 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 switch off all of the Include options 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 Main Storyboard Scene Within the Project Navigator panel select the Interface storyboard file located under the RecordApp WatchKit App entry Within Interface Builder add two Button objects to the main scene and change the text on the buttons to read Play and Record respectively Figure 27 3 Select the Play button display the Attributes Inspector and switch off the enabled property The button will only be enabled after a recording has been saved Display the Assistant Editor and establish an outlet connection from the Play button named playButton With the Assistant Editor still displayed establish action connections from the Play and Record buttons to methods named playAudio and recordAudio respectively Creating and Joining the App Group An app group container will be used to share the media file containing the recorded audio between the WatchKit app and the WatchKit extension Begin by selecting the RecordApp target located at the top of the Project Navigator panel and in the Settings panel select the Capabilities tab and click on the target menu located in the top left hand corner of the panel highlighted in Figure 27 4 Figure 27 4 From the target menu Figure 27 5 select the RecordApp WatchKit App target Figure 27 5 eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or

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


  • An Overview of ClockKit and Apple Watch Complications - Techotopia
    that they can automatically adjust the text to make the best use of the available space within the complication The text providers supported in watchOS 2 can be summarized as follows CLKDateTextProvider Automatically formats date information so that it best fits into the available complication space CLKRelativeDateTextProvider Automatically formats text representing the difference in time between the current date and a specified date CLKSimpleTextProvider Allows a line of text to be specified in both long and abbreviated forms On smaller complications the abbreviated form will be used CLKTimeIntervalTextProvider Used to convey a range of time for example 9 00am 10 30am automatically configured to make the best use of the available space within a complication CLKTimeTextProvider Used to convey a single time value which is automatically configured to match the available space Image Provider Class Images are added to timeline objects using the CLKImageProvider class This class is passed image and optional tint values Images can be constructed from a foreground image or by passing through both background and foreground images which are superimposed to create a single image When the user customizes a clock face the option is provided to change the tint color of the face This tint setting is also applied to both the images and text contained within the complications visible in the clock face As such any images included in a complication must be provided as alpha template images This means that the image must contain only the alpha channel which controls the level of transparency of the image and no RGB red green and blue color channel information This allows ClockKit to adjust the tint of the image to match the user s color preferences When creating an image using both background and foreground images the background image is tinted while the foreground remains in black or white 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 a Timeline Entry Object Having explored the various elements that comprise a complication timeline entry object now is a good time to look at what the code to create such an object might look like The following code for example creates a new timeline object let template CLKComplicationTemplateModularLargeStandardBody let myImage UIImage named my image template headerImageProvider CLKImageProvider onePieceImage myImage template headerTextProvider CLKSimpleTextProvider text 11 30 Sales Meeting template body1TextProvider CLKSimpleTextProvider text Conference Rm 1 Building 2 let entry CLKComplicationTimelineEntry date NSDate complicationTemplate template The above code creates a new modular large standard body template object and creates an image object using an image asset name my image An image provider is then created using the image object and assigned to the header of the complication template Two simple text providers are then used to assign text to be displayed in the header and body of the complication Finally the timeline entry object is created using the template

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

  • A watchOS 2 Complication Tutorial - Techotopia
    and text content of the placeholder The reply handler is then called and used to return the template object to the ClockKit framework Configuring Travel Directions The ClockKit framework time travel feature needs to know the directions in which it will be able to traverse the timeline For the purposes of the example the timeline will only contain future events As such the getSupportedTimeTravelDirectionsForComplication method needs to be modified to support only forward direction time travel func getSupportedTimeTravelDirectionsForComplication complication CLKComplication withHandler handler CLKComplicationTimeTravelDirections Void handler Forward With this property configured for forward travel only the complication in the clock face will be dimmed out in the event the user attempts to travel into the past Adding a Timeline Entry Creation Method A number of the methods in the data source class will need to create complication timeline entries Rather than duplicate code in multiple methods it is more efficient to write a single utility method and call that whenever a timeline entry is needed This method will be named createTimeLineEntry and will take as parameters the header and body text to be included in the complication and the date of the timeline entry in the form of an NSDate object The code for the method will also incorporate the beer glass icon into the entry before returning the result in the form of a completed CLKComplicationTimelineEntry object Within the ComplicationController swift file implement this method as follows 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 func createTimeLineEntry headerText String bodyText String date NSDate CLKComplicationTimelineEntry let template CLKComplicationTemplateModularLargeStandardBody let beerGlass UIImage named beer glass template headerImageProvider CLKImageProvider onePieceImage beerGlass template headerTextProvider CLKSimpleTextProvider text headerText template body1TextProvider CLKSimpleTextProvider text bodyText let entry CLKComplicationTimelineEntry date date complicationTemplate template return entry The method takes the same steps as those outlined for the getPlaceholderTemplateForComplication method to create a complication template object and combines that object with the designated date to create a timeline entry object Specifying the Timeline Start and End Dates Among the information required by the ClockKit framework for a complication are the start and end dates of the timeline In this example the start date is the first timeline entry which will be displayed for the current time The getTimelineStartDateForComplication method therefore simply needs to return the current date and time func getTimelineStartDateForComplication complication CLKComplication withHandler handler NSDate Void let currentDate NSDate handler currentDate Since the timeline contains four entries spaced at hourly intervals the getTimelineEndDateForComplication method needs to calculate and return a time four hours into the future from the current time func getTimelineEndDateForComplication complication CLKComplication withHandler handler NSDate Void let currentDate NSDate let endDate currentDate dateByAddingTimeInterval NSTimeInterval 4 60 60 handler endDate Providing the Current Timeline Entry Once the user has added the complication into a clock face the ClockKit framework will request the timeline entry to be displayed for

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

  • Supporting Different Apple Watch Display Sizes in watchOS 2 - Techotopia
    the 42mm watch model When running on the smaller 38mm watch model however the 14pt customization font attribute will be used It is worth noting that it is not necessary to also add a customization attribute for the 42mm font setting since this is covered by the default value Working with Screen Sizes in Interface Builder By default the storyboard editor in Interface Builder displays storyboard scenes in Any Screen Size mode When in this mode interface objects are displayed using the default attributes To see how the scenes within a storyboard will appear on different screen sizes this mode can be changed using the selection at the bottom of the storyboard editor panel as shown in Figure 30 4 Figure 30 4 Clicking on the button displays a menu of screen size options which when selected change the mode of the storyboard canvas to match the chosen screen size When a specific screen size mode is selected in Interface Builder the scenes in the canvas will update to reflect the size of the screen In addition the interface objects within the scenes will change to reflect any customization attributes that have been set for the selected screen size This allows you to review the user interface layout as it will appear when running on the designated Apple Watch model A more significant point to note about changing the storyboard screen size mode is that any changes made to the scene layout will apply only for the current screen size Consider for example a scene containing a label If the storyboard editor is in 38mm mode when the text of the label is changed within the scene then that change only applies to the layout when the app is running on a 38mm device In other words visually manipulating the objects and layout when the storyboard editor is set for a specific screen size essentially configures screen size customization attributes which also appear within the Attributes Inspector panel In Figure 30 5 for example the text displayed on a label has been changed to read My 42mm Text from within the scene canvas while the storyboard editor is in 42mm mode Note that as a result of this change a matching 42mm screen size customization attribute for the text property is now listed in the Attributes Inspector panel Figure 30 5 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 Clearly screen size customization attributes can be configured both manually by adding custom attributes via the Attributes Inspector panel and visually from within the storyboard editor by changing the screen size mode and directly manipulating the interface objects When the storyboard editor is in any mode other than Any Screen Size the toolbar at the bottom of the canvas is displayed in blue This is intended as a subtle reminder that

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

  • A watchOS 2 WatchKit Map Tutorial - Techotopia
    call the requestWhenInUseAuthorization method within the awakeWithContext method import WatchKit import Foundation import CoreLocation class InterfaceController WKInterfaceController IBOutlet var mapObject WKInterfaceMap var locationManager CLLocationManager CLLocationManager override func awakeWithContext context AnyObject super awakeWithContext context locationManager requestWhenInUseAuthorization The requestWhenInUseAuthorization method call requires that a specific key value pair be added to the Information Property List dictionary contained within the application s Info plist file The value takes the form of a string describing the reason why the application needs access to the user s current location In the case of background access to location information the NSLocationWhenInUseUsageDescription key must be added to the property list Within the Project Navigator panel load the Info plist file the one located under the MapDemoApp target not either of the WatchKit target Info plist files into the editor The key value pair needs to be added to the Information Property List dictionary Select this entry in the list and click on the button to add a new entry to the dictionary Within the new entry enter NSLocationWhenInUseUsageDescription into the key column and once the key has been added double click in the corresponding value column and enter the following text This information is required to identify your current location Once the entry has been added to the Info plist file it should appear as illustrated in Figure 31 2 Figure 31 2 The WatchKit app must now be run and the location tracking request approved Select MapDemoApp WatchKit App in the Xcode run target menu and launch the app on a device or simulator session The first time that the app is launched the system will display a message on the Apple Watch indicating that permission must be given on the iPhone device to track location information Figure 31 3 Using the paired iPhone tap the Allow button on the request panel to enable this access Figure 31 3 Getting the Current Location The next step is to instruct the location manager to request the user s current location This involves declaring the InterfaceController class as implementing the CLLocationManagerDelegate protocol assigning the class as the delegate for the location manager and then the location update Note that the code also declares a variable in which to store the current map location import WatchKit import Foundation import CoreLocation class InterfaceController WKInterfaceController CLLocationManagerDelegate IBOutlet var mapObject WKInterfaceMap var locationManager CLLocationManager CLLocationManager var mapLocation CLLocationCoordinate2D override func awakeWithContext context AnyObject super awakeWithContext context locationManager requestWhenInUseAuthorization locationManager desiredAccuracy kCLLocationAccuracyBest locationManager delegate self locationManager requestLocation When the location manager receives a location update it will call the didUpdateLocations method on the delegate This class now needs to be implemented in the InterfaceController swift file along with the didFailWithError delegate method as follows func locationManager manager CLLocationManager didUpdateLocations locations CLLocation let currentLocation locations 0 let lat currentLocation coordinate latitude let long currentLocation coordinate longitude self mapLocation CLLocationCoordinate2DMake lat long let span MKCoordinateSpanMake 0 1 0 1 let region MKCoordinateRegionMake self mapLocation span self mapObject setRegion region self mapObject addAnnotation self mapLocation withPinColor

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

  • An Overview of Notifications in watchOS 2 - Techotopia
    Apple Watch is selected by the user the handler method that gets called will depend on whether the notification was remote or local in origin and whether the notification action was configured for background or foreground mode 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 When a remote notification appears on the Apple Watch and the user selects a background notification action the following method is called on the application delegate of the containing iOS app application handleActionWithIdentifier forRemoteNotification completionHandler If the notification is local however the following method will be called on the app delegate of the containing iOS app when a background action is selected application handleActionWithIdentifier forLocalNotification completionHandler When a remote notification appears on the Apple Watch and the user selects a foreground notification action the WatchKit app will launch and the following method on the extension delegate will be called handleActionWithIdentifier forRemoteNotification The same scenario involving a local notification on the Apple Watch will result in the following method being called on the extension delegate of the WatchKit app handleActionWithIdentifier forLocalNotification Handling Inline Text Reply Actions Local notification inline text reply actions trigger the following method on the extension delegate of the WatchKit app handleActionWithIdentifier forLocalNotification withResponseInfo In the event that the notification containing the inline text reply action was remotely initiated the following method of the extension delegate will be called handleActionWithIdentifier forRemoteNotification withResponseInfo Both methods are passed a string containing the notification category identifier the notification object and dictionary object containing the text entered by the user The key to access to the text within the dictionary is UIUserNotificationActionResponseTypedTextKey The following code for example shows an example implementation of the handleActionWithIdentifier forLocalNotification withResponseInfo method in which the user s text input is extracted from the dictionary and printed to the console func handleActionWithIdentifier identifier String forLocalNotification localNotification UILocalNotification withResponseInfo responseInfo NSObject AnyObject let reply responseInfo UIUserNotificationActionResponseTypedTextKey print reply The topics of WatchKit notification handling action notifications and inline text input will be covered in further detail in the next chapters A watchOS 2 WatchKit Notification Tutorial A watchOS 2 WatchKit Custom Notification Tutorial and A watchOS 2 WatchKit Inline Notification Text Reply Tutorial Responding to Notifications When a notification arrives on the Apple Watch the WatchKit app will be notified via method call The specific method that is called depends on whether notification is local or remote in origin and whether or not the WatchKit app is currently active When the WatchKit app is active when the notification arrives either the didReceiveRemoteNotification or the didReceiveLocalNotification method of the extension delegate will be called depending on whether the notification was initiated remotely or locally These methods are then able to make changes where necessary within the running app in response to the notification If the WatchKit app is inactive when the notification is triggered either

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

  • A watchOS 2 WatchKit Notification Tutorial - Techotopia
    each time the notification is set The code to configure the action will be placed within the Application Delegate class within the didFinishLaunchingWithOptions method 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 Begin by locating and selecting the AppDelegate swift file and modifying the didFinishLaunchingWithOptions method to create the notification action as follows func application application UIApplication didFinishLaunchingWithOptions launchOptions NSObject AnyObject Bool let repeatAction UIMutableUserNotificationAction UIMutableUserNotificationAction repeatAction identifier REPEAT IDENTIFIER repeatAction title Repeat repeatAction destructive false repeatAction authenticationRequired false repeatAction activationMode UIUserNotificationActivationMode Background return true The above code creates a new notification action instance and configures it with an identifier string this will be used to identify which action has been selected by the user later in the project The action is also configured to display text which reads Repeat on the action button and to indicate that it is a non destructive action in other words it does not cause the loss of any data or information In the event that the device is locked when the notification is triggered the action is configured such that it is not necessary for the user to unlock the device for the action to be performed Finally since the notification can be repeated without the need to display the iOS app the activation mode is set to Background mode The next task is to create the notification category containing the action and register it with the notification system func application application UIApplication didFinishLaunchingWithOptions launchOptions NSObject AnyObject Bool var repeatAction UIMutableUserNotificationAction UIMutableUserNotificationAction repeatAction identifier REPEAT IDENTIFIER repeatAction title Repeat repeatAction destructive false repeatAction authenticationRequired false repeatAction activationMode UIUserNotificationActivationMode Background let notificationCategory UIMutableUserNotificationCategory UIMutableUserNotificationCategory notificationCategory identifier REMINDER CATEGORY notificationCategory setActions repeatAction forContext UIUserNotificationActionContext Default let settings UIUserNotificationSettings forTypes UIUserNotificationType Sound UIUserNotificationType Alert UIUserNotificationType Badge categories notificationCategory application registerUserNotificationSettings settings return true Compile and run the iOS app on a physical iPhone device or simulator session allow notification access if prompted by the operating system and tap the Set Delay button Once the delay has been set place the app into the background by pressing the Home button on the device or selecting the Hardware Home menu option within the simulator When the notification appears swipe downward on the alert panel to display the action button as shown in Figure 33 3 Figure 33 3 Assuming that the notification appears as expected the next step is to handle the Repeat action Implementing the handleActionWithIdentifier Method When a notification action button configured for background mode is tapped by the user the handleActionWithIdentifier method of the app delegate class is called Among the items passed to the method are a notification object from which both the action and category identifiers of the action that triggered the call can be obtained together with a completion handler to be called once the action has been handled This method now

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

  • A watchOS 2 WatchKit Custom Notification Tutorial - Techotopia
    func snowAlert sender AnyObject let alertTime NSDate dateByAddingTimeInterval 5 let notifyAlarm UILocalNotification notifyAlarm fireDate alertTime notifyAlarm timeZone NSTimeZone defaultTimeZone notifyAlarm soundName UILocalNotificationDefaultSoundName notifyAlarm category WEATHER CATEGORY notifyAlarm alertTitle Snow notifyAlarm alertBody It is going to snow app scheduleLocalNotification notifyAlarm Note that both of the notifications have been configured with a category of WEATHER CATEGORY It is essential that this category be referenced in the custom notification on the WatchKit app later in the tutorial Compile and run the app on an iPhone device and click on Allow when the system requests access to notifications Once access has been allowed tap on the Rain button and lock the device After 5 seconds the alert should appear on the lock screen Repeat these steps to test that the Snow button notification also works Configuring the Custom Notification Within the Project Navigator panel select the Interface storyboard file and locate the notification scenes as illustrated in Figure 34 3 Figure 34 3 The left hand scene is the static scene By default this is configured with a Label object which will display the content of the alert body text when a notification arrives For the purposes of this example no other static content is required on the static notification scene Before designing the dynamic notification scene the notification category for the custom notification needs to be changed to match that referenced when the notifications were setup in the iOS View Controller Display the Document Outline panel using the button indicated by the arrow in Figure 34 4 and unfold the section entitled Static Notification Interface Controller Within this section of the panel select the Notification Category entry may also read myCategory and within the Attributes Inspector change the Name field to WEATHER CATEGORY Figure 34 4 Designing the Dynamic Notification Scene Remaining within the storyboard drag a Label object from the Object Library and drop it above the Dismiss button in the Dynamic Interface scene Select the Label object and using the Attributes Inspector panel set the Alignment setting in the Label section of the panel to center the text within the label Within the Alignment section of the panel set the Horizontal property to the Center setting Drag and drop an Image object so that it is positioned beneath the Label object and set the Mode property to Aspect Fit and the Horizontal Alignment property to Center On completion of these steps the layout of the Dynamic Interface scene should match Figure 34 5 Figure 34 5 Display the Assistant Editor verify that it is displaying the NotificationController swift file and establish an outlet connection from the Label object named notificationAlertLabel 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 Establish a second outlet this time connected to the Image object and named notificationImage On completion of these steps the top of the

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



  •