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".
  • Implementing a Page based iOS 7 Application using UIPageViewController - Techotopia
    assigned a data source which in turn is responsible for providing view controller objects as required for each page The data source takes the form of a class instance that implements the UIPageViewControllerDataSource protocol which at a minimum must implement the following two methods viewControllerAfterViewController This method is passed a view controller representing the currently displayed page and is required to return the view controller corresponding to the next page in the paging sequence viewControllerBeforeViewController This method is passed the view controller representing the currently displayed page and is required to return the view controller corresponding to the previous page in the paging sequence The mechanism used to create the requested view controllers and the content therein will generally be application specific and is at the discretion of the developer Apple does however recommend that in order to ensure optimal performance and minimal resource usage the view controllers be created on an as needed basis rather than pre created When a UIPageViewController object is initialized a number of configuration options may be specified to configure the appearance and behavior of the contained views eBookFrenzy com Purchase the fully updated iOS 9 Swift 2 Xcode 7 edition of this book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Navigation Orientation The page controller is capable of transitioning between views using either a vertical or horizontal paradigm In the case of horizontal navigation page transitions take place in the same way pages are turned in a physical book by sweeping a finger on the screen either left or right In the case of horizontal navigation pages are turned by making vertical gestures in much the same way the pages of a wall calendar are flipped These options are configured using the following constants UIPageViewControllerNavigationOrientationHorizontal UIPageViewControllerNavigationOrientationVertical Spine Location The UIPageViewController class allows for the location of the spine to be configured The term spine in this context is analogous to the spine of a book and dictates the location of the axis on which each page will turn The behavior of the spine location settings vary depending on the navigation orientation setting For example the default for most configurations is UIPageViewControllerSpineLocationMin which places the spine on the left hand side or top of the screen depending on whether the navigation orientation is horizontal or vertical Similarly the UIPageViewControllerSpineLocationMax setting will position the spine at the right or bottom edge of the display In order to display two pages simultaneously the UIPageViewControllerSpineLocationMid setting should be used The view controller may also be configured to treat pages as being double sided via the doubleSided property Note that when using UIPageViewControllerSpineLocationMid spine location it will be necessary to provide the page controller with two view controllers one for the left hand page and one for the right for each page turn Similarly when using either the min or max spine location together with the double sided setting view controllers for

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


  • An Example iOS 7 UIPageViewController Application - Techotopia
    an array object containing a number of string objects each configured to contain slightly different HTML content For the purposes of this example the data source for the UIPageViewController instance will be the application s PageAppViewController class This class will therefore need references to an NSArray and a UIPageViewController object It will also be necessary to declare this class as implementing the UIPageViewControllerDataSource protocol Select the PageAppViewController h file and add these references as follows together with an import directive for the ContentViewController h file import UIKit UIKit h import ContentViewController h interface PageAppViewController UIViewController UIPageViewControllerDataSource property strong nonatomic UIPageViewController pageController property strong nonatomic NSArray pageContent end The final step in creating the model is to add a method to the PageAppViewController m file to add the HTML strings to the array and then call that method from viewDidLoad import PageAppViewController h interface PageAppViewController end implementation PageAppViewController void createContentPages NSMutableArray pageStrings NSMutableArray alloc init for int i 1 i 11 i NSString contentString NSString alloc initWithFormat html head head body br h1 Chapter d h1 p This is the page d of content displayed using UIPageViewController in iOS 7 p body html i i pageStrings addObject contentString pageContent NSArray alloc initWithArray pageStrings void viewDidLoad super viewDidLoad self createContentPages The application now has a content view controller and a data model from which the content of each page will be extracted by the data source methods The next logical step therefore is to implement those data source methods As previously outlined in Implementing a Page based iOS 7 Application using UIPageViewController instances of the UIPageViewController class need a data source This takes the form of two methods one of which is required to return the view controller to be displayed after the currently displayed view controller and the other the view controller to be displayed before the current view controller Since the PageAppViewController is going to act as the data source for the page view controller object these two methods together with two convenience methods which we will borrow from the Xcode Page based Application template will need to be added to the PageAppViewController m file Begin by adding the two convenience functions import PageAppViewController h interface PageAppViewController end implementation PageAppViewController ContentViewController viewControllerAtIndex NSUInteger index Return the data view controller for the given index if self pageContent count 0 index self pageContent count return nil Create a new view controller and pass suitable data ContentViewController dataViewController ContentViewController alloc init UIStoryboard storyboard UIStoryboard storyboardWithName Main bundle NSBundle mainBundle ContentViewController dataViewController storyboard instantiateViewControllerWithIdentifier contentView dataViewController dataObject pageContent index return dataViewController NSUInteger indexOfViewController ContentViewController viewController return pageContent indexOfObject viewController dataObject end The viewControllerAtIndex method begins by checking to see if the page being requested is outside the bounds of available pages by checking if the index reference is zero the user cannot page back beyond the first page or greater than the number of items in the pageContent array In the event that the index value is valid a new instance of the ContentViewController

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

  • Using the iOS 7 UIPickerView and UIDatePicker Components - Techotopia
    in the far left hand component The UIDatePicker component is a pre configured class designed specifically for the selection of dates and times The UIPickerView class on the other hand allows for the creation of custom picker controls and will be explored in An iOS 7 UIPickerView Example A DatePicker Example By way of introduction to the concept of using pickers in an iOS 7 application we will begin with a very simple example that utilizes the UIDatePicker class The application will consist of a DatePicker a label and a button When the button is pressed by the user the date and time selected in the picker will be displayed on the label component Begin by creating a new iOS project using the Single View Application template with the Devices menu set to iPhone and DatePicker entered for both the product name and class prefix eBookFrenzy com Purchase the fully updated iOS 9 Swift 2 Xcode 7 edition of this book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Designing the User Interface Select the Main storyboard file and drag and drop the Date Picker Label and Button components from the Object Library panel View Utilities Show Object Library onto the view and modify and position the layout so that the view resembles Figure 29 2 google ADSDAQBOX FLOW google Figure 29 2 Note that the text of the label is centered assigned a 10 point font and that it has been stretched to the outer margins of the containing view The configuration of the Date Picker object such as date format etc may be changed if desired via a variety of properties accessed by selecting the object in the view and displaying the Attributes Inspector View Utilities Show Attributes Inspector Select the Date Picker object in the view canvas display the Assistant Editor panel and verify that the editor is displaying the contents of the DatePickerViewController h file Ctrl click on the Date Picker object and drag to a position just below the interface line in the Assistant Editor Release the line and in the resulting connection dialog establish an outlet connection named datePicker Repeat this step to connect the Label object to an outlet property named dateLabel Finally Ctrl click on the Button object drag the line to the Assistant Editor panel and configure an Action connection on the Touch Up Inside event to a method named getSelection Coding the Date Picker Example Functionality Now that the outlets and user interface have been implemented it is time to write the functional code that will make the application work In actual fact the only method that needs to be implemented for this simple example is the getSelection action method that is called when the user touches the button The following code fragment outlines the required changes to the DatePickerViewController m file import DatePickerViewController h interface DatePickerViewController end implementation DatePickerViewController void getSelection id sender

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

  • An iOS 7 UIPickerView Example - Techotopia
    interface PickerViewController UIViewController UIPickerViewDelegate UIPickerViewDataSource property strong nonatomic NSArray countryNames property strong nonatomic NSArray exchangeRates end Designing the User Interface To design the user interface begin by selecting the Main storyboard file to load it into Interface Builder Drag and drop a UIPickerView from the Object Library View Utilities Show Object Library onto the view and position it at the bottom of the view Also add a label and text field Stretch the right and left hand edges of the label until the dotted blue margin line appears Using the Attributes Inspector View Utilities Show Attributes Inspector configure centered alignment on the label and reduce the font size to 12 point Select the text field and change the Keyboard property in the Attributes Inspector to Numbers and Punctuation then change the Placeholder setting to US Dollars USD Once completed the view should appear as illustrated in the following figure Figure 30 1 Select the Picker View object in the view canvas display the Assistant Editor panel and verify that the editor is displaying the contents of the PickerViewController h file Ctrl click on the Picker View object and drag to a position just below the interface line in the Assistant Editor Release the line and in the resulting connection dialog establish an outlet connection named picker Repeat these steps to establish outlet connections from the Text Field and Label to properties named dollarText and resultLabel respectively Next select the PickerView component in the View window and display the Connections Inspector View Utilities Show Connections Inspector Click in the round circle to the right of the dataSource outlet in the inspector window and drag the line to the Picker View Controller icon located in the panel beneath the view canvas Figure 30 2 Repeat this task for the delegate outlet Figure 30 2 Finally Ctrl click on the text field object and drag the line to the area immediately beneath the newly created outlets in the Assistant Editor panel Release the line and within the connection dialog establish an Action method on the Did End On Exit event configured to call a method named textFieldReturn The user interface is now designed and the outlets and action connected Initializing the Arrays The data that will be used in our application is stored in two arrays one for the country name and the other for the corresponding exchange rate In the real world the application would likely obtain up to date exchange rate information from an external source but for the purposes of this example we will hard code the prevailing rates at the time of writing These arrays need to be initialized when the application loads so the necessary code should be added to the viewDidLoad method of the PickerViewController m file import PickerViewController h interface PickerViewController end implementation PickerViewController void viewDidLoad super viewDidLoad countryNames Australia AUD China CNY France EUR Great Britain GBP Japan JPY exchangeRates 0 9922f 6 5938f 0 7270f 0 6206f 81 57f eBookFrenzy com Purchase the fully

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

  • Working with Directories on iOS 7 - Techotopia
    method of the file manager object For example the following code fragment identifies the current working directory NSFileManager filemgr NSString currentPath filemgr NSFileManager defaultManager currentPath filemgr currentDirectoryPath In this code we declare a new object named filemgr that is an instance of the NSFileManager class and also an NSString object named currentPath to contain the current working directory path Having created a file manager instance we then call the currentDirectoryPath method of that instance and assign the resulting string to currentPath Identifying the Documents Directory As previously discussed each iOS application on a device has its own private Documents and tmp directories into which it is permitted to read and write data Because the location of these directories is different for each application the only way to find the correct path is to ask iOS In fact the exact location will also differ depending on whether the application is running on a physical iPhone or iPad device or in the iOS Simulator The Documents directory for an application may be identified by making a call to a C function named NSSearchPathForDirectoriesInDomains passing through an argument in this case NSDocumentDirectory indicating that we require the path to the Documents directory Since this is a C function as opposed to a method of an Objective C class there is no need for us to establish an instance of a Foundation class such as NSFileManager before making the call That said the function does return an object in the form of an NSArray containing the results of the request We can therefore obtain the path to the current application s Documents directory as follows NSArray dirPaths NSString docsDir dirPaths NSSearchPathForDirectoriesInDomains NSDocumentDirectory NSUserDomainMask YES docsDir dirPaths 0 When executed the above code will assign the path to the Documents directory to the docsDir string When executed within the iOS Simulator environment the path returned will take the form of Users user name Library Application Support iPhone Simulator sdk version Applications app id Documents Where user name is the name of the user currently logged into the Mac OS X system on which the simulator is running sdk version is the version of the iOS SDK used to compile the application and app id is the unique ID of the app for example 06A3AEBA 8C34 476E 937F A27BDD2E450A Clearly this references a path on your Mac OS X system so feel free to open up a Finder window and explore the file system sandbox areas for your iOS applications When executed on a physical iOS device the path returned by the function call will take the following form var mobile Applications app id Documents Identifying the Temporary Directory In addition to the Documents directory iOS applications are also provided with a tmp directory for the storage of temporary files The path to the current application s temporary directory may be ascertained with a call to the NSTemporaryDirectory C function as follows NSString tmpDir NSTemporaryDirectory Once executed the string object referenced by tmpDir will contain the

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

  • Working with Files on iOS 7 - Techotopia
    NSFileManager filemgr filemgr NSFileManager defaultManager if filemgr removeItemAtPath your file path error NULL YES NSLog Remove successful else NSLog Remove failed Creating a Symbolic Link A symbolic link to a particular file may be created using the createSymbolicLinkAtPath method This takes as arguments the path of the symbolic link the path to the file to which the link is to refer and an optional NSError object For example the following code creates a symbolic link from Users demo file1 txt that links to the pre existing file tmp myfile txt NSFileManager filemgr filemgr NSFileManager defaultManager if filemgr createSymbolicLinkAtPath your file path withDestinationPath your file path error NULL YES NSLog Remove successful else NSLog Remove failed eBookFrenzy com Purchase the fully updated iOS 9 Swift 2 Xcode 7 edition of this book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Reading and Writing Files with NSFileManager The NSFileManager class includes some basic file reading and writing capabilities These capabilities are somewhat limited when compared to the options provided by the NSFileHandle class but can be useful nonetheless Firstly the contents of a file may be read and stored in an NSData object through the use of the contentsAtPath method NSFileManager filemgr NSData databuffer filemgr NSFileManager defaultManager databuffer filemgr contentsAtPath your file path Having stored the contents of a file in an NSData object that data may subsequently be written out to a new file using the createFileAtPath method databuffer filemgr contentsAtPath your file path filemgr createFileAtPath your file path contents databuffer attributes nil In the above example we have essentially copied the contents from an existing file to a new file This however gives us no control over how much data is to be read or written and does not allow us to append data to the end of an existing file If the file tmp newfile txt in the above example had already existed it and any data it contained would have been overwritten by the contents of the source file Clearly some more flexible mechanism is required This is provided by the Foundation Framework in the form of the NSFileHandle class Working with Files using the NSFileHandle Class The NSFileHandle class provides a range of methods designed to provide a more advanced mechanism for working with files In addition to files this class can also be used for working with devices and network sockets In the following sections we will look at some of the more common uses for this class Creating an NSFileHandle Object An NSFileHandle object can be created when opening a file for reading writing or updating in other words both reading and writing This is achieved using the fileHandleForReadingAtPath fileHandleForWritingAtPath and fileHandleForUpdatingAtPath methods respectively Having opened a file it must subsequently be closed when we have finished working with it using the closeFile method If an attempt to open a file fails for example because an attempt is made

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

  • IOS 7 Directory Handling and File I/O – A Worked Example - Techotopia
    components and resize the width of the text field so that the layout appears as illustrated in Figure 33 1 Figure 33 1 Select the Text Field object in the view canvas display the Assistant Editor panel and verify that the editor is displaying the contents of the FileExampleViewController h file Ctrl click on the text field object and drag to a position just below the interface line in the Assistant Editor Release the line and in the resulting connection dialog establish an outlet connection named textBox Ctrl click on the button object and drag the line to the area immediately beneath the newly created outlet in the Assistant Editor panel Release the line and within the resulting connection dialog establish an Action method on the Touch Up Inside event configured to call a method named saveText eBookFrenzy com Purchase the fully updated iOS 9 Swift 2 Xcode 7 edition of this book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Checking the Data File on Application Startup Each time the application is launched by the user it will need to check to see if the data file already exists if the user has not previously saved any text the file will not have been created If the file does exist the contents need to be read by the application and displayed within the text field A good place to put initialization code of this nature is in the viewDidLoad method of the view controller With this in mind select the FileExampleViewController m file scroll down to the viewDidLoad method and edit it as follows void viewDidLoad super viewDidLoad NSFileManager filemgr NSString dataFile NSString docsDir NSArray dirPaths filemgr NSFileManager defaultManager Identify the documents directory dirPaths NSSearchPathForDirectoriesInDomains NSDocumentDirectory NSUserDomainMask YES docsDir dirPaths 0 Build the path to the data file dataFile docsDir stringByAppendingPathComponent datafile dat Check if the file already exists if filemgr fileExistsAtPath dataFile Read file contents and display in textBox NSData databuffer databuffer filemgr contentsAtPath dataFile NSString datastring NSString alloc initWithData databuffer encoding NSASCIIStringEncoding textBox text datastring Before proceeding we need to take some time to talk about what the above code is doing First we declare some variables that will be used in the method and then create an instance of the NSFileManager class Because each iOS application on a device has its own Documents directory we next make the appropriate calls to identify the path to that directory and assign the result to the docsDir variable Once we know where the documents directory is located we construct the full path to our file which is named datafile dat before checking whether the file already exists If it exists we read the contents of the file and assign it to the text property of our text field object so that it is visible to the user Finally we release the file manager object Now that we have the initialization code implemented we

    Original URL path: http://www.techotopia.com/index.php/IOS_7_Directory_Handling_and_File_I/O_%E2%80%93_A_Worked_Example (2016-02-13)
    Open archived version from archive

  • Preparing an iOS 7 App to use iCloud Storage - Techotopia
    iCloud storage methods outlined in the preceding section of this chapter Fortunately both of these tasks can be performed within the Capabilities screen within Xcode 5 Clearly iOS developers who are not yet members of the iOS Developer Program will need to enroll before implementing any iCloud functionality Details on enrolling in this program are outlined in the Joining the Apple iOS Developer Program iOS 7 chapter of this book eBookFrenzy com Purchase the fully updated iOS 9 Swift 2 Xcode 7 edition of this book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Enabling iCloud Support for an iOS 7 Application In order to enable iCloud support for an application load the project into Xcode and select the application name target from the top of the project navigator panel From the resulting project settings panel select the Capabilities tab and locate and switch on iCloud support as outlined in Figure 34 1 selecting a Development Team to use for the provisioning profile if prompted to do so Figure 34 1 Enabling iCloud support will have automatically added the iCloud entitlement to the application s App ID and also created an entitlements file to the project containing the application s iCloud container identifiers Reviewing the iCloud Entitlements File Once iCloud capabilities have been enabled for an application within Xcode a new file will appear in the project named product name entitlements Any applications that intend to use iCloud storage in any way must obtain entitlements appropriate to the iCloud features to be used These entitlements are placed into this entitlements file and built into the application at compile time If the application is intended to make use of iCloud document storage then the entitlements file must include a request for the com apple developer ubiquity container identifiers entitlement Similarly if the key value store is to be used then the com apple developer ubiquity kvstore identifier entitlement must be included Applications that require both forms of iCloud storage must include both entitlements The entitlements file is an XML file in which the requests are stored in a key value format The keys are the entitlement identifiers outlined above and the values are represented by one or more container identifiers comprised of the developer s ID and a custom string that uniquely identifies the application the corresponding application s App ID is generally recommended though not mandatory for this value The entitlements file may be created either manually or as outlined above automatically from within the Xcode environment When using the Capabilities settings the entitlements file will appear in the project navigator panel Note that by default Xcode only enables iCloud container storage If key value storage is also required make sure that the Use key value store option is also selected within the Capabilities panel A single ubiquity container is added to the entitlements file by default when using the Capabilities panel Additional containers

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



  •