AppDelegate.h File
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    NSString *databasePath;
}
@property (strong, nonatomic) UIWindow *window;
- (NSString *) getDatabasepath;
@end
AppDelegate.m File
#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self checkAndCreateDatabase];
}
//DB is copied from Project to Document Directory
-(void) checkAndCreateDatabase
{
 BOOL success;
 NSError *error;
 NSFileManager *fileManager = [NSFileManager defaultManager];
 NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory=[paths objectAtIndex:0];
 databasePath=[documentsDirectory stringByAppendingFormat:@“/DB.sqlite"];
 NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"DB.sqlite"];
 success = [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:&error];
}
//Find the DB Path from Document Directory
- (NSString *) getDatabasepath
{
 NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory=[paths objectAtIndex:0];
 databasePath=[documentsDirectory stringByAppendingFormat:@"/DB.sqlite"];
 return databasePath;
}
DBHelpher.h File
@interface DBHelper : NSObject
{
    NSMutableArray* tempArray;
    NSMutableDictionary* tempDict;
}
-(NSMutableArray *)get_images_from_web;
-(void)updateDate:(NSString *)dte
@end
DBHelpher.m File
#import "DBHelper.h"
#import "AppDelegate.h"
@implementation DBHelper
static sqlite3_stmt *Images_statement = nil;
static sqlite3_stmt *Images_count_statement = nil;
-(NSMutableArray *)get_images_from_web
{
    tempArray = [[NSMutableArray alloc] init];
    NSString *status=@"Y";
    NSString *databasePath=[(AppDelegate *)[[UIApplication sharedApplication] delegate] getDatabasepath];
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
        const char *sql = "select Logo_url from tablename where status = 'Y'";
        if (sqlite3_prepare_v2(database, sql, -1, &Images_statement, NULL) != SQLITE_OK)
        {
            NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
        }
        sqlite3_bind_text(Images_statement, 1, [status UTF8String],-1,SQLITE_TRANSIENT);
        // Loop through the results and add them to the feeds array
        while(sqlite3_step(Images_statement) == SQLITE_ROW)
        {
            NSString *imageurl = [NSString stringWithFormat:@"%s",sqlite3_column_text(Images_statement, 0)];
            [tempArray addObject:imageurl];
        }
        sqlite3_reset(Images_statement);
        sqlite3_finalize(Images_statement);
    }
    sqlite3_close(database);
    return tempArray;
}
-(void)update_img_count:(int)count_val
{
    NSString *sql = @"";
    NSString *databasePath=[(AppDelegate *)[[UIApplication sharedApplication] delegate] getDatabasepath];
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
        sql = [NSString stringWithFormat:@"UPDATE images_count_table SET img_count = '%d' WHERE serial = 1", count_val];
        if(sqlite3_prepare_v2(database, [sql UTF8String], -1, & Images_count_statement, NULL) != SQLITE_OK)
        {
            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
        }
        NSLog(@"update query : %@", sql);
        if(SQLITE_DONE != sqlite3_step(Images_count_statement))
            NSLog(@"Error while inserting data. '%s'", sqlite3_errmsg(database));
        sqlite3_reset(Images_count_statement);
        sqlite3_finalize(Images_count_statement);
    }
    sqlite3_close(database);
}
ViewController.h File
#import <UIKit/UIKit.h>
#import "DBHelper.h"
@interface ViewController : GAITrackedViewController
{
    DBHelper *dbHelper;
}
@end
ViewController.m File
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated
{
 dbHelper = [[DBHelper alloc] init];
    [self Images_Add_to_Local];
 [dbHelper update_img_count:20];
}
-(void)Images_Add_to_Local
{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    if (![[defaults objectForKey:@"Images_From_Web"] isEqualToString:@"Downloaded"])
    {
        [defaults setObject:@"Downloaded" forKey:@"Images_From_Web"];
        [defaults synchronize];
        NSMutableArray *imagesArray = [dbHelper get_images_from_web];
        for (int i=1; i<=[imagesArray count]; i++)
        {
            NSString *str_image_name = [NSString stringWithFormat:@"%d.jpg",i];
            NSURL *url = [NSURL URLWithString:[imagesArray objectAtIndex:i-1]];
            NSData *pictureData = [NSData dataWithContentsOfURL:url];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documents = [paths objectAtIndex:0];
            NSString *finalPath = [documents stringByAppendingPathComponent:str_image_name];
            [pictureData writeToFile:finalPath atomically:YES];
        }
    }
}
No comments:
Post a Comment