Har ikke så mye imot å dele koden, den er fra en bok så jeg eier den ikke
CityGuideDelegate.h er#import <UIKit/UIKit.h>
@class RootController;
@interface CityGuideDelegate:NSObject <UIApplicationDelegate> {
UIWindow *window;
RootController *viewController;
NSMutableArray *cities;
UINavigationController *navController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet RootController *viewController;
@property (nonatomic, retain) IBOutlet UINavigationController *navController;
@property (nonatomic, retain) NSMutableArray *cities;
-(NSString *)copyDatabaseToDocuments;
-(void)readCitiesFromDatabaseWithPath:(NSString *)filePath;
@end
CityGuideDelegate.m er#include "sqlite3.h"
#import "CityGuideDelegate.h"
#import "RootController.h"
#import "city.h"
@implementation CityGuideDelegate
@synthesize window;
@synthesize viewController;
@synthesize cities;
@synthesize navController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
cities = [[NSMutableArray alloc] init];
NSString *filePath = [self copyDatabaseToDocuments];
[self readCitiesFromDatabaseWithPath:filePath];
navController.viewControllers = [NSArray arrayWithObject:viewController];
[window addSubview:navController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[cities release];
[super dealloc];
}
-(NSString *)copyDatabaseToDocuments {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath
stringByAppendingPathComponent:@"cities.sqlite"];
if ( ![fileManager fileExistsAtPath:filePath] ) {
NSString *bundlePath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"cities.sqlite"];
[fileManager copyItemAtPath:bundlePath toPath:filePath error:nil];
}
-(void)readCitiesFromDatabaseWithPath:(NSString *)filePath {
sqlite3 *database;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "select * cities";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement,
-1, &compiledStatement, NULL) ==SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSString *cityName =
[NSString stringWithUTF8String:(char *)
sqlite3_column_text(compiledStatement, 1)];
NSString *cityDescription =
[NSString stringWithUTF8String:(char *)
sqlite3_column_text(compiledStatement, 2 )];
NSData *cityData = [[NSData alloc]
initWithBytes:sqlite3_column_blob(compiledStatement, 3)
length: sqlite3_column_bytes(compiledStatement, 3)];
UIImage *cityImage = [UIImage imageWithData:cityData];
City *newCity = [[City alloc] init];
newCity.cityName = cityName;
newCity.cityDescription = cityDescription;
newCity.cityPicture = (UIImage *)cityImage;
[self.cities addObject:newCIty];
[newCity release];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
@end
Takk for at du tar deg tid