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".
  • Synchronizing iPad iOS 5 Key-Value Data using iCloud - Techotopia
    application instance This is achieved by setting up an observer on the NSUbiquitousKeyValueStore DidChangeExternallyNotification notification This notification is triggered when a change is made to any key value pair in a specified key value store and is passed an array of strings containing the keys that were changed together with an NSNumber indicating the reason for the change In the event that the available space for the key value storage has been exceeded this number will match the NSUbiquitousKeyValueStoreQuotaViolationChange constant value An iCloud Key Value Data Storage Example The remainder of this chapter is devoted to the creation of an application that uses iCloud key value storage to store a key with a string value using iCloud In addition to storing a key value pair the application will also configure an observer to receive notification when the value is changed by another application instance Begin the application creation process by launching Xcode and creating a new Single View Application project with the name and class prefix of iCloudKeys Enabling the Application for iCloud Key Value Data Storage A mandatory step in the development of the application is to configure the appropriate iCloud entitlement This is achieved by selecting the application target at the top of the Xcode project navigator panel and selecting the Summary tab in the main panel Scroll down the summary information until the entitlements section comes into view and turn on the Enable Entitlements and iCloud Key Value Store options Figure 36 1 Once selected Xcode will create an entitlements file for the project named iCloudKeys entitlements containing the appropriate iCloud entitlements key value pairs Select the entitlements file from the project navigator and note the value assigned to the ubiquity kvstore identity key By default this is typically comprised of your team or individual developer ID combined with your reverse company domain URL and application ID Any other applications that use the same value for the entitlement key will share access to the same iCloud based key value data stored by this application Implementing the View Controller The application is going to consist of a text field into which a string may be entered by the user and a button which when selected will save the string to the application s iCloud key value data store As such the application will need an outlet to the text field and a method to be called when the button is selected In addition an instance of the NSUbiquitousKeyStore class will be needed Choose the iCloudKeysViewController h file therefore and modify it as follows import UIKit UIKit h interface iCloudKeysViewController UIViewController property strong nonatomic NSUbiquitousKeyValueStore keyStore property strong nonatomic IBOutlet UITextField textField IBAction saveKey end Modifying the viewDidLoad Method Having declared the outlets and action for the user interface and created a variable to point to the NSUbiquitousKeyValueStore object the next step is to modify the viewDidLoad method of the view controller Select the iCloudKeysViewController m implementation file and modify it to add the synthesize directive for the textField

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


  • IOS 5 iPad Data Persistence using Archiving - Techotopia
    project navigator and modify the file so that it reads as follows import UIKit UIKit h interface archiveViewController UIViewController property strong nonatomic IBOutlet UITextField name property strong nonatomic IBOutlet UITextField address property strong nonatomic IBOutlet UITextField phone property strong nonatomic NSString dataFilePath IBAction saveData end Having made the appropriate declarations in the interface file the next step is to move to the archiveViewController m implementation file where we will synthesize the access methods for our instance variables and create a template method for the saveData action method import archiveViewController h interface archiveViewController end implementation archiveViewController synthesize name address phone dataFilePath void saveData end Memory Management Having allocated memory in implementing the above outlets it is important that we add code to indicate that resources can be freed when the view is no longer needed To do so edit the archiveViewController m file again and modify the viewDidUnload method as follows void viewDidUnload Release any retained subviews of the main view e g self myOutlet nil self name nil self address nil self phone nil self dataFilePath nil We will need to do some more coding later but at this point it makes sense to design the user interface and establish the connections between the user interface object and the outlets and action we have declared so far Designing the User Interface The user interface for our application is going to consist of three UILabels three UITextFields and single UIButton Select archiveViewController xib in the main Xcode project navigator and display the Object library panel View Utilities Show Object Library if it is not already visible Drag drop resize position and configure objects on the View window canvas until your design approximates that illustrated in Figure 37 1 Figure 37 1 The next step is to establish the connections to our action and outlets Beginning with the outlets hold down the Ctrl key and click and drag from the File s Owner object to the text field component located to right of the Name label object Release the Ctrl key and mouse button and select the name outlet from the resulting menu Repeat these steps for the address and phone text fields connecting them to the corresponding outlets To connect the action select the Save button object in the view window and display the Connections Inspector View Utilities Show Connections Inspector Click with the mouse within the small round circle to the right of the Touch Up Inside event and drag the blue line to the File s Owner object Release the mouse button and select the saveData method from the resulting menu Checking for the Existence of the Archive File on Startup Each time the application is launched by the user the code will need to identify whether the archive data file exists from a previous session In the event that it does exist the archive will need to read the contents to recreate the original array object from which the archive was created Using this newly recreated array object

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

  • IOS 5 iPad Database Implementation using SQLite - Techotopia
    host system in the following folder Users user Library Application Support iPhone Simulator sdk version Applications id Documents Where user is the login name of the user running the iOS Simulator session sdk version is the version of the iOS SDK used to build the application and id is the unique ID of the application Preparing an iPad Application Project for SQLite Integration By default the Xcode environment does not assume that you will be including SQLite in your application When developing SQLite based applications a few additional steps are required to ensure the code will compile when the application is built Firstly the project needs to be configured to include the libsqlite3 dylib dynamic library during the link phase of the build process To achieve this select the target entry in the Xcode project navigator the top entry with the product name to display the summary information Select the Build Phases tab to display the build information Figure 38 1 The Link Binary with Libraries section lists the libraries and frameworks already included in the project To add another library or framework click on the button to display the full list From this list search for and then select the required item in this case libsqlite3 dylib and click Add Secondly the sqlite3 h include file must be imported into any files where references are made to SQLite definitions declarations or functions This file is located in the usr include directory and may be imported when needed as follows import usr include sqlite3 h Key SQLite Functions When implementing a database using SQLite it will be necessary to utilize a number of C functions contained within the libsqlite3 dylib library A summary of the most commonly used functions is as follows sqlite3 open Opens specified database file If the database file does not already exist it is created sqlite3 close Closes a previously opened database file sqlite3 prepare v2 Prepares a SQL statement ready for execution sqlite3 step Executes a SQL statement previously prepared by the sqlite3 prepare v2 function sqlite3 column type Returns a data field from the results of a SQL retrieval operation where type is replaced by the data type of the data to be extracted text blob bytes int int16 etc sqlite3 finalize Deletes a previously prepared SQL statement from memory sqlite3 exec Combines the functionality of sqlite3 prepare v2 sqlite3 step and sqlite3 finalize into a single function call This of course represents only a small subset of the complete range of functions available with SQLite A full list can be found at http www sqlite org c3ref funclist html Declaring a SQLite Database Before any tasks can be performed on a database it must first be declared To do so it is necessary to declare a variable that points to an instance of a structure of type sqlite3 the sqlite3 structure is defined in the sqlite3 h include file For example sqlite3 contactDB Declare a pointer to sqlite database structure Opening or Creating

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

  • An Example SQLite based iOS 5 iPad Application - Techotopia
    a table within the database in which to store the contact information entered by the user The code to perform this task can be placed in the viewDidLoad method of our view controller class Select the databaseViewController m file scroll down to the viewDidLoad method and modify it as follows void viewDidLoad super viewDidLoad NSString docsDir NSArray dirPaths Get the documents directory dirPaths NSSearchPathForDirectoriesInDomains NSDocumentDirectory NSUserDomainMask YES docsDir dirPaths objectAtIndex 0 Build the path to the database file databasePath NSString alloc initWithString docsDir stringByAppendingPathComponent contacts db NSFileManager filemgr NSFileManager defaultManager if filemgr fileExistsAtPath databasePath NO const char dbpath databasePath UTF8String if sqlite3 open dbpath contactDB SQLITE OK char errMsg const char sql stmt CREATE TABLE IF NOT EXISTS CONTACTS ID INTEGER PRIMARY KEY AUTOINCREMENT NAME TEXT ADDRESS TEXT PHONE TEXT if sqlite3 exec contactDB sql stmt NULL NULL errMsg SQLITE OK status text Failed to create table sqlite3 close contactDB else status text Failed to open create database The code in the above method performs the following tasks Identifies the application s Documents directory and constructs a path to the contacts db database file Creates an NSFileManager instance and subsequently uses it to detect if the database file already exists If the file does not yet exist the code converts the path to a UTF 8 string and creates the database via a call to the SQLite sqlite3 open function passing through a reference to the contactDB variable declared previously in the interface file Prepares a SQL statement to create the contacts table in the database Reports the success or otherwise of the operation via the status label Closes the database Implementing the Code to Save Data to the SQLite Database The saving of contact data to the database is the responsibility of the saveData action method This method will need to open the database file extract the text from the three text fields and construct and execute a SQL INSERT statement to add this data as a record to the database Having done this the method will then need to close the database In addition the code will need to clear the text fields ready for the next contact to be entered and update the status label to reflect the success or failure of the operation In order to implement this behavior therefore we need to modify the template method created previously as follows void saveData sqlite3 stmt statement const char dbpath databasePath UTF8String if sqlite3 open dbpath contactDB SQLITE OK NSString insertSQL NSString stringWithFormat INSERT INTO CONTACTS name address phone VALUES name text address text phone text const char insert stmt insertSQL UTF8String sqlite3 prepare v2 contactDB insert stmt 1 statement NULL if sqlite3 step statement SQLITE DONE self status text Contact added self name text self address text self phone text else self status text Failed to add contact sqlite3 finalize statement sqlite3 close contactDB The next step in our application development process is to implement the action for the find button Implementing Code to Extract Data

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

  • An iOS 5 iPad Core Data Tutorial - Techotopia
    readonly strong nonatomic NSPersistentStoreCoordinator persistentStoreCoordinator void saveContext NSURL applicationDocumentsDirectory end With an instance of the view controller declared in the interface file we now need to modify the applicationDidFinishLaunchingWithOptions method located in the coreDataAppDelegate m implementation file to initialize and allocate the coreDataViewController instance and assign it as the window view so that it is visible to the user Note also that we have to import coreDataViewController h into this file and synthesize accessors for the viewController object import coreDataAppDelegate h import coreDataViewController h implementation coreDataAppDelegate synthesize window window synthesize managedObjectContext managedObjectContext synthesize managedObjectModel managedObjectModel synthesize persistentStoreCoordinator persistentStoreCoordinator synthesize viewController BOOL application UIApplication application didFinishLaunchingWithOptions NSDictionary launchOptions self window UIWindow alloc initWithFrame UIScreen mainScreen bounds Override point for customization after application launch viewController coreDataViewController alloc initWithNibName coreDataViewController bundle nil self window backgroundColor UIColor whiteColor self window addSubview viewController view self window makeKeyAndVisible return YES end Adding Actions and Outlets to the View Controller As with previous instances of our Contacts application our Core Data example will include a user interface that needs to accept and display name address and phone information and to react to the selection of the save and find buttons In addition a status label will be used to provide the user with feedback To declare the outlets and actions associated with these user interface components select the coreDataViewController h file and modify the class declaration so that it reads as follows import UIKit UIKit h interface coreDataViewController UIViewController property strong nonatomic IBOutlet UITextField name property strong nonatomic IBOutlet UITextField address property strong nonatomic IBOutlet UITextField phone property strong nonatomic IBOutlet UILabel status IBAction saveData IBAction findContact end Next select the coreDataViewController m implementation file add an synthesize directive for the outlets import the coreDataAppDelegate h file and add template methods for the two declared actions import coreDataViewController h import coreDataAppDelegate h interface coreDataViewController end implementation coreDataViewController synthesize name address phone status void saveData void findContact end Designing the User Interface With the actions and outlets defined now is a good time to design the user interface and establish the connections so select the coreDataViewController xib file to begin the design work The user interface and corresponding connections used in this tutorial are the same as those in previous data persistence chapters The completed view should once again appear as outlined in Figure 41 3 Figure 41 3 Before proceeding stretch the status label located above the two buttons so that it covers most of the width of the view Finally edit the label and remove the word Label so that it is blank Next connect the three text fields and status label to the name address phone and status outlets respectively by holding down the Ctrl key and clicking and dragging from File s Owner icon to the corresponding component From the resulting menu select the outlet corresponding to the selected view object The last step involves connecting the two buttons to the corresponding actions First display the Connections Inspector View Utilities Show Connections Inspector then

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

  • An Example iOS 5 iPad Touch, Multitouch and Tap Application - Techotopia
    UILabel touchStatus property strong nonatomic IBOutlet UILabel methodStatus property strong nonatomic IBOutlet UILabel tapStatus end Having declared the outlets in the interface file edit the touchViewController m implementation file and add the corresponding synthesize directive import touchViewController h interface touchViewController end implementation touchViewController synthesize touchStatus methodStatus tapStatus end Having declared the outlets it is time to design the user interface Designing the User Interface Load the view into the Interface Builder tool by selecting the touchViewController xib file Within the Interface Builder panel modify the user interface by adding label components from the Object library View Utilities Show Object Library and modifying properties until the view appears as outlined in Figure 43 1 When adding the labels to the right of the view be sure to stretch them so that they reach beyond the mid point of the view area Figure 43 1 Ctrl click on the File s Owner icon and drag the resulting blue line to the label adjacent to the Method label and select the methodStatus outlet from the resulting menu Repeat these steps to establish connections between the touchStatus and tapStatus outlets and the corresponding label objects Enabling Multitouch on the View By default views are configured to respond to only single touches in other words a single finger touching or tapping the screen at any one time For the purposes of this example we plan to detect multiple touches In order to enable this support it is necessary to change an attribute of the view object To achieve this click on the background of the View window display the Attribute Inspector View Utilities Show Attribute Inspector and make sure that the Multiple Touch option is selection in the Interaction section at the bottom of the window Figure 43 2 Implementing the touchesBegan Method When the user touches the screen the touchesBegan method of the first responder is called In order to capture these event types we need to implement this method in our view controller In the Xcode project navigator select the touchViewController m file and add the touchesBegan methods as follows void touchesBegan NSSet touches withEvent UIEvent event NSUInteger touchCount touches count NSUInteger tapCount touches anyObject tapCount self methodStatus text touchesBegan self touchStatus text NSString stringWithFormat d touches touchCount self tapStatus text NSString stringWithFormat d taps tapCount This method obtains a count of the number of touch objects contained in the touches set essentially the number of fingers touching the screen and assigns it to a variable It then gets the tap count from one of the touch objects The code then updates the methodStatus label to indicate that the touchesBegan method has been triggered constructs a string indicating the number of touches and taps detected and displays the information on the touchStatus and tapStatus labels Implementing the touchesMoved Method When the user moves one or more fingers currently in contact with the surface of the iPad touch screen the touchesMoved method is called repeatedly until the movement ceases In order to capture these

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

  • Detecting iOS 5 iPad Touch Screen Gesture Motions - Techotopia
    Y coordinates of the touch and update these values in real time as the finger moves across the screen When the finger is lifted from the screen the start and end coordinates of the gesture will then be displayed This will require the creation of two outlets that will later be connected to labels within the user interface of the application In addition we need to add an instance variable to our view controller in which to store the start position of the touch To create these outlets and the instance variable select the touchMotionViewController h file and modify it as follows import UIKit UIKit h interface touchMotionViewController UIViewController property strong nonatomic IBOutlet UILabel xCoord property strong nonatomic IBOutlet UILabel yCoord property CGPoint startPoint end Next edit the touchMotionViewController m file and add the synthesize directive import touchMotionViewController h interface touchMotionViewController end implementation touchMotionViewController synthesize xCoord yCoord startPoint end Designing the Application User Interface Select the touchMotionViewController xib file to load the NIB file into Interface Builder Within the Interface Builder panel create a user interface such that it resembles the layout in Figure 44 1 Figure 44 1 Be sure to stretch the labels so that they both extend to cover most of the width of the view Once the label objects have been added to the view establish a connection between the xCoord outlet and the top label by Crtl clicking on the File s Owner and dragging the blue line to the top label Release the pointer and select xCoord from the resulting menu Repeat this step for the yCoord Implementing the touchesBegan Method When the user first touches the screen the location coordinates need to be saved in the startPoint instance variable and the coordinates reported to the user This can be achieved by implementing the touchesBegan method in the touchMotionViewController m file as follows void touchesBegan NSSet touches withEvent UIEvent event UITouch theTouch touches anyObject startPoint theTouch locationInView self view CGFloat x startPoint x CGFloat y startPoint y self xCoord text NSString stringWithFormat x f x self yCoord text NSString stringWithFormat y f y Implementing the touchesMoved Method When the user s finger moves across the screen the touchesMoved event will be called repeatedly until the motion stops By implementing the touchesMoved method in our view controller therefore we can detect the motion and display the revised coordinates to the user void touchesMoved NSSet touches withEvent UIEvent event UITouch theTouch touches anyObject CGPoint touchLocation theTouch locationInView self view CGFloat x touchLocation x CGFloat y touchLocation y self xCoord text NSString stringWithFormat x f x self yCoord text NSString stringWithFormat y f y Implementing the touchesEnded Method When the user s finger lifts from the screen the touchesEnded method of the first responder is called The final task therefore is to implement this method in our view controller such that it displays the start and end points of the gesture void touchesEnded NSSet touches withEvent UIEvent event UITouch theTouch touches anyObject CGPoint endPoint theTouch locationInView self

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

  • An iPad iOS 5 Gesture Recognition Tutorial - Techotopia
    that the corresponding gesture has been detected With these requirements in mind select the recognizerViewController h file and modify it as follows import UIKit UIKit h interface recognizerViewController UIViewController property strong nonatomic IBOutlet UILabel statusLabel IBAction tapDetected id sender IBAction pinchDetected id sender IBAction rotationDetected id sender IBAction swipeDetected id sender IBAction longPressDetected id sender end Next edit the recognizerViewController m file and add the synthesize directive for the new outlet and update viewDidUnload import recognizerViewController h interface recognizerViewController end implementation recognizerViewController synthesize statusLabel void viewDidUnload super viewDidUnload Release any retained subviews of the main view e g self myOutlet nil self statusLabel nil end Designing the User Interface Select the recognizerViewController xib file to display the interface builder panel In addition to the UIView created for us by Xcode when the project was created the only visual user interface object that needs to be added is the status label Display the Object library View Utilities Show Object Library and drag a Label object from the library to the center of the view Once positioned stretch the label to the outer edges of the view and then modify the label properties to center the label text Ctrl click on the File s Owner object drag the blue line to the label object and release From the resulting menu select the statusLabel outlet Next the non visual gesture recognizer objects need to be added to the design Scroll down the list of objects in the Object Library panel until the Tap Gesture Recognizer object comes into view Drag and drop the object onto the View in the design area if the object is dropped outside the view the connection between the recognizer and the view on which the gestures are going to be performed will not be established Repeat these steps to add Pinch Rotation Swipe and Long Press Gesture Recognizer objects to the design Note that the center panel has updated to reflect the presence of the gesture recognizer objects as illustrated in Figure 46 1 Figure 46 1 Within the center panel select the Tap Gesture Recognizer instance and display the Attributes Inspector View Utilities Show Attributes Inspector Within the attributes panel change the Taps value to 2 so that only double taps are detected Similarly select the Long Press Recognizer object and change the Press Duration attribute to 3 seconds Having added and configured the gesture recognizers the next step is to connect each recognizer to its corresponding action method Remaining within the Interface Builder environment Ctrl click on the Tap Gesture Recognizer and drag the resulting blue line to the File s Owner object Upon releasing the line select the tapDetected method from the resulting menu Repeat these steps to connect the remaining recognizer objects to the corresponding action methods Adding the Action Methods Having configured the gesture recognizers the next step is to write the action methods that will be called by each recognizer when the corresponding gesture is detected These methods will also reside in

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



  •