document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #import "buildingsAppDelegate.h"
  2. #import "Building.h"
  3. #import <sqlite3.h>
  4.  
  5.  
  6. @implementation buildingsAppDelegate
  7.  
  8. @synthesize window = _window;
  9. @synthesize databaseName, databasePath, items;
  10.  
  11. - (void)applicationWillResignActive:(UIApplication *)application
  12. {
  13.     /*
  14.      Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  15.      Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
  16.      */
  17. }
  18.  
  19. - (void)applicationDidEnterBackground:(UIApplication *)application
  20. {
  21.     /*
  22.      Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  23.      If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  24.      */
  25. }
  26.  
  27. - (void)applicationWillEnterForeground:(UIApplication *)application
  28. {
  29.     /*
  30.      Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
  31.      */
  32. }
  33.  
  34. - (void)applicationDidBecomeActive:(UIApplication *)application
  35. {
  36.     /*
  37.      Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  38.      */
  39. }
  40.  
  41. - (void)applicationWillTerminate:(UIApplication *)application
  42. {
  43.     /*
  44.      Called when the application is about to terminate.
  45.      Save data if appropriate.
  46.      See also applicationDidEnterBackground:.
  47.      */
  48. }
  49.  
  50.  
  51. //------------- BEGINNING OF ADDED CODE ---------------------------------
  52.  
  53.  
  54. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  55. {
  56.    
  57.    
  58.      // Override point for customization after application launch.
  59.      databaseName = @"buildings.sqlite";
  60.      
  61.      
  62.      // Get the path to the documents directory and append the databaseName
  63.      NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  64.      
  65.      NSString *documentsDir = [documentPaths objectAtIndex:0];
  66.      databasePath = [documentsDir stringByAppendingPathComponent: databaseName];
  67.      
  68.      
  69.      // Execute the "checkAndCreateDatabase" function
  70.      [self checkAndCreateDatabase];
  71.      
  72.      // Query the database for all animal records and construct the "animals" array
  73.      [self readBuildingsFromDatabase];
  74.      
  75.      
  76.      
  77.    
  78.     return YES;
  79. }
  80.  
  81.  
  82.  
  83. - (void) checkAndCreateDatabase{
  84.    
  85.     // Check if the SQL database has already been saved to the users phone, if not then copy it over
  86.     BOOL success;
  87.    
  88.     // Create a FileManager object, we will use this to check the status
  89.     // of the database and to copy it over if required
  90.     NSFileManager *fileManager = [NSFileManager defaultManager];
  91.    
  92.     // Check if the database has already been created in the users filesystem
  93.     success = [fileManager fileExistsAtPath:databasePath];
  94.    
  95.     // If the database already exists then return without doing anything
  96.     if(success) return;
  97.    
  98.     // If not then proceed to copy the database from the application to the users filesystem
  99.    
  100.     // Get the path to the database in the application package
  101.     NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
  102.    
  103.     // Copy the database from the package to the users filesystem
  104.     [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
  105.    
  106.    
  107.    
  108.    
  109. }
  110.  
  111.  
  112.  
  113. - (void) readBuildingsFromDatabase{
  114.    
  115.    
  116.     // Setup the database object
  117.     sqlite3 *database;
  118.    
  119.     // Init the BuildingsArray
  120.     items = [[NSMutableArray alloc] init];
  121.    
  122.     // Open the database from the users filessytem
  123.     if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
  124.        
  125.         // Setup the SQL Statement and compile it for faster access
  126.         const char *sqlStatement = "select * from buildings";
  127.        
  128.         sqlite3_stmt *compiledStatement;
  129.        
  130.         if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
  131.            
  132.             // Loop through the results and add them to the feeds array
  133.             while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
  134.                
  135.                 // Read the data from the result row
  136.                
  137.                 //Unique ID Field
  138.                
  139.                 NSMutableString *bUniqueID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
  140.                
  141.                 // Name Field
  142.                
  143.                 NSMutableString *bName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
  144.                
  145.                 // Address Field
  146.                  NSMutableString *bAddress = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
  147.                
  148.                 // Latitude Field
  149.                 NSMutableString *bLatitude = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
  150.                
  151.                 // Longitude Field
  152.                 NSMutableString *bLongitude = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
  153.                
  154.                // Type Field
  155.                 NSMutableString *bType = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
  156.                
  157.                
  158.                
  159.                
  160.                 // This is to remove the double quotes at the beginning and end of line
  161.                 NSString *UniqueID = [bUniqueID substringWithRange:NSMakeRange(1, bUniqueID.length -2)];
  162.                 NSString *name =  [bName substringWithRange:NSMakeRange(1, bName.length -2)];
  163.                 NSString *address = [bAddress substringWithRange:NSMakeRange(1, bAddress.length -2)];
  164.                 NSString *latitude =  [bLatitude substringWithRange:NSMakeRange(1, bLatitude.length -2)];
  165.                 NSString *longitude =  [bLongitude substringWithRange:NSMakeRange(1, bLongitude.length -2)];
  166.                 NSString *type =  [bType substringWithRange:NSMakeRange(1, bType.length -2)];
  167.                
  168.                
  169.                 // Important To Print Out Values To Ensure Correctness
  170.                 // Debugging Step 1
  171.  
  172.                 // My Error 1
  173.                 // %g is for type double
  174.                 //NSLog(@"Name: %@ Latitude: %g Longitude: %g", name, latitude, longitude);
  175.  
  176.                 // Corect Output
  177.                 // I converted to double since CLLocationCoordinates2D are of type double
  178.               //  NSLog(@"Name: %@ Latitude: %g Longitude: %g", name, [latitude doubleValue], [longitude doubleValue]);
  179.  
  180.                              
  181.                
  182.                 // Create new building object and then add it to the items array.
  183.                
  184.                
  185.                
  186.                 [items addObject:[Building buildingWithID:UniqueID name:name address:address latitude:latitude longitude:longitude type:type]];
  187.              
  188.  
  189.             }
  190.         }
  191.        
  192.         // Release the compiled statement from memory
  193.         sqlite3_finalize(compiledStatement);
  194.        
  195.     }
  196.     // Nifty Sorting Tricks in Objective-C
  197.    
  198.    
  199.    
  200.     // Important To Print Out Values for Objects
  201.     // Debug Step 2
  202.     /*
  203.     for (Building *theBuilding in items) {
  204.        
  205.         NSLog(@"Name: %@ Latitude: %g Longitude: %g", theBuilding.name, [theBuilding.latitude doubleValue], [theBuilding.longitude doubleValue]);
  206.        
  207.     }
  208.     */
  209.    
  210.    
  211.     // Nifty Objective-C Method to Print Out Values using For Loops
  212.  
  213.     NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
  214.     NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
  215.     NSArray *sortedArray = [items sortedArrayUsingDescriptors:sortDescriptors];
  216.    
  217.     self.items = [sortedArray mutableCopy];
  218.    
  219.     sqlite3_close(database);
  220.    
  221. }
  222.  
  223.  
  224.  
  225. //---------------- END OF ADDED CODE ------------------------------
  226.  
  227.  
  228.  
  229. @end
');